""" author:少校 create Time:2025/2/28 16:39 越努力越幸运 """ #根据excle表格制作看板 import openpyxl wb = openpyxl.load_workbook("charts/2020年销售数据.xlsx") sheet1 = wb["data"] mr = sheet1.max_row xsqy={} xsqd={} pp={} xl={} for row in range(2,mr+1): if sheet1.cell(row,2).value not in xsqy: xsqy[sheet1.cell(row,2).value] = sheet1.cell(row, 7).value * sheet1.cell(row, 6).value else: xsqy[sheet1.cell(row, 2).value]+=sheet1.cell(row, 7).value * sheet1.cell(row, 6).value if sheet1.cell(row,3).value not in xsqd: xsqd[sheet1.cell(row, 3).value] = sheet1.cell(row, 7).value else: xsqd[sheet1.cell(row, 3).value] += sheet1.cell(row, 7).value if sheet1.cell(row,5).value not in pp: pp[sheet1.cell(row, 5).value] = sheet1.cell(row, 7).value else: pp[sheet1.cell(row, 5).value] += sheet1.cell(row, 7).value if sheet1.cell(row,2).value not in xl: xl[sheet1.cell(row, 2).value] = sheet1.cell(row, 7).value else: xl[sheet1.cell(row, 2).value] += sheet1.cell(row, 7).value #制图 from pyecharts.charts import Bar, Line, Pie, Map, Grid, Page from pyecharts import options from pyecharts.globals import ThemeType # 图1 x1=[] y1=[] for x in xsqd: x1.append(x) y1.append(xsqd[x]) print(x1,y1) bar = Bar(init_opts=options.InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(x1) bar.add_yaxis('渠道', y1) bar.set_global_opts( title_opts=options.TitleOpts( title='各渠道订单量', pos_left='240' ), legend_opts=options.LegendOpts( pos_right=85, pos_top=30 ) ) g1 = Grid(init_opts=options.InitOpts( width='600px', height='400px' )) g1.add(bar, grid_opts=options.GridOpts()) # 图2 x2=[] y2=[] for x in pp: x2.append(x) y2.append(pp[x]) print(x2,y2) line = Line(init_opts=options.InitOpts(theme=ThemeType.LIGHT)) line.add_xaxis(x2) line.add_yaxis('品牌销量', y2) g2 = Grid(init_opts=options.InitOpts( width='600px', height='400px' )) g2.add(line, grid_opts=options.GridOpts()) # 图3 data=[] for x in xl: data.append((x,xl[x])) print(data) pie = Pie() pie.add( '地区销量', data, radius=(50, 150), # (内半径, 外半径) rosetype='radius', # 让外半径的值根据占比自动调整(制作不规则饼图) ) pie.set_series_opts( # 设置数据标签的格式 label_opts=options.LabelOpts( # {b} - 名称 {d} - 百分比比值 formatter='{b}({d}%)' ) ) pie.set_global_opts( legend_opts=options.LegendOpts( is_show=False ), title_opts=options.TitleOpts( title='各地区销售数量', pos_left='330' ) ) g3 = Grid(init_opts=options.InitOpts( width='600px', height='400px' )) g3.add(pie, grid_opts=options.GridOpts()) # 图4 map1 = Map() data2=[] for x in xsqy: if x == "北京" or x == "上海" or x == "南京": data2.append((f"{x}市",xsqy[x])) else: data2.append((f"{x}省", xsqy[x])) print(data2) map1.add( '地区销售额', data2, zoom=1.3, # 默认大小为原地图的1.5倍 pos_top=100, max_scale_limit=3, # 缩放最大倍数为3 min_scale_limit=0.7 # 缩放最小倍数为0.7 ) map1.set_global_opts( visualmap_opts=options.VisualMapOpts( is_show=False, max_=20000000, min_=500000, is_piecewise=True ), legend_opts=options.LegendOpts( is_show=False ), title_opts=options.TitleOpts( title='全国各地区销售额', pos_left='340' ) ) g4 = Grid(init_opts=options.InitOpts( width='600px', height='400px' )) g4.add(map1, grid_opts=options.GridOpts()) # 2. 创建一个Page对象作为大的盒子来存放所有的图对应的小盒子 page = Page(layout=Page.SimplePageLayout) page.add(g1, g2, g3, g4) page.render('charts/homework.html')
2月28日作业
本节3903字2025-03-03 17:26:39