Files
YunNanProject/Model/RuYuanZaiYuanCountModel.py

142 lines
5.7 KiB
Python
Raw Normal View History

2025-09-10 16:08:59 +08:00
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
2025-09-11 13:46:12 +08:00
2025-09-10 16:08:59 +08:00
CurrentConfig.ONLINE_HOST = ONLINE_HOST
2025-09-11 13:46:12 +08:00
2025-09-10 16:08:59 +08:00
class RuYuanZaiYuanModel:
@staticmethod
def load_student_data():
try:
# 加载招生数据(入园人数)
with open("./Data/ZhaoShengCount.json", "r", encoding="utf-8") as f:
enrollment_data = json.load(f)
2025-09-11 13:46:12 +08:00
2025-09-10 16:08:59 +08:00
# 加载在校生数据(在园人数)
with open("./Data/ZaiXiaoShengCount.json", "r", encoding="utf-8") as f:
in_school_data = json.load(f)
2025-09-11 13:46:12 +08:00
2025-09-10 16:08:59 +08:00
return enrollment_data, in_school_data
except Exception as e:
print(f"读取学生数据出错: {e}")
return [], []
2025-09-11 13:46:12 +08:00
2025-09-10 16:08:59 +08:00
@staticmethod
def generate_preschool_education_config():
# 获取学前教育相关数据
2025-09-11 14:57:37 +08:00
enrollment_data, in_school_data = RuYuanZaiYuanModel.load_student_data()
# 提取云南省级数据
yunnan_enroll = next((item for item in enrollment_data if item["area_name"] == "云南省"), None)
if not yunnan_enroll:
return {}
2025-09-11 14:32:15 +08:00
# # 构建学前教育数据
2025-09-11 14:57:37 +08:00
urban_data = [] # 城区数据
town_data = [] # 镇区数据
rural_data = [] # 乡村数据
total_enroll = [] # 总入园数
# 提取年份数据(2015-2024)
years = [str(year) for year in range(2015, 2025)]
for year in years:
enroll_data = yunnan_enroll["education_data"]["preschool"].get(year, {})
urban_data.append(enroll_data.get("urban", 0) / 10000) # 转换为万人
town_data.append(enroll_data.get("town", 0) / 10000) # 转换为万人
rural_data.append(enroll_data.get("rural", 0) / 10000) # 转换为万人
# 计算总和作为总入园数而非使用文件中的total字段
calculated_total = enroll_data.get("urban", 0) + enroll_data.get("town", 0) + enroll_data.get("rural", 0)
total_enroll.append(calculated_total / 10000) # 转换为万人
2025-09-11 14:59:10 +08:00
# 添加2022年基数的粉色折线
base_year = "2022"
# 找到2022年在years中的索引位置
base_index = years.index(base_year) if base_year in years else 0
# 获取2022年的总入园数作为基数
base_value = total_enroll[base_index] if base_index < len(total_enroll) else 0
# 创建2022年基数折线数据2022-2024年
base_2022_line = []
for i, year in enumerate(years):
# 只在2022年及之后显示基数线
if i >= base_index:
base_2022_line.append(base_value)
else:
base_2022_line.append(None) # 2022年之前不显示
2025-09-11 14:57:37 +08:00
data = {"xAxis_data": years,
2025-09-11 15:04:14 +08:00
"series_data_0": urban_data, # 城区
"series_data_1": town_data, # 镇区
"series_data_2": rural_data, # 乡村
"series_data_3": total_enroll, # 总入园数
"series_data_4": base_2022_line # 2022年基数
2025-09-10 16:08:59 +08:00
}
2025-09-11 14:32:15 +08:00
return data
2025-09-11 13:46:12 +08:00
2025-09-10 20:18:01 +08:00
@staticmethod
def generate_in_school_education_config():
# 获取学前教育相关数据
enrollment_data, in_school_data = RuYuanZaiYuanModel.load_student_data()
2025-09-11 13:46:12 +08:00
2025-09-10 20:18:01 +08:00
# 提取云南省级数据
yunnan_in_school = next((item for item in in_school_data if item["area_name"] == "云南省"), None)
2025-09-11 13:46:12 +08:00
2025-09-10 20:18:01 +08:00
if not yunnan_in_school:
return {}
2025-09-11 13:46:12 +08:00
2025-09-10 20:18:01 +08:00
# 提取年份数据(2015-2024)
years = [str(year) for year in range(2015, 2025)]
2025-09-11 13:46:12 +08:00
2025-09-10 20:18:01 +08:00
# 构建学前教育数据
2025-09-11 13:46:12 +08:00
urban_data = [] # 城区数据
town_data = [] # 镇区数据
rural_data = [] # 乡村数据
2025-09-10 20:18:01 +08:00
total_in_school = [] # 总在园数
2025-09-11 13:46:12 +08:00
2025-09-10 20:18:01 +08:00
for year in years:
# 将education_data改为student_data
in_school_year_data = yunnan_in_school["student_data"]["preschool"].get(year, {})
2025-09-11 13:46:12 +08:00
2025-09-10 20:23:42 +08:00
# 先获取原始数据
urban_val = in_school_year_data.get("urban", 0)
town_val = in_school_year_data.get("town", 0)
rural_val = in_school_year_data.get("rural", 0)
2025-09-11 13:46:12 +08:00
2025-09-10 20:23:42 +08:00
# 转换为万人并添加到各自列表
urban_data.append(urban_val / 10000) # 转换为万人
2025-09-11 13:46:12 +08:00
town_data.append(town_val / 10000) # 转换为万人
2025-09-10 20:23:42 +08:00
rural_data.append(rural_val / 10000) # 转换为万人
2025-09-11 13:46:12 +08:00
2025-09-10 20:23:42 +08:00
# 计算总和并转换为万人
calculated_total = (urban_val + town_val + rural_val) / 10000 # 先计算总和再转换为万人
total_in_school.append(calculated_total)
2025-09-11 13:46:12 +08:00
2025-09-10 20:32:37 +08:00
# 添加2022年基数的粉色折线
base_year = "2022"
# 找到2022年在years中的索引位置
base_index = years.index(base_year) if base_year in years else 0
# 获取2022年的总在园数作为基数
base_value = total_in_school[base_index] if base_index < len(total_in_school) else 0
# 创建2022年基数折线数据2022-2024年
base_2022_line = []
for i, year in enumerate(years):
# 只在2022年及之后显示基数线
if i >= base_index:
base_2022_line.append(base_value)
else:
base_2022_line.append(None) # 2022年之前不显示
2025-09-11 13:46:12 +08:00
2025-09-11 15:04:14 +08:00
data = {
2025-09-11 15:14:30 +08:00
"xAxis_data": years,
"series_data_0": urban_data,
"series_data_1": town_data,
"series_data_2": rural_data,
"series_data_3": total_in_school,
"series_data_4": base_2022_line
2025-09-11 13:46:12 +08:00
}
2025-09-11 15:04:14 +08:00
return data