python 柱状图工具类

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from imp import reload

import xlsxwriter

import sys  

reload(sys)

sys.setdefaultencoding('utf-8')

import xlsxwriter

# 默认渲染方法 (表格实例, 页实例, 处理方式[True: 行处理(默认) | False: 列处理 ],  标题, x坐标标题, y坐标标题)

def create_chart(workbook, worksheet, select, xy_default = True, title=None, x_name=None, y_name=None):

    chart = workbook.add_chart({'type':'column'})

    if title:

        chart.set_title({'name': title})

    if x_name:

        chart.set_x_axis({'name': x_name})

    if y_name:

        chart.set_y_axis({'name': y_name})

    chart.set_style(1)

    sheet, row_and_col = select.split("!")

    start, end = row_and_col.split(":")

    # 预造颜色

    color_list = ["#4B0082","#FFD700","#FF69B4","#B22222","#CD5C5C","#FFFF00","#FFE4E1","#556B2F","#808000","#8FBC8F","#FFC0CB","#FF6347","#F08080","#FF4500","#FFDEAD"

        ,"#00FF00","#98FB98","#ADFF2F","#DEB887","#FFF5EE","#00FA9A","#FF00FF","#FFEFD5","#FFEBCD","#7FFF00","#696969","#000000","#FFDAB9","#00FF7F","#7FFFD4","#FFFFFF","#FFA500","#FFA07A",

        "#2F4F4F","#A52A2A","#FFFFF0","#1E90FF","#CD853F","#7CFC00","#D2691E","#DC143C","#228B22","#6A5ACD","#20B2AA","#00FFFF","#F5FFFA","#C0C0C0","#FAEBD7","#BA55D3","#87CEEB","#808080",

        "#00CED1","#DAA520","#006400","#FFFAF0","#9400D3","#A9A9A9","#FFE4B5","#8B4513","#483D8B","#87CEFA","#FFB6C1","#C71585","#FF0000","#FF1493","#32CD32","#8B008B","#EEE8AA","#DDA0DD",

        "#40E0D0","#FAFAD2","#B8860B","#E6E6FA","#800000","#9ACD32","#FAA460","#D8BFD8","#EE82EE","#000080","#FF00FF","#D2B48C","#BC8F8F","#6B8E23","#0000FF","#ADD8E6","#F8F8FF","#F0FFF0",

        "#6495ED","#FAF0E6","#00008B","#B0E0E6","#2E8B57","#BDB76B","#FFFAFA","#A0522D","#0000CD","#4169E1","#E0FFFF","#008000","#9370DB","#191970","#FFF8DC","#AFEEEE","#FFE4C4","#708090",

        "#008B8B","#F0E68C","#F5DEB3","#008080","#9932CC","#00BFFF","#FA8072","#8B0000","#4682B4","#DB7093","#778899","#F0F8FF","#90EE90","#DA70D6","#DCDCDC","#3CB371","#D3D3D3","#48D1CC",

        "#FFFACD","#5F9EA0","#FFFFE0","#FFF0F5","#FF7F50","#800080","#00FFFF","#F5F5F5","#7B68EE","#FF8C00","#66CDAA","#E9967A","#F5F5DC","#8A2BE2","#F0FFFF","#B0C4DE","#FDF5E6"]

    if(xy_default):

        item = start.split("$")[2]

        for i in range(ord(start.split("$")[1]),ord(end.split("$")[1])):

            data = {'name':'=%(sheet)s!$%(cow)s$%(row)s' % {"sheet": sheet, "cow": chr(i+1), "row": item},

              'categories':'=%(sheet)s!$%(start_col)s$%(start_row)s:$%(start_col)s$%(end_row)s'

               % {"sheet": sheet, "start_col": start.split("$")[1], "start_row": int(start.split("$")[2])+1 ,"end_row": end.split("$")[2]},

              'values':   '=sheet1!$%(col)s$%(start_row)s:$%(col)s$%(end_row)s'

              % {"sheet": sheet, "col": chr(i+1), "start_row": int(start.split("$")[2])+1, "end_row": end.split("$")[2]},

               'fill': {'color': color_list[i]},

              }

            print(data)

            chart.add_series(data)

    else:

        item = start.split("$")[1]

        for i in range(int(start.split("$")[2]), int(end.split("$")[2])):

            data = {'name':'=%(sheet)s!$%(cow)s$%(row)s' % {"sheet": sheet, "cow": item, "row": i+1},

              'categories':'=%(sheet)s!$%(start_col)s$%(start_row)s:$%(end_col)s$%(start_row)s'

               % {"sheet": sheet, "start_col": chr(ord(start.split("$")[1])+1), "start_row": int(start.split("$")[2]) ,"end_col": chr(ord(end.split("$")[1]))},

              'values':   '=sheet1!$%(start_col)s$%(row)s:$%(end_col)s$%(row)s'

              % {"sheet": sheet, "start_col": chr(ord(start.split("$")[1])+1), "end_col": chr(ord(end.split("$")[1])), "row": i+1},

               'fill': {'color': color_list[i-ord('A')]},

              }

            print(data)

            chart.add_series(data)

    chart.set_size({'width': 900, 'height': 376})

    #首页最低行插入

    worksheet.insert_chart(start.split("$")[1] + str((int(end.split("$")[2])+1)),chart, {'x_offset':25,'y_offset':10,'x_scale': 1,'y_scale': 1})


 

#导入模块

workbook = xlsxwriter.Workbook('new_excel.xlsx')   #创建新的excel

worksheet = workbook.add_worksheet('sheet1')        #创建新的sheet

headings = ['Number','testA','testB']             #创建表头

data = [

    ['2017-9-1','2017-9-2','2017-9-3','2017-9-4','2017-9-5','2017-9-6'],

    [10,40,50,20,10,50],

    [30,60,70,50,40,30],

]                                                       #自己造的数据

worksheet.write_row('A1',headings)

worksheet.write_column('A2',data[0])

worksheet.write_column('B2',data[1])

worksheet.write_column('C2',data[2])

create_chart(workbook, worksheet, "sheet1!$A$1:$C$7", xy_default = False, title="测试标题", x_name="x_name", y_name="y_name")

workbook.close()

你可能感兴趣的:(python,python,开发语言,信息可视化)