import json from pyecharts import options as opts from pyecharts.charts import Bar, Line from pyecharts.globals import CurrentConfig from Config.Config import ONLINE_HOST CurrentConfig.ONLINE_HOST = ONLINE_HOST class RuYuanZaiYuanModel: @staticmethod def load_student_data(): try: # 加载招生数据(入园人数) with open("./Data/ZhaoShengCount.json", "r", encoding="utf-8") as f: enrollment_data = json.load(f) # 加载在校生数据(在园人数) with open("./Data/ZaiXiaoShengCount.json", "r", encoding="utf-8") as f: in_school_data = json.load(f) return enrollment_data, in_school_data except Exception as e: print(f"读取学生数据出错: {e}") return [], [] @staticmethod def generate_preschool_education_config(): # 获取学前教育相关数据 enrollment_data, in_school_data = RuYuanZaiYuanModel.load_student_data() # 提取云南省级数据 yunnan_enroll = next((item for item in enrollment_data if item["area_name"] == "云南省"), None) yunnan_in_school = next((item for item in in_school_data if item["area_name"] == "云南省"), None) if not yunnan_enroll or not yunnan_in_school: return {} # 提取年份数据(2015-2024) years = [str(year) for year in range(2015, 2025)] # 构建学前教育数据 enroll_total = [] # 入园总数 in_school_total = [] # 在园总数 for year in years: # 入园总数(招生数据) enroll_data = yunnan_enroll["education_data"]["preschool"].get(year, {}) enroll_total.append(enroll_data.get("total", 0)) # 在园总数(在校生数据) in_school_data = yunnan_in_school["student_data"]["preschool"].get(year, {}) in_school_total.append(in_school_data.get("total", 0)) # 构建ECharts配置 option = { "title": { "text": "云南省学前教育人数统计", "left": "center", "textStyle": {"color": "#333"} }, "tooltip": { "trigger": "axis", "axisPointer": {"type": "shadow"} }, "legend": { "data": ["入园总数", "在园总数"], "top": 30, "textStyle": {"color": "#333"} }, "xAxis": { "type": "category", "data": years, "axisLabel": {"color": "#333"}, "axisLine": {"lineStyle": {"color": "#333"}} }, "yAxis": { "type": "value", "name": "人数", "min": 0, "max": 100, "interval": 30, "axisLabel": { "formatter": "{value} 万人", "color": "#333" }, "axisLine": {"lineStyle": {"color": "#333"}}, "splitLine": {"lineStyle": {"color": "rgba(0,0,0,0.1)"}} }, "series": [ { "name": "入园总数", "type": "bar", "data": enroll_total, "itemStyle": { "color": "#5470c6", "borderRadius": [4, 4, 0, 0] }, "barWidth": "40%" }, { "name": "在园总数", "type": "line", "data": in_school_total, "lineStyle": {"color": "#91cc75", "width": 3}, "symbol": "circle", "symbolSize": 8, "itemStyle": {"color": "#91cc75"} } ], "grid": { "left": "3%", "right": "4%", "bottom": "3%", "containLabel": True } } return option