def draw_map_world(data, to_file, svg_name, label_name, number_max): """ 画地图 :param data: :param to_file: :param svg_name: :param label_name: 图例名称 :param number_max: 最大值 :return: """ geo = Map(init_opts=opts.InitOpts(width="800px", height="600px", bg_color='rgb(255, 255, 255)')) \ .add(label_name, data, maptype="world") \ .set_series_opts(label_opts=opts.LabelOpts(is_show=False), showLegendSymbol=False) \ .set_global_opts(legend_opts=opts.LegendOpts(item_width=50, item_height=30, textstyle_opts=opts.TextStyleOpts(font_size=30)), visualmap_opts=opts.VisualMapOpts(min_=0, max_=int(number_max), background_color='rgb(255, 255, 255)', is_piecewise=True, item_width=50, item_height=30, textstyle_opts=opts.TextStyleOpts(font_size=30)), toolbox_opts=opts.ToolboxOpts( feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), ) ), ) # geo.render(to_file) make_snapshot(snapshot, geo.render(to_file), svg_name) # 生成svg图片
def pyechart_Sunburst_plot(self, filedata, para): from pyecharts.charts import Sunburst file_name = '旭日图.html' path_plotly = self.path_dir_plotly_html + os.sep + file_name # 文件路径,前面是文件夹后面是文件名 costumeTheme = self.themedict[para['theme']] # ----------------------------------------------------------------------- df = pd.read_excel(filedata, sheet_name='Sheet1') dic = self.Tree_dict(df) sunburst = ( Sunburst(init_opts=opts.InitOpts(theme=costumeTheme, width=para['width'], height=para['height']) ) # costumeTheme=self.themedict[para['theme']] .add(series_name="", data_pair=dic, radius=[ 0, "90%" ]).set_global_opts( title_opts=opts.TitleOpts(title=para["title"], subtitle=para["subtitle"]), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color="white"))), #visualmap_opts=opts.VisualMapOpts(), ).set_series_opts(label_opts=opts.LabelOpts( is_show=True, color=para["color"], position="inside", font_size=para["font_size"], font_family=para["font_family"])).render(path_plotly)) return path_plotly # 返回该HTML文件路径
def Line_charts(users, xaxis_data, yaxis_data, date) -> Line: """定义一个Line_charts函数""" c = Line() c.add_xaxis(xaxis_data=xaxis_data) # 设置图例信息 c.add_yaxis(series_name=str(users) + "users" + " " + "date: " + date, y_axis=yaxis_data, is_smooth=True) # c.add_yaxis(series_name='400users', y_axis=y2) data_zoom = { "show": False, "title": { "zoom": "data zoom", "back": "data zoom restore" } } # 数据项设置,全局只设置一次 c.set_global_opts( # 设置标题 title_opts=opts.TitleOpts(title="不同users对应的TPM值"), # 设置图例is_show=False是 不显示图例 legend_opts=opts.LegendOpts(is_show=True), # 设置提示项 tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'), # 工具箱的设置 toolbox_opts=opts.ToolboxOpts( is_show=False, feature=opts.ToolBoxFeatureOpts(data_zoom=data_zoom))) return c
def Common_code(self, chart, path_plotly, para): datazoom_opts = [] toolbox_opts = [] visualmap_opts = [] if para['DataZoom']: datazoom_opts = opts.DataZoomOpts() if para['ToolBox']: toolbox_opts = opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color="white", pixel_ratio=2))) if para['VisualMap']: visualmap_opts = opts.VisualMapOpts() chart.set_global_opts( title_opts=opts.TitleOpts(title=para["title"], subtitle=para["subtitle"]), legend_opts=opts.LegendOpts(item_width=50, item_height=25), # 图例,默认25/14 visualmap_opts=visualmap_opts, datazoom_opts=datazoom_opts, toolbox_opts=toolbox_opts, yaxis_opts=opts.AxisOpts(name=para['YAxis']), xaxis_opts=opts.AxisOpts(name=para['XAxis']), ) #chart.set_global_opts( visualmap_opts=opts.VisualMapOpts(orient="horizontal")) chart.set_series_opts( label_opts=opts.LabelOpts(is_show=True, position=para['positon'], font_size=para["font_size"], font_family=para["font_family"]), linestyle_opts=opts.LineStyleOpts(width=para['line_width'], opacity=para['opacity'], curve=para['curve'])) if not para['TimeLineOn']: chart.render(path_plotly)
def pie_plot(data): _p_data = data.copy() pie = (Pie(init_opts=opts.InitOpts(width=fig_width, height=fig_height)).add( series_name="占比", data_pair=[list(z) for z in zip(_p_data.index, (_p_data.values).round(2).tolist())], radius=["30%", "50%"], # center=["30%","35%"], label_opts=opts.LabelOpts(is_show=False), ).set_global_opts(legend_opts=opts.LegendOpts(is_show=True,pos_left="2%", orient="vertical",pos_top="10%"), title_opts=opts.TitleOpts(title="",subtitle="数据来源:WIND"), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) ) \ .set_series_opts( tooltip_opts=opts.TooltipOpts( trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)" ), label_opts=opts.LabelOpts(formatter= "{b}: {c}({d}%)") )) return pie
def __init__(self, width="1200px", height="800px"): self.graph = Graph(init_opts=opts.InitOpts( width=width, height=height, bg_color='rgba(255,255,255)')).set_global_opts( # 设置显示工具栏 toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( name='networks', # 设置工具栏图片保存的文件名 ), ))) self.line_width = 1
def draw_river_picture_right_legend(series_name, data, to_file, svg_name, pos_right, width='800px'): """ 主题河流图 :param width: :param pos_right: :param series_name: [str] :param data: [[date, value, series_name]] :param svg_name: svg文件 :param to_file: 结果文件 :return: """ bar = ThemeRiver(init_opts=opts.InitOpts(width="800px", height="600px", bg_color='white')) \ .add( series_name=series_name, data=data, singleaxis_opts=opts.SingleAxisOpts( pos_top="50", pos_bottom="50", type_="time", name_textstyle_opts=opts.TextStyleOpts(font_size=20), axislabel_opts=opts.LabelOpts(font_size=20) ), label_opts=opts.LabelOpts(is_show=False), ) \ .set_global_opts( tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"), toolbox_opts=opts.ToolboxOpts( feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), ) ), legend_opts=opts.LegendOpts(pos_right=pos_right, pos_top="2%", orient="vertical", item_width=30, item_height=14, textstyle_opts=opts.TextStyleOpts(font_size=18) ), ) grid = (Grid(init_opts=opts.InitOpts( width=width, height="600px", bg_color='rgb(255, 255, 255)')).add( bar, grid_opts=opts.GridOpts(pos_right='10%'), is_control_axis_index=True)) make_snapshot(snapshot, grid.render(to_file), svg_name) # 生成svg图片
def heat_plot(data): n, m = data.shape x, y = np.arange(0, n, 1), np.arange(0, m, 1) x, y = np.meshgrid(x, y, indexing='ij') z = data.values.flatten() x = m - 1 - x _data = list(zip(y.flatten(), x.flatten(), z)) _data = [[int(d[0]), int(d[1]), np.round(d[2], 4)] for d in _data] h = ( HeatMap( init_opts=opts.InitOpts(width="1000px", height="300px")).add_xaxis( xaxis_data=data.columns.astype(str).tolist()).add_yaxis( series_name="", yaxis_data=data.index.astype(str).tolist()[::-1], value=_data, label_opts=opts.LabelOpts(position='inside', is_show=True, font_size=15), ).set_series_opts().set_global_opts( # toolbox_opts=opts.ToolboxOpts(is_show=True), legend_opts=opts.LegendOpts(is_show=False), tooltip_opts=opts.TooltipOpts(is_show=True, ), xaxis_opts=opts.AxisOpts( type_="category", splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), ), yaxis_opts=opts.AxisOpts( type_="category", splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), ), visualmap_opts=opts.VisualMapOpts(min_=z.min(), max_=z.max(), is_calculable=True, orient="vertical", pos_left="left"), toolbox_opts=opts.ToolboxOpts( is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=2, name="pic", ), )), )) return h
def chart(f): x = [time_str(i["Now"]) for i in f] y = [i["Price"] for i in f] l = ( Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width="1366px")) .add_xaxis(x) .add_yaxis("v", y, is_connect_nones=True) .set_global_opts( title_opts=opts.TitleOpts(title="MC"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts()), visualmap_opts=opts.VisualMapOpts(min_=500, max_=1200), ) ) return l.render_embed()
def pyechart_Pie_plot(self, filedata, para): from pyecharts.charts import Pie file_name = '南丁格尔玫瑰图.html' path_plotly = self.path_dir_plotly_html + os.sep + file_name # 文件路径,前面是文件夹后面是文件名 costumeTheme = self.themedict[para['theme']] # ----------------------------------------------------------------------- # 准备数据 df = pd.read_excel(filedata, sheet_name='sheet1') # # 提取数据 v = df['provinces'].values.tolist() d = df['num'].values.tolist() color_series = df['color_series'].values.tolist() # 降序排序 df.sort_values(by='num', ascending=False, inplace=True) # 实例化Pie类 pie1 = Pie(init_opts=opts.InitOpts( theme=costumeTheme, width=para['width'], height=para['height'])) # 设置颜色 pie1.set_colors(color_series) # 添加数据,设置饼图的半径,是否展示成南丁格尔图 pie1.add("", [list(z) for z in zip(v, d)], radius=["10%", "135%"], center=["50%", "65%"], rosetype="area") # 设置全局配置项 pie1.set_global_opts( title_opts=opts.TitleOpts(title=para["title"], subtitle=para["subtitle"]), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color="white"))) #visualmap_opts=opts.VisualMapOpts(), ) # 设置系列配置项 pie1.set_series_opts( label_opts=opts.LabelOpts( is_show=True, position="inside", font_size=12, formatter="{b}:{c}天", font_style="normal", # css的格式 font_weight="normal", font_family="宋体"), ) # 生成html文档 pie1.render(path_plotly) return path_plotly # 返回该HTML文件路径
def scatter3d_base(f) -> Scatter3D: data = [ [time_m_d(i.now), time_h_m(i.now), i.price] for i in f ] name = ["d", "h", "v"] c = ( Scatter3D(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width="1366px")) .add("", data, ) .set_global_opts( visualmap_opts=opts.VisualMapOpts(min_=300, max_=1200), title_opts=opts.TitleOpts(title="MC"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts()), ) ) return c.render_embed()
def draw_river_picture(series_name, data, to_file, svg_name): """ 主题河流图 :param series_name: [str] :param data: [[date, value, series_name]] :param svg_name: svg文件 :param to_file: 结果文件 :return: """ bar = ThemeRiver(init_opts=opts.InitOpts(width="800px", height="600px", bg_color='white')) \ .add( series_name=series_name, data=data, singleaxis_opts=opts.SingleAxisOpts( pos_top="2%", pos_bottom="10%", pos_right="20%", type_="time", name_textstyle_opts=opts.TextStyleOpts(font_size=25), axislabel_opts=opts.LabelOpts(font_size=25) ), label_opts=opts.LabelOpts(is_show=False, position='bottom', distance='200px'), ) \ .set_global_opts( tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"), toolbox_opts=opts.ToolboxOpts( feature=opts.ToolBoxFeatureOpts( # data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), ) ), legend_opts=opts.LegendOpts(type_="scroll", pos_left="82%", pos_top="18%", orient="vertical", backgroundColor='rgb(255, 255, 255)', item_width=40, item_height=20, textstyle_opts=opts.TextStyleOpts(font_size=25) ) ) make_snapshot(snapshot, bar.render(to_file), svg_name) # 生成svg图片
def to_treemap(data, proj, version): complexity = {'name': f'{proj}-{version}', 'children': []} extract(complexity, 'complexity', data) maintainability = {'name': f'{proj}-{version}', 'children': []} extract(maintainability, 'maintainability', data) testability = {'name': f'{proj}-{version}', 'children': []} extract(testability, 'testability', data) readability = {'name': f'{proj}-{version}', 'children': []} extract(readability, 'readability', data) reusability = {'name': f'{proj}-{version}', 'children': []} extract(reusability, 'reusability', data) inheritance = {'name': f'{proj}-{version}', 'children': []} extract(inheritance, 'inheritance', data) treemap = TreeMap() for indicator in [ 'complexity', 'maintainability', 'testability', 'readability', 'reusability', 'inheritance' ]: treemap = treemap.add(series_name=indicator, data=[locals()[indicator]], leaf_depth=2, roam=False, label_opts=opts.LabelOpts(position='inside')) treemap = (treemap.set_global_opts( tooltip_opts=opts.TooltipOpts(formatter='{b}<br/>{a}: {c}'), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( magic_type=opts.ToolBoxFeatureMagicTypeOpts(is_show=False), data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), brush=opts.ToolBoxFeatureBrushOpts(type_='clear'))), legend_opts=opts.LegendOpts(is_show=True, selected_mode='single', pos_top='7%', orient='horizontal', padding=0), title_opts=opts.TitleOpts( title=f'Code Quality Treemap of {proj}-{version}', pos_left='center'))) grid = Grid() grid.add(treemap, grid_opts=opts.GridOpts(pos_top='100%')) return grid
def to_linechart(data, proj): versions = [version['version_name'] for version in data] linechart = Line().add_xaxis(xaxis_data=versions) for indicator in [ 'complexity', 'maintainability', 'testability', 'readability', 'reusability', 'inheritance' ]: linechart = linechart.add_yaxis( series_name=indicator, y_axis=[round(version[indicator], 2) for version in data], is_smooth=True, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ) linechart = (linechart.set_global_opts( tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), xaxis_opts=opts.AxisOpts(boundary_gap=False), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(formatter="{value}"), splitline_opts=opts.SplitLineOpts(is_show=True), ), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( magic_type=opts.ToolBoxFeatureMagicTypeOpts(is_show=False), data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), brush=opts.ToolBoxFeatureBrushOpts(type_='clear'))), legend_opts=opts.LegendOpts(is_show=True, pos_top='middle', pos_left='1%', orient='vertical', padding=0), datazoom_opts=opts.DataZoomOpts(type_='slider', range_start=0, range_end=100), title_opts=opts.TitleOpts(title=f'Code Quality Linechart of {proj}', pos_left='center'))) grid = Grid() grid.add(linechart, grid_opts=opts.GridOpts(pos_left='150')) return grid
def dayloss_line_base(self, x, y, t, st): c = (Line( init_opts=opts.InitOpts(width="1000px", height="500px") ).add_xaxis(x).add_yaxis("线损率(%)", y, is_smooth=True).set_global_opts( tooltip_opts=opts.TooltipOpts(is_show=True, trigger="axis", axis_pointer_type="cross"), title_opts=opts.TitleOpts(title=t, subtitle=st), xaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(rotate=-90), max_interval=366, type_="category", axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"), ), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(formatter="{value} %"), axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), ), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( pixel_ratio=2, background_color='white'), restore=opts.ToolBoxFeatureRestoreOpts(is_show=False), data_view=opts.ToolBoxFeatureDataViewOpts(is_show=False), data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), magic_type=opts.ToolBoxFeatureMagicTypeOpts(is_show=False), brush=opts.ToolBoxFeatureBrushOpts(type_='clear'), ))).set_series_opts( label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts( data=[ opts.MarkLineItem(y=6), ], label_opts=opts.LabelOpts(is_show=False), ), )) return c
def word_clouds(weights): w = WordCloud(init_opts=opts.InitOpts(width=fig_width, height=fig_height)).add(series_name="热点分析", data_pair=weights, word_size_range=[6, 66], shape="circle", )\ .set_global_opts( title_opts=opts.TitleOpts( title="", title_textstyle_opts=opts.TextStyleOpts(font_size=23),subtitle="数据来源:WIND" ), tooltip_opts=opts.TooltipOpts(is_show=True), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) ) return w
def __init__(self, title: str, output: str): self.title = title self.output = output self.datas = [] self.chart = (Line(init_opts=opts.InitOpts( page_title=self.title, theme=ThemeType.DARK, width="1280px", height="720px", )).set_global_opts( title_opts=opts.TitleOpts(title=self.title), xaxis_opts=opts.AxisOpts(type_="value", is_scale=True, split_number=10, name="bitrate/kbps"), yaxis_opts=opts.AxisOpts(type_="value", is_scale=True, name="vmaf"), toolbox_opts=opts.ToolboxOpts( is_show=True, orient="vertical", pos_left="right", feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( title="save as image", is_show=True), restore=opts.ToolBoxFeatureRestoreOpts(is_show=False), data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), data_view=opts.ToolBoxFeatureDataViewOpts( is_show=False, is_read_only=True, title="data"), magic_type=opts.ToolBoxFeatureMagicTypeOpts(is_show=False), brush=opts.ToolBoxFeatureBrushOpts(type_=[]))), legend_opts=opts.LegendOpts(pos_top="bottom"), tooltip_opts=opts.TooltipOpts( is_show=True, formatter=pyecharts_utils.JsCode( "function(x) {return x.seriesName + '<br/>bitrate '+ x.data[0] + ' kbps<br/>vmaf ' + x.data[1];}" ))))
def draw_line_sentiment(lists): print(lists) c=Line(init_opts=opts.InitOpts(width='2000px',height='600px')) c.add_xaxis(xaxis_data=list(range(1,len(lists)+1))) # c.add_xaxis(xaxis_data=content) c.add_yaxis(series_name="",y_axis=lists) data_zoom = { "show": True, "title": {"zoom": "data zoom", "back": "data zoom restore"} } c.set_global_opts( # 设置标题 title_opts=opts.TitleOpts(title="情感分析得分走势图"), # 设置图例is_show=False是不显示图例 legend_opts=opts.LegendOpts(is_show=True), # 设置提示项 tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'), # 工具箱的设置 toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts(data_zoom=data_zoom)) ) c.set_series_opts() c.render('line_sentiments.html')
def issuer_industry_plot(data): name = data.columns[0] data = data.sort_values(by=name) x_data = data.index value = np.round(data[name],2).tolist() bar = ( Bar() .add_xaxis( xaxis_data=x_data.tolist(), ) .add_yaxis( series_name="", yaxis_data=value, yaxis_index=0, color="#36648B", ) .set_global_opts( yaxis_opts=opts.AxisOpts( type_="value", name=name, position="left", # offset=80, axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts() ), axislabel_opts=opts.LabelOpts(formatter="{value}"), ), xaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(rotate=-90), type_="category", name="行业", ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), title_opts=opts.TitleOpts(subtitle="数据来源:WIND"), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) ) ) pie = (Pie(init_opts=opts.InitOpts()).add( series_name="", data_pair=[list(z) for z in zip(x_data, (data['占比']*100).round(2).tolist())], radius=["25%", "35%"], center=["30%","35%"], label_opts=opts.LabelOpts(is_show=False), ).set_global_opts(legend_opts=opts.LegendOpts(is_show=False,pos_left="legft", orient="vertical")) \ .set_series_opts( tooltip_opts=opts.TooltipOpts( trigger="item", formatter="{b}: {c}%" ), label_opts=opts.LabelOpts(formatter="{b}: {c}%"), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) )) grid_chart = ( Grid(init_opts=opts.InitOpts(width=fig_width, height=fig_height)) .add( bar, grid_opts=opts.GridOpts( pos_left="10%", pos_top="10%" ), ) .add(pie, grid_opts=opts.GridOpts()) ) return grid_chart
def rate_histogram_plot(data,bins = 10): m, bins = np.histogram(data.dropna(), bins) m = m.astype(float) s = m.sum() x = bins.round(2).astype(str).tolist() + [""] bar = ( Bar(init_opts=opts.InitOpts(width=fig_width, height=fig_height)) .add_xaxis(xaxis_data=bins.round(2).tolist(), ) .extend_axis(xaxis_data=x, xaxis=opts.AxisOpts( position="bottom", is_show=True, # , is_scale = True, boundary_gap = False, # type_="value", axistick_opts=opts.AxisTickOpts(is_align_with_label=True), axisline_opts=opts.AxisLineOpts( on_zero_axis_index = 0, is_on_zero=False, linestyle_opts=opts.LineStyleOpts() ), axispointer_opts=opts.AxisPointerOpts( is_show=False, label=opts.LabelOpts() ), ), ) .add_yaxis( xaxis_index =0, series_name="", yaxis_data=m.astype(float).tolist(), # yaxis_index=0, # color=colors[1], category_gap=0, gap =5, itemstyle_opts=opts.ItemStyleOpts(border_color=None), label_opts=opts.LabelOpts( formatter=JsCode( """function(params) { return params.data + '(' + ((Math.round(params.data / %s *10000)/100)) + '%s)' ; }"""%(s,"%") ) ), color="#36648B", ) .set_global_opts( title_opts=opts.TitleOpts(subtitle="数据来源:WIND"), yaxis_opts=opts.AxisOpts( type_="value", name="频数", position="left", # offset=80, axisline_opts=opts.AxisLineOpts( is_on_zero=True,linestyle_opts=opts.LineStyleOpts() ), axislabel_opts=opts.LabelOpts( # # if ('value' in params.data) { # } # formatter="{value}" formatter = JsCode( """function(params) { return (params); }""" ) ), ), xaxis_opts=opts.AxisOpts( is_show=False ), tooltip_opts=opts.TooltipOpts(is_show=False), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) )) return bar
def province_analysis_plot(data): def format_data(data): data['占比'] = (data['占比'] * 100).round(4) _ = data.reset_index().values data = list(zip([d[0] for d in _], _.tolist())) data = [list(d) for d in data] return data data = format_data(data) min_data, max_data = ( min([d[1][1] for d in data]), max([d[1][1] for d in data]), ) map_chart = ( Map(init_opts=opts.InitOpts( width=fig_width, height=fig_height, page_title="中国各省疫情防控债发行情况")) .add( series_name="", data_pair=data, # label_opts=opts.LabelOpts(is_show=False, # # formatter="{b}:{@占比}" # ), is_map_symbol_show=False, ).set_series_opts( label_opts=opts.LabelOpts(is_show=True, formatter=JsCode(""" function(params){ if (typeof(params.data) == 'undefined') { return params.value[2]; } else { return params.data.name + params.data.value[2] + '%' ; } }""" )) ) .set_global_opts( title_opts=opts.TitleOpts( # title="中国各省疫情防控债发行情况", subtitle="数据来源:WIND", pos_left="center", pos_top="top", title_textstyle_opts=opts.TextStyleOpts( font_size=25, color="rgba(255,255,255, 0.9)" ), ), tooltip_opts=opts.TooltipOpts( is_show=True, formatter=JsCode( """function(params) { if ('value' in params.data) { return params.data.value[0] + '<br/>占比:' + params.data.value[2]+'%<br/>数量:'+params.data.value[1]; } }""" ), ), visualmap_opts=opts.VisualMapOpts( is_calculable=True, dimension=1, pos_left="10", pos_top="center", range_text=["High", "Low"], range_color=["lightskyblue", "yellow", "orangered"], textstyle_opts=opts.TextStyleOpts(color="#ddd"), min_=min_data, max_=max_data, ), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) ) ) return map_chart
feature=opts.ToolBoxFeatureOpts( # 保存工具 opts.ToolBoxFeatureSaveAsImageOpts( # 是否显示 is_show=True, # 提示语 title="保存为图片", ), # 还原工具 opts.ToolBoxFeatureRestoreOpts( # 是否显示该工具 is_show=True, # 提示语 title="还原", ), # 数据视图工具 opts.ToolBoxFeatureDataViewOpts( # 是否显示该工具 is_show=True, # 提示语 title="数据视图", # 是否不可编辑 is_read_only=False, ), # 缩放工具配置项,直角坐标系适用 opts.ToolBoxFeatureDataZoomOpts( # 是否显示该工具。 is_show=False, # 提示语 zoom_title="区域缩放", # 提示语 back_title="区域缩放还原", ), # 图表类型切换,适用于直角坐标系 opts.ToolBoxFeatureMagicTypeOpts( # 是否显示该工具 is_show=False, # 启用的动态类型 type_=['stack', 'line', 'bar', 'tiled'], ), # 工具箱选框组件配置项 opts.ToolBoxFeatureBrushOpts( # 选择显示哪些选框 type_=[]), )),
def bar(): cursor = connection.cursor() cursor.execute("""SELECT DATE_FORMAT( rev.create_time, '%Y-%m-%d' ) create_time, revt.`name`, count( * ) FROM resource_event rev LEFT JOIN resource_eventtype revt ON rev.event_type_id = revt.id GROUP BY create_time, event_type_id""") rows = cursor.fetchall() name_list = [] event_type_list = [] for row in rows: name = row[0] if name not in name_list: name_list.append(name) event_type = row[1] if event_type not in event_type_list: event_type_list.append(event_type) bar = Bar(init_opts=opts.InitOpts(height="350px")) bar.add_xaxis(name_list) for event_type in event_type_list: tmp_list = [0 for i in range(len(name_list))] for row in rows: if event_type == row[1]: index = name_list.index(row[0]) tmp_list[index] = row[2] bar.add_yaxis(event_type, tmp_list) bar.set_global_opts(title_opts=opts.TitleOpts(title="违章记录统计"), toolbox_opts=opts.ToolboxOpts(is_show=False, feature=opts.ToolBoxFeatureOpts( restore=None, data_zoom=None))) return bar.render_embed()
def plot_ground_truth_analysis( all_score_columns: typing.List[str], title: str, output_path: str, df: pd.DataFrame, add_wrong_candidates: bool = False, wrong_candidates_score_column: str = None, ) -> None: """ use pyechart to plot html interactive figure """ df_processed = copy.deepcopy(df) for each_col in df_processed.columns: df_processed[each_col] = pd.to_numeric(df_processed[each_col], errors='ignore') xaxis_labels = [] memo = defaultdict(list) groupby_res = df_processed[df_processed["evaluation_label"] == 1].groupby(["column", "row"]) for key, each_group in reversed(tuple(groupby_res)): if add_wrong_candidates: df_wrong_examples = df_processed[(df_processed["column"] == key[0]) & (df_processed["row"] == key[1]) & (df_processed["evaluation_label"] == -1)] \ .sort_values(by=[wrong_candidates_score_column], ascending=False).iloc[:3, :] # add wrong candidate information for _, each_row in df_wrong_examples.iterrows(): longest_string = max(each_row["kg_labels"].split("|"), key=len) xaxis_labels.append(each_row["label_clean"] + " \n({})".format(longest_string)) for each_score_column in all_score_columns: memo[each_score_column].append("{:.2f}".format( each_row[each_score_column])) # add ground truth information xaxis_labels.append(each_group["label_clean"].iloc[0]) for each_score_column in all_score_columns: memo[each_score_column].append("{:.2f}".format( each_group[each_score_column].iloc[0])) # build figure bar = Bar() bar.add_xaxis(xaxis_labels) for k, v in memo.items(): bar.add_yaxis(k, v) # set the global options bar.set_global_opts( title_opts=opts.TitleOpts(title=title, pos_left='40%'), legend_opts=opts.LegendOpts(pos_left="center", pos_top="bottom", orient='horizontal'), brush_opts=opts.BrushOpts(), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( title="save as image"), magic_type=opts.ToolBoxFeatureMagicTypeOpts( line_title="switch to line chart", bar_title="switch to bar chart", stack_title="switch to stacked values", tiled_title="switch to tiled values"), data_zoom=opts.ToolBoxFeatureDataZoomOpts( zoom_title="zoom in", back_title="zoom reset"), restore=opts.ToolBoxFeatureRestoreOpts(title="reset"), data_view=opts.ToolBoxFeatureDataViewOpts( title="Data table view", lang=["Table view", "Close", "Refresh"], ), brush=opts.ToolBoxFeatureBrushOpts( rect_title="rectangle choice", polygon_title="polygon choice", clear_title="clear choices", keep_title="keep choices"))), datazoom_opts=opts.DataZoomOpts(orient="vertical"), # yaxis_opts=opts.AxisOpts(name='labels', name_gap=5000, name_rotate=15), tooltip_opts=opts.TooltipOpts(is_show=True, trigger="axis", axis_pointer_type="shadow"), xaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts( is_inside=True, length=850, linestyle_opts=opts.LineStyleOpts(type_="dotted", opacity=0.5)))) # do not shown bar label values bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) bar.reversal_axis() grid = (Grid(init_opts=opts.InitOpts( width="1500px", height="1000px", page_title="Table Linker visualization page")).add( bar, grid_opts=opts.GridOpts(pos_top='5%', pos_bottom="10%", pos_right='5%', pos_left="20%"))) grid.render(output_path + ".html")
def draw_heat_map_picture(grid_data, x_list, max_number, to_file, svg_name, height='1200px'): """ grid堆叠热力图 :param height: :param x_list: 横轴列表 :param max_number: 最大值 :param grid_data: [{}] key包括 series_name, y_name, sub_title, value :param to_file: :param svg_name: :return: """ grid_number = len(grid_data) grid = (Grid(init_opts=opts.InitOpts( width="900px", height=height, bg_color='rgb(255, 255, 255)'))) i = 0 all_top = 5 for grid_data_one in grid_data: # max_number_list = [i[2] for i in grid_data_one['value'][3:]] # max_number = max(max_number_list) length = int(len(grid_data_one['y_name'])) * 1.5 top_position = all_top bottom_position = 100 - top_position - length all_top = top_position + length + 2 # print(top_position) # print(bottom_position) # print(all_top) c = HeatMap() \ .add_xaxis(x_list) \ .add_yaxis(grid_data_one['series_name'], grid_data_one['y_name'], grid_data_one['value'], label_opts=opts.LabelOpts(is_show=True, position="inside"), ) if i == grid_number - 1: c = c.set_global_opts( title_opts=opts.TitleOpts( title="", subtitle=grid_data_one['sub_title'], pos_top=str(top_position) + "%", ), visualmap_opts=opts.VisualMapOpts( is_show=False, min_=0, max_=max_number, range_color=['#fff799', '#ae4130']), xaxis_opts=opts.AxisOpts( is_show=True, axislabel_opts=opts.LabelOpts(interval=0, rotate=30), splitarea_opts=opts.SplitAreaOpts( is_show=False, areastyle_opts=opts.AreaStyleOpts(opacity=0))), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(font_size=14, interval=0), splitarea_opts=opts.SplitAreaOpts( is_show=False, areastyle_opts=opts.AreaStyleOpts(opacity=0))), legend_opts=opts.LegendOpts(pos_top="120%"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), )), ) else: c = c.set_global_opts( title_opts=opts.TitleOpts( title="", subtitle=grid_data_one['sub_title'], pos_top=str(top_position - 2) + "%", ), visualmap_opts=opts.VisualMapOpts( is_show=False, min_=0, max_=max_number, range_color=['#fff799', '#ae4130']), xaxis_opts=opts.AxisOpts( is_show=False, splitarea_opts=opts.SplitAreaOpts( is_show=False, areastyle_opts=opts.AreaStyleOpts(opacity=0))), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(font_size=14, interval=0), splitarea_opts=opts.SplitAreaOpts( is_show=False, areastyle_opts=opts.AreaStyleOpts(opacity=0))), legend_opts=opts.LegendOpts(pos_top="120%"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), )), ) # 加到grid grid = grid.add(c, grid_opts=opts.GridOpts( pos_bottom=str(bottom_position) + "%", pos_top=str(top_position) + "%", pos_left="25%", pos_right="10%")) i = i + 1 make_snapshot(snapshot, grid.render(to_file), svg_name) # 生成svg图片
def draw_bar_reverse_picture(xaxis_data, data, to_file, unit, svg_name, stack, y_name, pos_left='25%'): """ 多个y列,是否堆叠要确认下 :param pos_left: "25%" :param is_reverse: xy轴是否反转 :param y_name: y轴名称 :param stack: boolean, 是否要堆叠 :param xaxis_data: x轴 :param data: {series_name, data} :param svg_name: svg文件 :param to_file: 结果文件 :param unit: 横轴的数值的单位(加在每个横轴值后面,比如 月) :return: """ xaxis_data = [str(i) + unit for i in xaxis_data] bar = Bar(init_opts=opts.InitOpts( width="800px", height="600px", bg_color='white')).add_xaxis( # 1250px xaxis_data=xaxis_data, # add_xaxis=opts.LabelOpts(formatter="{value}" + unit), ) for series_name, y_axis in data.items(): if stack: bar = bar.add_yaxis( series_name=series_name, stack='1', # color="#d14a61", y_axis=y_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), ) else: bar = bar.add_yaxis( series_name=series_name, color="#61a0a8", category_gap='50%', y_axis=y_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=True, position='right'), ) bar = bar.set_global_opts( yaxis_opts=opts.AxisOpts( type_="category", # min_=0, # max_=250, position="left", offset=0, axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts()), axislabel_opts=opts.LabelOpts(formatter="{value}", font_size=20), ), xaxis_opts=opts.AxisOpts( name=y_name, name_textstyle_opts=opts.TextStyleOpts(font_size=20), axislabel_opts=opts.LabelOpts(formatter="{value}", font_size=20), ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), )), legend_opts=opts.LegendOpts(is_show=False), ) bar = bar.reversal_axis() grid = ( Grid(init_opts=opts.InitOpts(width="800px", height="600px", bg_color='rgb(255, 255, 255)')) # 800px .add(bar, grid_opts=opts.GridOpts(pos_left=pos_left), is_control_axis_index=True)) make_snapshot(snapshot, grid.render(to_file), svg_name) # 生成svg图片
def draw_line_with_two_y(xaxis_data, y1_series_name, y1_axis, y2_series_name, y2_axis, y3_series_name, y3_axis, y1_name, y2_name, to_file, unit, svg_name): """ 左边是 两条线; 右边是 一条线。 :param svg_name: svg文件 :param xaxis_data: 横轴 :param y1_series_name: 左边第一条线的系列名 :param y1_axis: 左边第一条线的数值 :param y2_series_name: 左边第二条线的系列名 :param y2_axis: 左边第二条线的数值 :param y3_series_name: 右边第一条线的系列名 :param y3_axis: 右边第一条线的数值 :param y1_name: 左边的纵轴名称 :param y2_name: 右边的纵轴名称 :param to_file: 结果文件 :param unit: 横轴的数值的单位(加在每个横轴值后面,比如 月) :return: """ xaxis_data = [str(i) + unit for i in xaxis_data] bar = Line(init_opts=opts.InitOpts( width="800px", height="600px", bg_color='white' )).add_xaxis( xaxis_data=xaxis_data, # add_xaxis=opts.LabelOpts(formatter="{value}" + unit), ).add_yaxis( series_name=y1_series_name, is_smooth=True, symbol="circle", symbol_size=8, is_symbol_show=True, # color="#d14a61", y_axis=y1_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ).add_yaxis( series_name=y2_series_name, is_smooth=True, symbol="circle", symbol_size=8, is_symbol_show=True, # color="#d14a61", y_axis=y2_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ).add_yaxis( series_name=y3_series_name, is_smooth=True, symbol="circle", symbol_size=8, is_symbol_show=True, # color="#d14a61", y_axis=y3_axis, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ).extend_axis(yaxis=opts.AxisOpts( type_="value", name=y2_name, # min_=0, # max_=25, position="right", axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts()), axislabel_opts=opts.LabelOpts(formatter="{value}", font_size=15), splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)), name_textstyle_opts=opts.TextStyleOpts(font_size=15))).set_global_opts( yaxis_opts=opts.AxisOpts( type_="value", name=y1_name, # min_=0, # max_=250, position="left", offset=0, axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts()), axislabel_opts=opts.LabelOpts(formatter="{value}", font_size=15), name_textstyle_opts=opts.TextStyleOpts(font_size=15)), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts( formatter="{value}", font_size=15), ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), )), legend_opts=opts.LegendOpts( item_width=25, item_height=10, textstyle_opts=opts.TextStyleOpts(font_size=15)), ) make_snapshot(snapshot, bar.render(to_file), svg_name) # 生成svg图片
def draw_line_picture(xaxis_data, data, to_file, unit, svg_name, stack, y_name): """ 多条线,是否堆叠要确认下 :param label_right: :param y_name: y轴名称 :param stack: boolean, 是否要堆叠 :param xaxis_data: x轴 :param data: {series_name, data} :param svg_name: svg文件 :param to_file: 结果文件 :param unit: 横轴的数值的单位(加在每个横轴值后面,比如 月) :return: """ xaxis_data = [str(i) + unit for i in xaxis_data] bar = Line(init_opts=opts.InitOpts( width="800px", height="600px", bg_color='white')).add_xaxis( xaxis_data=xaxis_data, # add_xaxis=opts.LabelOpts(formatter="{value}" + unit), ) for series_name, y_axis in data.items(): if stack: bar = bar.add_yaxis( series_name=series_name, is_smooth=True, symbol="circle", # symbol_size=8, stack='1', is_symbol_show=False, # color="#d14a61", y_axis=y_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), # linestyle_opts=opts.LineStyleOpts(width=2), areastyle_opts=opts.AreaStyleOpts(opacity=1)) else: bar = bar.add_yaxis( series_name=series_name, is_smooth=True, symbol="circle", symbol_size=8, is_symbol_show=True, # color="#d14a61", y_axis=y_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ) bar = bar.set_global_opts( yaxis_opts=opts.AxisOpts( type_="value", name=y_name, name_textstyle_opts=opts.TextStyleOpts(font_size=25), # min_=0, # max_=250, position="left", offset=0, axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts()), axislabel_opts=opts.LabelOpts(formatter="{value}", font_size=25), ), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts( formatter="{value}", font_size=25, interval=0), ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), )), legend_opts=opts.LegendOpts( pos_left="12%", pos_top="10%", orient="vertical", # backgroundColor='rgb(255, 255, 255)', item_width=40, item_height=20, textstyle_opts=opts.TextStyleOpts(font_size=25)), ) make_snapshot(snapshot, bar.render(to_file), svg_name) # 生成svg图片
def daily_issue_plot(daily,cummulative): import pyecharts.options as opts from pyecharts.charts import Bar, Line # color = ["#36648B","tomato"] daily = daily.round(2) cummulative = cummulative.round(2) x_data = daily.index.astype(str).tolist() bar = ( Bar(init_opts=opts.InitOpts(width=fig_width, height=fig_height)) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="每日发行数量", yaxis_index=2, yaxis_data=daily['发行数量'].tolist(), label_opts=opts.LabelOpts(is_show=False), ).add_yaxis( series_name="每日发行总额", yaxis_index=3, yaxis_data=daily['发行总额'].tolist(), label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts( tooltip_opts=opts.TooltipOpts( is_show=True, trigger="axis", axis_pointer_type="cross" ), xaxis_opts=opts.AxisOpts( type_="category", axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"), ), yaxis_opts=opts.AxisOpts( name="数量", type_="value", # min_=0, # max_=250, # interval=50, axislabel_opts=opts.LabelOpts(formatter="{value}"), axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), ), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ) ) ) line = ( Line(init_opts=opts.InitOpts(width=fig_width, height=fig_height)) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="累计发行数量", yaxis_index=0, y_axis=cummulative['累计发行数量'].tolist(), label_opts=opts.LabelOpts(is_show=False), is_smooth=True, is_symbol_show=True, symbol="circle", symbol_size=6, linestyle_opts=opts.LineStyleOpts(width=2), ).add_yaxis( series_name="累计发行总额", yaxis_index=1, is_smooth=True, is_symbol_show=True, symbol="circle", symbol_size=6, linestyle_opts=opts.LineStyleOpts(width=2), y_axis=cummulative['累计发行总额'].tolist(), label_opts=opts.LegendOpts(is_show=False), ) .extend_axis(yaxis=opts.AxisOpts( name= "总额", type_="value", position='right', offset=60, axislabel_opts=opts.LabelOpts(formatter="{value}亿",position="right"), ),).extend_axis( yaxis=opts.AxisOpts( name="数量", type_="value", position="left", offset=0, axislabel_opts=opts.LabelOpts(formatter="{value}"), ) ) .extend_axis( yaxis=opts.AxisOpts( name="总额", type_="value", position="left", offset=60, axislabel_opts=opts.LabelOpts(formatter="{value}亿"), ) ) .set_global_opts( tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), xaxis_opts=opts.AxisOpts( splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color="#E8E8E8") ), ), yaxis_opts=opts.AxisOpts( name = "数量", type_="value", position="right", offset=0, axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts(width=1) ), splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color='#E8E8E8') ), ), toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts( save_as_image=opts.ToolBoxFeatureSaveAsImageOpts( background_color='white', connected_background_color="white", pixel_ratio=pixel_ratio, name="pic", ), ) ), ) ) line.overlap(bar) return line
def draw_bar_picture(xaxis_data, data, to_file, unit, svg_name, stack, y_name): """ 多个y列,是否堆叠要确认下 :param is_reverse: xy轴是否反转 :param y_name: y轴名称 :param stack: boolean, 是否要堆叠 :param xaxis_data: x轴 :param data: {series_name, data} :param svg_name: svg文件 :param to_file: 结果文件 :param unit: 横轴的数值的单位(加在每个横轴值后面,比如 月) :return: """ xaxis_data = [str(i) + unit for i in xaxis_data] bar = Bar(init_opts=opts.InitOpts( width="900px", height="600px", bg_color='white')).add_xaxis( xaxis_data=xaxis_data, # add_xaxis=opts.LabelOpts(formatter="{value}" + unit), ) for series_name, y_axis in data.items(): if stack: bar = bar.add_yaxis( series_name=series_name, stack='1', # color="#d14a61", y_axis=y_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), ) else: bar = bar.add_yaxis( series_name=series_name, # color="#d14a61", y_axis=y_axis, yaxis_index=0, label_opts=opts.LabelOpts(is_show=False), ) bar = bar.set_global_opts( yaxis_opts=opts.AxisOpts( type_="value", name=y_name, name_textstyle_opts=opts.TextStyleOpts(font_size=15), # min_=0, # max_=250, position="left", offset=0, axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts()), axislabel_opts=opts.LabelOpts(formatter="{value}", font_size=15), ), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts( formatter="{value}", font_size=15, interval=0), ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), toolbox_opts=opts.ToolboxOpts(feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), )), legend_opts=opts.LegendOpts( item_width=25, item_height=15, textstyle_opts=opts.TextStyleOpts(font_size=15)), ) make_snapshot(snapshot, bar.render(to_file), svg_name) # 生成svg图片