Sunburst graph pyecharts 中的排序函数

问题描述 投票:0回答:1

我有一个像这样的森伯斯特图:

我希望图表按月开始,例如八月、十月和十一月。我尝试使用

sort= undefined
但它没有修复它,即使它适用于
echarts

这是当前选项集:

c = (
    Sunburst(init_opts=opts.InitOpts(width="1200px", height="1200px"))
    .add(
        "",
        data_pair=data4,
        highlight_policy="ancestor",
        radius=[0, "95%"],
        sort_="undefined",
    )
      .render("sunburst1.html")
)

有谁知道我该如何解决这个问题,我一直在查看文档和示例,但是没有关于此的任何信息。

python echarts
1个回答
0
投票

你必须在

sort_
中编写JS代码才能使用它。 github 上有一个关于这个的问题。要保持数据默认顺序而不是按值,请设置
sort_ = types.JsCode("null")

from pyecharts.charts import Sunburst
from pyecharts import options as opts, types

c = (
    Sunburst(init_opts=opts.InitOpts(width="1200px", height="1200px"))
    .add(
        "",
        data_pair=data,
        highlight_policy="ancestor",
        radius=[0, "95%"],
        sort_=types.JsCode("null"),
    )
      .render("sunburst.html")
)

在禁用它的默认 JS 排序后,您可以在 python 中对

data
进行排序。例如,在您的情况下,您想按月对它进行排序,这是您的原始数据。

data = [
    opts.SunburstItem(
        name="2021",
        children=[
            opts.SunburstItem(
                name="October",
                value=15,
                children=[
                    opts.SunburstItem(name="24th", value=2),
                    opts.SunburstItem(
                        name="7th",
                        value=5,
                        children=[opts.SunburstItem(name="0.1.0", value=2)],
                    ),
                    opts.SunburstItem(name="5th", value=4),
                ],
            ),
            opts.SunburstItem(
                name="August",
                value=10,
                children=[
                    opts.SunburstItem(name="17th", value=5),
                    opts.SunburstItem(name="13th", value=1),
                ],
            ),
            opts.SunburstItem(
                name="November",
                value=10,
                children=[
                    opts.SunburstItem(name="12th", value=5),
                    opts.SunburstItem(name="4th", value=1),
                ],
            ),
        ],
    ),
    opts.SunburstItem(
        name="2022",
        children=[
            opts.SunburstItem(
                name="May",
                children=[
                    opts.SunburstItem(name="16th", value=1),
                    opts.SunburstItem(name="8th", value=2),
                ],
            )
        ],
    ),
]

可以这样排序

from datetime import datetime

for i, year in enumerate(data):
    months = year.opts['children']
    sorted_months = sorted(months, key=lambda m: datetime.strptime(m.opts['name'], "%B"))
    data[i].opts['children'] = sorted_months
© www.soinside.com 2019 - 2024. All rights reserved.