相关文章推荐
拉风的猕猴桃  ·  You have JVM Property ...·  7 月前    · 
跑龙套的感冒药  ·  Matlab ...·  1 年前    · 
卖萌的勺子  ·  Android ...·  1 年前    · 
活泼的石榴  ·  error 500 ...·  1 年前    · 
[{ "cid" : "1" , "width" : "1860px" , "height" : "80px" , "top" : "0px" , "left" : "0px" }, { "cid" : "2" , "width" : "500px" , "height" : "410px" , "top" : "80px" , "left" : "0px" }, { "cid" : "3" , "width" : "860px" , "height" : "820px" , "top" : "80px" , "left" : "500px" }, { "cid" : "4" , "width" : "500px" , "height" : "410px" , "top" : "80px" , "left" : "1360px" }, { "cid" : "5" , "width" : "500px" , "height" : "410px" , "top" : "490px" , "left" : "0px" }, { "cid" : "6" , "width" : "500px" , "height" : "410px" , "top" : "490px" , "left" : "1360px" }]

其中每个图表有 5 个参数:

cid 是图表编号,对应后面作图将用到的 chart_id;

width 是图表的宽度;

height 是图表的高度;

top 是图表距离顶部的位置;

left 是图表距离左边的位置。

这些参数决定了每个图表在大屏中的大小和位置。

然后,在 Jupyter Lab 中新建一个 Notebook,运行以下代码:

# 导入相关库 from pyecharts import options as opts from pyecharts.charts import Map , Bar , Grid , Line , Page , Pie , Polar from pyecharts.commons.utils import JsCode from pyecharts.globals import ThemeType import pandas as pd

# 从 data 文件夹读取 Excel 中的数据 df = pd.read_excel( './data/超市数据.xlsx' )

# 用数据透视表的方法汇总各省销售额 province_sale = df.pivot_table(values= '销售额' , index= '省/自治区' , aggfunc=sum) # 各地区销售额 district_sale = df.pivot_table(values= '销售额' , index= '地区' , aggfunc=sum) # 每天各类产品销售额 daily_sale = df.fillna( 0 ).pivot_table(values= '销售额' , index= '订单日期' , columns= '类别' , aggfunc=sum).fillna( 0 ) # 细分类别销售额 parts_sale = df.fillna( 0 ).pivot_table(values= '销售额' , index= '子类别' , aggfunc=sum).fillna( 0 ) # 邮寄方式销售额 mail_sale = df.fillna( 0 ).pivot_table(values= '销售额' , index= '邮寄方式' , aggfunc=sum).fillna( 0 )

# 把数据整理成绘图所需的列表格式,规范名称,销售额换算成万元,保留一位小数 list_province_sale = [list(z) for z in zip(province_sale.index.str.replace( '自治区' , '' ), round(province_sale.销售额/ 10000 , 1 ))]

# 求各省销售额的最大值 max_sale = round(province_sale.销售额.max/ 10000 , 0 )

# 数据可视化大屏的标题 def main_title: c = ( Pie (init_opts=opts. InitOpts (chart_id= 1 , bg_color= '#00589F' )) .set_global_opts( title_opts=opts. TitleOpts (title= "超市数据管理驾驶舱" , title_textstyle_opts=opts. TextStyleOpts (font_size= 36 , color= '#FFFFFF' ), pos_left= 'center' , pos_top= 'middle' )) ) return c

# 柱形图 def bar_sale -> Bar : c = ( Bar (init_opts=opts. InitOpts (chart_id= 2 , bg_color= '#00589F' )) .add_xaxis(list(district_sale.index)) .add_yaxis( "" , list(round(district_sale.销售额/ 10000 , 1 )), color= '#5D9BCF' , label_opts=opts. LabelOpts (color= '#FFFFFF' )) .set_global_opts( title_opts=opts. TitleOpts (title= "各地区销售额汇总" , pos_left= 'center' , pos_top= '10' , title_textstyle_opts=opts. TextStyleOpts (color= "#FFFFFF" , font_size= 16 )), xaxis_opts=opts. AxisOpts ( axislabel_opts=opts. LabelOpts (color= '#999999' ), axistick_opts=opts. AxisTickOpts (is_align_with_label= True ), axisline_opts=opts. AxisLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#999999' )), ), yaxis_opts=opts. AxisOpts ( axislabel_opts=opts. LabelOpts (color= '#999999' ), axisline_opts=opts. AxisLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#999999' )), ), ) ) return c

# 数据地图 def map_china -> Map : # 绘制地图 c = ( # 主题样式和背景颜色 Map (init_opts=opts. InitOpts (chart_id= 3 , bg_color= '#00589F' )) # 添加地图数据 .add(series_name= "省份" , data_pair=list_province_sale, maptype= "china" , is_map_symbol_show= False ) # 全局配置 .set_global_opts( # 标题文字 title_opts=opts. TitleOpts (title= "{:,}" .format( int (province_sale.销售额.sum)), # 副标题 subtitle= 'tt2016年到2019年总销售额' , # 标题位置 pos_left= 'center' , pos_top= 20 , # 标题颜色和大小 title_textstyle_opts=opts. TextStyleOpts (color= "#FBC171" , font_size= 36 ), # 副标题颜色和大小 subtitle_textstyle_opts=opts. TextStyleOpts (color= "#CCCCCC" , font_size= 15 )), legend_opts=opts. LegendOpts (is_show= False ), # 可视化组件参数 visualmap_opts=opts. VisualMapOpts ( # 最大值 max_=max_sale, # 显示精确的分段值 is_piecewise= True , # 图例位置 pos_left= '36%' , pos_bottom= 50 , # 图例字体颜色 textstyle_opts=opts. TextStyleOpts (color= "#FFFFFF" )), ) # 隐藏文字标签 .set_series_opts(label_opts=opts. LabelOpts (is_show= False )) ) return c

