|
|
|
@ -1,6 +1,11 @@
|
|
|
|
|
from pyecharts.charts import Bar
|
|
|
|
|
from pyecharts.commons.utils import JsCode
|
|
|
|
|
|
|
|
|
|
from pyecharts import options as opts
|
|
|
|
|
import os
|
|
|
|
|
from pyecharts.charts import Bar
|
|
|
|
|
from pyecharts.commons.utils import JsCode
|
|
|
|
|
from pyecharts import options as opts
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
def generate_bar_chart(result, x_columns: list, y_columns: list, output_file: str = "bar_chart.html"):
|
|
|
|
|
"""
|
|
|
|
@ -27,17 +32,16 @@ def generate_bar_chart(result, x_columns: list, y_columns: list, output_file: st
|
|
|
|
|
# 提取 Y 轴数据
|
|
|
|
|
y_axis_data = [row[y_columns[0]] for row in result] # 目前只支持一个 Y 轴列
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 为每个柱状图设置不同颜色
|
|
|
|
|
colors = [
|
|
|
|
|
"#5470C6", "#91CC75", "#EE6666", "#73C0DE", "#3BA272", "#FC8452", "#9A60B4", "#EA7CCC"
|
|
|
|
|
] # 自定义颜色列表
|
|
|
|
|
|
|
|
|
|
# 创建柱状图并配置布局
|
|
|
|
|
bar = Bar()
|
|
|
|
|
bar.add_xaxis(x_axis_data)
|
|
|
|
|
bar.add_yaxis(
|
|
|
|
|
series_name=y_columns,
|
|
|
|
|
series_name=y_columns[0], # 使用 y_columns 的第一个元素作为系列名称
|
|
|
|
|
y_axis=y_axis_data,
|
|
|
|
|
bar_width="30%", # 控制柱宽
|
|
|
|
|
itemstyle_opts=opts.ItemStyleOpts(
|
|
|
|
@ -50,17 +54,15 @@ def generate_bar_chart(result, x_columns: list, y_columns: list, output_file: st
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
bar.set_global_opts(
|
|
|
|
|
title_opts=opts.TitleOpts(title="学段-科目课程数量柱状图"), # 添加标题
|
|
|
|
|
xaxis_opts=opts.AxisOpts(
|
|
|
|
|
name=" - ".join(x_columns),
|
|
|
|
|
axislabel_opts=opts.LabelOpts(rotate=45),
|
|
|
|
|
boundary_gap=True, # 开启分类间隙
|
|
|
|
|
splitline_opts=opts.SplitLineOpts(is_show=False)
|
|
|
|
|
),
|
|
|
|
|
yaxis_opts=opts.AxisOpts(name=y_columns),
|
|
|
|
|
legend_opts=opts.LegendOpts(
|
|
|
|
|
pos_left="10%", # 图例距左侧边界的百分比距离
|
|
|
|
|
orient="vertical" # 垂直排列:ml-citation{ref="1,3" data="citationList"}
|
|
|
|
|
)
|
|
|
|
|
yaxis_opts=opts.AxisOpts(name=y_columns[0]), # 使用 y_columns 的第一个元素作为 Y 轴名称
|
|
|
|
|
legend_opts=opts.LegendOpts(is_show=False) # 隐藏图例
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 确保目标目录存在
|
|
|
|
@ -79,13 +81,11 @@ def generate_bar_chart(result, x_columns: list, y_columns: list, output_file: st
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"生成柱状图时发生错误: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from pyecharts import options as opts
|
|
|
|
|
from pyecharts.charts import Pie
|
|
|
|
|
from pyecharts.commons.utils import JsCode
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_pie_chart(
|
|
|
|
|
_data,
|
|
|
|
|
category_columns: list, # 改为列表接收多列(如["学段","科目"])
|
|
|
|
@ -123,7 +123,7 @@ def generate_pie_chart(
|
|
|
|
|
pie.add(
|
|
|
|
|
series_name="占比",
|
|
|
|
|
data_pair=data_pairs,
|
|
|
|
|
radius=["30%", "55%"], # 环形布局优化:ml-citation{ref="2" data="citationList"}
|
|
|
|
|
radius=["30%", "55%"], # 环形布局优化
|
|
|
|
|
label_opts=opts.LabelOpts(
|
|
|
|
|
formatter="{b}\n{d}%", # 换行显示标签和比例
|
|
|
|
|
position="outside",
|
|
|
|
@ -138,12 +138,7 @@ def generate_pie_chart(
|
|
|
|
|
title=f"{' + '.join(category_columns)}分布",
|
|
|
|
|
subtitle="数据维度:" + " → ".join(category_columns)
|
|
|
|
|
),
|
|
|
|
|
legend_opts=opts.LegendOpts(
|
|
|
|
|
orient="vertical",
|
|
|
|
|
pos_left="15%",
|
|
|
|
|
item_height=20,
|
|
|
|
|
textstyle_opts=opts.TextStyleOpts(font_size=12)
|
|
|
|
|
)
|
|
|
|
|
legend_opts=opts.LegendOpts(is_show=False) # 不显示图例
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 输出文件
|
|
|
|
|