def theme_infographic() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-infographic"))) return c
def theme_westeros() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.WESTEROS)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-westeros"))) return c
def theme_wonderland() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-wonderland"))) return c
def theme_vintage() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-vintage"))) return c
def theme_walden() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.WALDEN)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-walden"))) return c
def grid_horizontal() -> Grid: scatter = ( Scatter() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts( title_opts=opts.TitleOpts(title="Grid-Scatter"), legend_opts=opts.LegendOpts(pos_left="20%"), ) ) line = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts( title_opts=opts.TitleOpts(title="Grid-Line", pos_right="5%"), legend_opts=opts.LegendOpts(pos_right="20%"), ) ) grid = ( Grid() .add(scatter, grid_opts=opts.GridOpts(pos_left="55%")) .add(line, grid_opts=opts.GridOpts(pos_right="55%")) ) return grid
def grid_vertical() -> Grid: bar = ( Bar() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts(title_opts=opts.TitleOpts(title="Grid-Bar")) ) line = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts( title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"), legend_opts=opts.LegendOpts(pos_top="48%"), ) ) grid = ( Grid() .add(bar, grid_opts=opts.GridOpts(pos_bottom="60%")) .add(line, grid_opts=opts.GridOpts(pos_top="60%")) ) return grid
def scatter_value_xaxis() -> Scatter: c = (Scatter().add_xaxis(Faker.values()).add_yaxis( "商家A", Faker.values()).set_global_opts( title_opts=opts.TitleOpts(title="Scatter-X 数值轴"), xaxis_opts=opts.AxisOpts(type_="value"), tooltip_opts=opts.TooltipOpts(formatter="{c}"))) return c
def theme_macarons() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-macarons"))) return c
def line_base(): c = ( Line() # 传入X轴数据,选择七个品类中的一种中的七样东西 .add_xaxis(Faker.choose()) # 传入Y轴数据,第一个参数是系列的名称,第二个参数是数据(随机选择20-150之间的整数) # 平滑曲线加上is_smooth=True即可自动平滑 .add_yaxis('商家A', Faker.values(), is_smooth=True) # 隐藏某系列数据False,隐藏后图例是灰色,点击就可以显示出来,默认参数是True显示 .add_yaxis('商家B', Faker.values(), is_smooth=True, is_selected=True) # 设置数据配置项 # 每个商品的数量是显示在点的上部,改变数据的颜色 .set_series_opts(label_opts=opts.LabelOpts( is_show=True, color='red', position='top')) # 设置全局配置项 .set_global_opts( # 显示主副标题 title_opts=opts.TitleOpts(title='主标题:商家', subtitle='副标题:销售记录'), # 显示工具栏,工具栏用于刷新图片,下载图片,查看图片数据等 toolbox_opts=opts.ToolboxOpts(), # 隐藏图例False,默认显示图例True,没有该设置,默认所有图例都显示 legend_opts=opts.LegendOpts(is_show=True), # 设置Y轴坐标刻度 yaxis_opts=opts.AxisOpts(min_=0, max_=180))) # 绘制图片 pic = c.render() return pic
def district_score_box_plot(df, district, school_name): try: # school_name=None middle_score = middle_school_data4box(df) labels = list(middle_score.columns) c = Boxplot(init_opts=opts.InitOpts(width='1750px')) c.add_xaxis([""]) for col in labels: value = [list(middle_score[col].dropna())] c.add_yaxis(col, c.prepare_data(value)) c.set_global_opts( title_opts=opts.TitleOpts(title=f"{district}-{school_name}成绩分布"), legend_opts=opts.LegendOpts(is_show=False), yaxis_opts=opts.AxisOpts(min_=400, max_=750)) return c except: v2 = [Faker.values(450, 650)] v1 = [Faker.values(500, 700)] c = Boxplot(init_opts=opts.InitOpts(width='1750px')) c.add_xaxis(['']) c.add_yaxis("中学A", c.prepare_data(v1)).add_yaxis("中学B", c.prepare_data(v2)) c.set_global_opts(title_opts=opts.TitleOpts( title=f"{district}初中成绩分布(示例)", subtitle="当看到此页面,说明所选区目前无数据")) return c
def theme_roma() -> Bar: c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA)).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-roma"))) return c
def bar_base(): # 采用链式写法,先传入所有的数据和配置 c = ( Bar() # 传入X轴数据,选择七个品类中的一种中的七样东西 .add_xaxis(Faker.choose()) # 传入Y轴数据,第一个参数是系列的名称,第二个参数是数据(随机选择20-150之间的整数) .add_yaxis('商家A', Faker.values()) # 隐藏某系列数据False,隐藏后图例是灰色,点击就可以显示出来,默认参数是True显示 .add_yaxis('商家B', Faker.values(), is_selected=True) # 翻转XY轴,变成横条图,X轴竖向显示,Y轴横向显示 .reversal_axis() # 柱状图的每个商品的数量是显示在柱子顶部,翻转后数据要显示在右侧 .set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='right')) # 设置全局配置项 .set_global_opts( # 显示主副标题 title_opts=opts.TitleOpts(title='主标题:商家', subtitle='副标题:销售记录'), # 显示工具栏,工具栏用于刷新图片,下载图片,查看图片数据等 toolbox_opts=opts.ToolboxOpts(), # 隐藏图例False,默认显示图例True,没有该设置,默认所有图例都显示 legend_opts=opts.LegendOpts(is_show=True), ) ) # 绘制图片 pic = c.render() return pic
def timeline_map() -> Timeline: map0 = (Map().add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china").set_global_opts( title_opts=opts.TitleOpts(title="Map-2015年某些数据"), visualmap_opts=opts.VisualMapOpts(max_=200), )) map1 = (Map().add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china").set_global_opts( title_opts=opts.TitleOpts(title="Map-2016年某些数据"), visualmap_opts=opts.VisualMapOpts(max_=200), )) map2 = (Map().add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china").set_global_opts( title_opts=opts.TitleOpts(title="Map-2017年某些数据"), visualmap_opts=opts.VisualMapOpts(max_=200), )) map3 = (Map().add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china").set_global_opts( title_opts=opts.TitleOpts(title="Map-2018年某些数据"), visualmap_opts=opts.VisualMapOpts(max_=200), )) map4 = (Map().add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china").set_global_opts( title_opts=opts.TitleOpts(title="Map-2019年某些数据"), visualmap_opts=opts.VisualMapOpts(max_=200), )) tl = (Timeline().add(map0, "2015年").add(map1, "2016年").add(map2, "2017年").add( map3, "2018年").add(map4, "2019年")) return tl
def bar_reversal_axis() -> Bar: c = (Bar().add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).reversal_axis().set_series_opts( label_opts=opts.LabelOpts(position="right")).set_global_opts( title_opts=opts.TitleOpts(title="Bar-翻转 XY 轴"))) return c
def bar_stack0() -> Bar: c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values(), stack="stack1").add_yaxis( "商家B", Faker.values(), stack="stack1").set_series_opts( label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts(title="Bar-堆叠数据(全部)"))) return c
def bar_graphic_image_component() -> Bar: c = ( Bar() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts( title_opts=opts.TitleOpts(title="Bar-Graphic Image 组件示例"), graphic_opts=[ opts.GraphicImage( graphic_item=opts.GraphicItem( id_="logo", right=20, top=20, z=-10, bounding="raw", origin=[75, 75], ), graphic_imagestyle_opts=opts.GraphicImageStyleOpts( image="http://echarts.baidu.com/images/favicon.png", width=150, height=150, opacity=0.4, ), ) ], ) ) return c
def theme_default() -> Bar: c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).add_yaxis( "商家C", Faker.values()).add_yaxis("商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-default"))) return c
def bar_custom_bar_color() -> Bar: color_function = """ function (params) { if (params.value > 0 && params.value < 50) { return 'red'; } else if (params.value > 50 && params.value < 100) { return 'blue'; } return 'green'; } """ c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values(), itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)), ).add_yaxis( "商家B", Faker.values(), itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)), ).add_yaxis( "商家C", Faker.values(), itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)), ).set_global_opts(title_opts=opts.TitleOpts(title="Bar-自定义柱状颜色"))) return c
def bar_base(): # 采用链式写法,先传入所有的数据和配置 c = ( Bar() # 传入X轴数据,选择七个品类中的一种中的七样东西 .add_xaxis(Faker.choose()) # 传入Y轴数据,第一个参数是系列的名称,第二个参数是数据(随机选择20-150之间的整数) # 堆叠显示,加入堆叠系列名称,同一个系统堆叠在同一个柱子上,不堆叠的不传参数 .add_yaxis('商家A', Faker.values(), stack='stack1') # 隐藏某系列数据False,隐藏后图例是灰色,点击就可以显示出来,默认参数是True显示 .add_yaxis('商家B', Faker.values(), stack='stack1', is_selected=True) # 翻转XY轴,变成横条图,X轴竖向显示,Y轴横向显示 # .reversal_axis() # 柱状图的每个商品的数量是显示在柱子顶部,翻转后数据要显示在右侧 .set_series_opts(label_opts=opts.LabelOpts( is_show=True, color='black', position='top')) # 设置全局配置项 .set_global_opts( # 显示主副标题 title_opts=opts.TitleOpts(title='主标题:商家', subtitle='副标题:销售记录'), # 显示工具栏,工具栏用于刷新图片,下载图片,查看图片数据等 toolbox_opts=opts.ToolboxOpts(), # 隐藏图例False,默认显示图例True,没有该设置,默认所有图例都显示 legend_opts=opts.LegendOpts(is_show=True), # 设置Y轴坐标刻度,由于堆叠以后数据超过单独的最大数据150,需要将刻度调整 yaxis_opts=opts.AxisOpts(min_=0, max_=300))) # 绘制图片 pic = c.render() return pic
def theme_purple_passion() -> Bar: c = (Bar(init_opts=opts.InitOpts( theme=ThemeType.PURPLE_PASSION)).add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).add_yaxis( "商家C", Faker.values()).add_yaxis( "商家D", Faker.values()).set_global_opts( title_opts=opts.TitleOpts("Theme-purple-passion"))) return c
def bar_with_brush() -> Bar: c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).set_global_opts( title_opts=opts.TitleOpts(title="Bar-Brush示例", subtitle="我是副标题"), brush_opts=opts.BrushOpts(), )) return c
def bar_base_with_animation() -> Bar: c = (Bar(init_opts=opts.InitOpts(animation_opts=opts.AnimationOpts( animation_delay=1000, animation_easing="elasticOut"))).add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).set_global_opts(title_opts=opts.TitleOpts( title="Bar-动画配置基本示例", subtitle="我是副标题"))) return c
def scatter_visualmap_size() -> Scatter: c = (Scatter().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).set_global_opts( title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"), visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20), )) return c
def bar_toolbox() -> Bar: c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).set_global_opts( title_opts=opts.TitleOpts(title="Bar-显示 ToolBox"), toolbox_opts=opts.ToolboxOpts(), legend_opts=opts.LegendOpts(is_show=False), )) return c
def bar_base_dict_config() -> Bar: c = (Bar({ "theme": ThemeType.MACARONS }).add_xaxis(Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).set_global_opts(title_opts={ "text": "Bar-通过 dict 进行配置", "subtext": "我也是通过 dict 进行配置的" })) return c
def bar_xyaxis_name() -> Bar: c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).set_global_opts( title_opts=opts.TitleOpts(title="Bar-XY 轴名称"), yaxis_opts=opts.AxisOpts(name="我是 Y 轴"), xaxis_opts=opts.AxisOpts(name="我是 X 轴"), )) return c
def bar_yaxis_formatter() -> Bar: c = (Bar().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values()).add_yaxis("商家B", Faker.values()).set_global_opts( title_opts=opts.TitleOpts(title="Bar-Y 轴 formatter"), yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts( formatter="{value} /月")), )) return c
def line_base() -> Line: c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例")) ) return c
def line_smooth() -> Line: c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values(), is_smooth=True) .add_yaxis("商家B", Faker.values(), is_smooth=True) .set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth")) ) return c