# 折线图 def line_daily -> Line : c = ( Line (init_opts=opts. InitOpts (chart_id= 4 , bg_color= '#00589F' )) .add_xaxis(list(daily_sale.iloc[- 30 :,:].index.strftime( '%Y-%m-%d' ))) .add_yaxis( "办公用品" , list(round(daily_sale.iloc[- 30 :,:].办公用品, 1 )), is_smooth= True , color= '#5D9BCF' ) .add_yaxis( "家具" , list(round(daily_sale.iloc[- 30 :,:].家具, 1 )), is_smooth= True , color= '#FBC171' ) .set_series_opts( areastyle_opts=opts. AreaStyleOpts (opacity= 0.2 ), label_opts=opts. LabelOpts (is_show= False ), ) .set_global_opts( title_opts=opts. TitleOpts (title= "分类产品每天销售额变化趋势" , pos_left= 'center' , pos_top= '10' , title_textstyle_opts=opts. TextStyleOpts (color= "#FFFFFF" , font_size= 16 )), xaxis_opts=opts. AxisOpts ( axislabel_opts=opts. LabelOpts (color= '#999999' ), axistick_opts=opts. AxisTickOpts (is_align_with_label= True ), axisline_opts=opts. AxisLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#999999' )), is_scale= False , boundary_gap= False ), yaxis_opts=opts. AxisOpts ( axislabel_opts=opts. LabelOpts (color= '#999999' ), axisline_opts=opts. AxisLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#999999' )), ), legend_opts=opts. LegendOpts (pos_top= '36' , textstyle_opts=opts. TextStyleOpts (color= "#FFFFFF" , font_size= 12 )), ) ) return c

# 饼图 def pie_parts -> Pie : x_data = list(parts_sale.index) y_data = list(round(parts_sale.销售额/ 10000 , 1 )) data_pair = [list(z) for z in zip(x_data, y_data)] data_pair.sort(key= lambda x: x[ 1 ])

c = ( Pie (init_opts=opts. InitOpts (chart_id= 5 , bg_color= "#00589F" , theme= ThemeType .MACARONS)) .add( series_name= "细分产品类别" , data_pair=data_pair, rosetype= "radius" , radius= "75%" , center=[ "50%" , "50%" ], label_opts=opts. LabelOpts (is_show= False , position= "center" ), ) .set_global_opts( title_opts=opts. TitleOpts ( title= "细分产品类别分布" , pos_left= "center" , pos_top= "0" , title_textstyle_opts=opts. TextStyleOpts (color= "#FFFFFF" ), ), legend_opts=opts. LegendOpts (is_show= False ), ) ) return c

# 极坐标图 def polar_parts -> Polar : c = ( Polar (init_opts=opts. InitOpts (chart_id= 6 , bg_color= "#00589F" )) .add_schema( radiusaxis_opts=opts. RadiusAxisOpts (data=list(mail_sale.index), type_= "category" , axisline_opts=opts. AxisLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#999999' )), axislabel_opts=opts. LabelOpts (color= "#999999" ) ), angleaxis_opts=opts. AngleAxisOpts (is_clockwise= True , split_number= 9 , axisline_opts=opts. AxisLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#999999' )), splitline_opts=opts. SplitLineOpts (linestyle_opts=opts. LineStyleOpts (color= '#00589F' )), axislabel_opts=opts. LabelOpts (color= "#999999" )), ) .add( "" , list(round(mail_sale.销售额/ 10000 , 1 )), type_= "bar" , ) .set_global_opts( title_opts=opts. TitleOpts (title= "不同邮寄方式的销售额" , pos_left= 'center' , pos_top= '0' , title_textstyle_opts=opts. TextStyleOpts (color= "#FFFFFF" , font_size= 16 ) ) ) .set_series_opts( label_opts=opts. LabelOpts (color= "#999999" ), ) .set_colors([ "#FBC171" ]) ) return c

# 可拖动的页面 page = Page (layout= Page . DraggablePageLayout , page_title= "超市数据管理驾驶舱" )

# 在页面中添加图表 page.add( main_title, bar_sale, map_china, line_daily, pie_parts, polar_parts, )

# 调用绘制函数后生成一个 temp.html 文件 _ = page.render( 'temp.html' )

# 新建 chart_config.json 文件,定义图表位置

# 生成数据可视化大屏的文件 _ = page.save_resize_html( 'temp.html' , cfg_file= 'chart_config.json' , dest= '超市数据管理驾驶舱.html' )

运行结束之后,系统会自动生成一个文件:超市数据管理驾驶舱.html,用浏览器打开它,就能看到一个数据可视化大屏:超市数据管理驾驶舱。

这是一个简化版的数据可视化大屏,你可以根据自己的业务情况,替换成自己的真实数据,修改为自己想要的图表和颜色样式等。

------------------------------------- 返回搜狐,查看更多

责任编辑: