You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from pyecharts.charts import Bar
from pyecharts import options as opts
import pandas as pd
def process_data(raw_data):
"""
数据处理流程:
1. 转换为DataFrame
2. 过滤无效行政区
3. 按上传量降序排序
4. 取前10条记录
"""
df = pd.DataFrame(raw_data)
# 过滤空值包括NaN和空字符串
df = df[(df['行政区划名称'].notna()) & (df['行政区划名称'] != '')]
# 按上传量排序并保留前10
return df.sort_values('上传课程数量', ascending=False).head(10)
def create_horizontal_barchart(data):
# 提取数据
schools = data['学校名称'].tolist()
counts = data['上传课程数量'].tolist()
# 创建图表对象
bar = Bar(init_opts=opts.InitOpts(width='1200px', height='600px'))
# 添加数据(横向柱状图需要反转坐标系)
bar.add_xaxis(counts)
bar.add_yaxis(
series_name="上传数量",
y_axis=schools,
itemstyle_opts=opts.ItemStyleOpts(color="#5470c6"),
label_opts=opts.LabelOpts(
position="right",
formatter="{b}: {c}"
)
)
# 配置图表选项
bar.set_global_opts(
title_opts=opts.TitleOpts(
title="课程资源上传量TOP10学校排名"
#subtitle="数据来源:课程资源管理系统"
),
tooltip_opts=opts.TooltipOpts(
formatter=opts.TooltipItemOptsFormatter(
# 显示学校、行政区和数量
"行政区:{a}<br/>"
"上传量:<b>{c}</b>"
)
),
yaxis_opts=opts.AxisOpts(
name="学校名称",
axislabel_opts=opts.LabelOpts(
font_size=12,
formatter=lambda name: name[:6] + "..." if len(name) > 6 else name
)
),
xaxis_opts=opts.AxisOpts(name="上传数量(件)"),
datazoom_opts=[opts.DataZoomOpts(type_="inside")],
)
# 反转坐标系实现横向显示
# bar.reversal_axis()
return bar