You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将指定列表数据转换为Pyecharts旭日图所需的层级树结构?

实现API变更数据到旭日图层级结构的转换方法

需求说明

给定如下格式的API变更数据:

data = [
    [2019, "July", 8, '1.2.0', 7.0, None, None, None],
    [2019, "July", 10, '1.2.0', 52.0, "Breaking", 6.0, 'Path Removed w/o Deprecation'],
    [2019, "July", 15, "0.1.0", 210.0, "Breaking", 57.0, 'Request Parameter Removed'],
    [2019, 'August', 20, '2.0.0', 100.0, "Breaking", None, None],
    [2019, 'August', 25, '2.0.0', 200.0, 'Non-breaking', None, None],
]

数据字段对应关系为:Year, Month, Day, info_version, API_changes, type1, count, content,需要将其转换为pyecharts旭日图所需的层级树结构,且不能修改原始数据。

实现思路

通过迭代遍历每条数据,逐层构建节点:

  • 从年份开始,依次检查每个层级(月份、日期、版本、变更类型、变更内容)的节点是否已存在,不存在则创建新节点
  • 根据字段是否为None决定是否添加对应层级(比如type1为None时,跳过该层级)
  • 为每个节点设置对应的value值,严格遵循示例中的赋值规则

代码实现

def build_sunburst_data(raw_data):
    # 初始化根容器,存储所有年份节点
    root = {}
    for row in raw_data:
        year, month, day, version, api_changes, type1, count, content = row
        
        # 处理年份节点
        year_str = str(year)
        if year_str not in root:
            root[year_str] = {"name": year_str, "children": []}
        year_node = root[year_str]
        
        # 处理月份节点
        month_node = next((child for child in year_node["children"] if child["name"] == month), None)
        if not month_node:
            month_node = {"name": month, "children": []}
            year_node["children"].append(month_node)
        
        # 处理日期节点
        day_str = str(day)
        day_node = next((child for child in month_node["children"] if child["name"] == day_str), None)
        if not day_node:
            day_node = {"name": day_str, "children": []}
            month_node["children"].append(day_node)
        
        # 处理版本节点
        version_node = next((child for child in day_node["children"] if child["name"] == version), None)
        if not version_node:
            version_node = {"name": version, "value": api_changes, "children": []}
            day_node["children"].append(version_node)
        
        # 处理变更类型节点(仅当type1不为空时)
        if type1 is not None:
            type_node = next((child for child in version_node["children"] if child["name"] == type1), None)
            if not type_node:
                # 优先用count作为value,无count则用api_changes
                node_value = count if count is not None else api_changes
                type_node = {"name": type1, "value": node_value, "children": []}
                version_node["children"].append(type_node)
            
            # 处理变更内容节点(仅当content不为空时)
            if content is not None:
                content_node = next((child for child in type_node["children"] if child["name"] == content), None)
                if not content_node:
                    content_node = {"name": content, "value": count}
                    type_node["children"].append(content_node)
    
    # 转换为旭日图可用的列表格式
    return list(root.values())

# 测试转换并查看结果
sunburst_data = build_sunburst_data(data)
import json
print(json.dumps(sunburst_data, indent=2))

代码说明

  • 用字典root存储年份节点,避免重复创建相同层级
  • 每层节点通过next()快速查找是否已存在,不存在则创建并添加到父节点的children列表
  • 针对None值字段做了跳过处理,保证结构符合示例要求
  • 数值赋值严格匹配示例规则:版本节点value为API_changes,类型节点优先用count,内容节点value固定为count

转换后的结构可直接传入pyecharts的Sunburst组件使用。

内容的提问来源于stack exchange,提问作者Brie MerryWeather

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:43:11