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)] # 构建学前教育数据 urban_enroll = [] # 城区入园人数 town_enroll = [] # 镇区入园人数 rural_enroll = [] # 乡村入园人数 urban_in_school = [] # 城区在园人数 town_in_school = [] # 镇区在园人数 rural_in_school = [] # 乡村在园人数 for year in years: # 入园人数(招生数据) pre_enroll = yunnan_enroll.get("education_data", {}).get("preschool", {}).get(year, {}) urban_enroll.append(pre_enroll.get("urban", 0)) town_enroll.append(pre_enroll.get("town", 0)) rural_enroll.append(pre_enroll.get("rural", 0)) # 在园人数(在校生数据) pre_in_school = yunnan_in_school.get("education_data", {}).get("preschool", {}).get(year, {}) urban_in_school.append(pre_in_school.get("urban", 0)) town_in_school.append(pre_in_school.get("town", 0)) rural_in_school.append(pre_in_school.get("rural", 0)) # 构建option数据结构 option = { "grid": { "left": 0, "right": 0, "top": 40, "bottom": 10, "containLabel": True, }, "textStyle": { "color": "#fff", }, "tooltip": { "trigger": "axis", "axisPointer": { "type": "cross", "crossStyle": { "color": "#999" }, }, "textStyle": { "color": "#fff" }, "backgroundColor": "rgba(96,98,102,0.8)", "borderColor": "rgba(255,255,255,0.3)", "borderWidth": 1, }, "legend": { "data": ["城区入园人数", "镇区入园人数", "乡村入园人数", "城区在园人数", "镇区在园人数", "乡村在园人数"], "top": 0, "textStyle": { "color": "#fff" }, "icon": "roundRect", "itemWidth": 12, "itemHeight": 12, }, "xAxis": [ { "type": "category", "data": years, "axisPointer": { "type": "shadow" }, "axisLine": { "lineStyle": { "color": "#fff" } }, "axisLabel": { "color": "#fff" }, "nameTextStyle": { "color": "#fff" }, }, ], "yAxis": [ { "type": "value", "axisLabel": { "formatter": "{value} 人", "color": "#fff" }, } ], "series": [ { "name": "城区入园人数", "type": "bar", "data": urban_enroll, "itemStyle": { "borderRadius": [6, 6, 0, 0] }, }, { "name": "镇区入园人数", "type": "bar", "data": town_enroll, "itemStyle": { "borderRadius": [6, 6, 0, 0] }, }, { "name": "乡村入园人数", "type": "bar", "data": rural_enroll, "itemStyle": { "borderRadius": [6, 6, 0, 0] }, }, { "name": "城区在园人数", "type": "line", "data": urban_in_school, "yAxisIndex": 0, "lineStyle": { "width": 3 }, "symbolSize": 8, }, { "name": "镇区在园人数", "type": "line", "data": town_in_school, "yAxisIndex": 0, "lineStyle": { "width": 3 }, "symbolSize": 8, }, { "name": "乡村在园人数", "type": "line", "data": rural_in_school, "yAxisIndex": 0, "lineStyle": { "width": 3 }, "symbolSize": 8, } ], } return option