Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

桑基图节点不能自定义顺序的BUG #2265

Closed
YanCore opened this issue Nov 26, 2023 · 7 comments
Closed

桑基图节点不能自定义顺序的BUG #2265

YanCore opened this issue Nov 26, 2023 · 7 comments
Labels

Comments

@YanCore
Copy link

YanCore commented Nov 26, 2023

问题

  1. 桑基图节点不能自定义顺序

运行环境(系统环境及 pyecharts 版本)

Python: 3.11.6
pyecharts: 2.0.4

代码及截图

代码


from pyecharts import options as opts
from pyecharts.charts import Sankey


nodes = [
    {'name': '指标A-高,指标B-高'},
    {'name': '指标A-高,指标B-低'},
    {'name': '指标A-低,指标B-高'},
    {'name': '指标A-低,指标B-低'},
    {'name': '指标C-高,指标D-高'},
    {'name': '指标C-高,指标D-低'},
    {'name': '指标C-低,指标D-高'},
    {'name': '指标C-低,指标D-低'}
]


links = [
    {'source': '指标A-高,指标B-高', 'target': '指标C-高,指标D-高', 'value': 8},
    {'source': '指标A-高,指标B-低', 'target': '指标C-高,指标D-高', 'value': 1},
    {'source': '指标A-低,指标B-高', 'target': '指标C-高,指标D-高', 'value': 1},
    {'source': '指标A-低,指标B-低', 'target': '指标C-高,指标D-高', 'value': 1},
    {'source': '指标A-高,指标B-高', 'target': '指标C-高,指标D-低', 'value': 18},
    {'source': '指标A-高,指标B-低', 'target': '指标C-高,指标D-低', 'value': 1},
    {'source': '指标A-低,指标B-高', 'target': '指标C-高,指标D-低', 'value': 4},
    {'source': '指标A-低,指标B-低', 'target': '指标C-高,指标D-低', 'value': 1},
    {'source': '指标A-高,指标B-高', 'target': '指标C-低,指标D-高', 'value': 1},
    {'source': '指标A-低,指标B-高', 'target': '指标C-低,指标D-高', 'value': 16},
    {'source': '指标A-低,指标B-低', 'target': '指标C-低,指标D-高', 'value': 7},
    {'source': '指标A-高,指标B-高', 'target': '指标C-低,指标D-低', 'value': 2},
    {'source': '指标A-高,指标B-低', 'target': '指标C-低,指标D-低', 'value': 4},
    {'source': '指标A-低,指标B-高', 'target': '指标C-低,指标D-低', 'value': 78},
    {'source': '指标A-低,指标B-低', 'target': '指标C-低,指标D-低', 'value': 134}
]

c = (
    Sankey()
    .add(
        "sankey",
        nodes,
        links,
        layout_iterations=0,
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="right"),
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
    .render("sankey_base.html")
)

运行结果

image

希望实现的效果

image

@YanCore
Copy link
Author

YanCore commented Nov 26, 2023

修改源码两处地方或者继承Sankey 重写add 方法实现。

        # BUG 设置layout_iterations为0,可以根据links的顺序自定义排序,不清楚这样限制的原因。
        # if layout_iterations < 32:
        #     layout_iterations = 32
                # BUG layoutIteration 改成 layoutIterations
                # "layoutIteration": layout_iterations,
                "layoutIterations": layout_iterations,

修改后的源码

from ... import options as opts
from ... import types
from ...charts.chart import Chart
from ...globals import ChartType


class Sankey(Chart):
    """
    <<< Sankey >>>

    Sankey diagram is a special flow diagram, which is mainly used to
    express how raw materials, energy and so on from the initial form through
    the intermediate process of processing, transformation to the final form.
    """

    def add(
        self,
        series_name: str,
        nodes: types.Sequence,
        links: types.Sequence,
        *,
        pos_left: types.Union[str, types.Numeric] = "5%",
        pos_top: types.Union[str, types.Numeric] = "5%",
        pos_right: types.Union[str, types.Numeric] = "20%",
        pos_bottom: types.Union[str, types.Numeric] = "5%",
        node_width: types.Numeric = 20,
        node_gap: types.Numeric = 8,
        node_align: str = "justify",
        layout_iterations: types.Numeric = 32,
        orient: str = "horizontal",
        is_draggable: bool = True,
        edge_label_opt: types.Label = None,
        focus_node_mode: str = "none",
        levels: types.SankeyLevel = None,
        label_opts: types.Label = opts.LabelOpts(),
        linestyle_opt: types.LineStyle = opts.LineStyleOpts(),
        tooltip_opts: types.Tooltip = None,
        itemstyle_opts: types.ItemStyle = None,
    ):
        # BUG 设置layout_iterations为0,可以根据links的顺序自定义排序,不清楚这样限制的原因。
        # if layout_iterations < 32:
        #     layout_iterations = 32

        self._append_legend(series_name)
        self.options.get("series").append(
            {
                "type": ChartType.SANKEY,
                "name": series_name,
                "data": nodes,
                "links": links,
                "left": pos_left,
                "top": pos_top,
                "right": pos_right,
                "bottom": pos_bottom,
                "nodeWidth": node_width,
                "nodeGap": node_gap,
                "nodeAlign": node_align,
                # BUG layoutIteration 改成 layoutIterations
                # "layoutIteration": layout_iterations,
                "layoutIterations": layout_iterations,
                "orient": orient,
                "draggable": is_draggable,
                "edgeLabel": edge_label_opt,
                "emphasis": {
                    "focus": focus_node_mode,
                },
                "levels": levels,
                "label": label_opts,
                "lineStyle": linestyle_opt,
                "tooltip": tooltip_opts,
                "itemStyle": itemstyle_opts,
            }
        )
        return self

@sunhailin-Leo
Copy link
Member

@YanCore

  • layoutIterations 这个我已经知道了,还没提交代码到 dev.
  • 关于默认值是 32,echarts 是这么解释的:布局的迭代次数,目的是不断迭代优化图中节点和边的位置,以减少节点和边之间的相互遮盖,默认值是 32。如果希望图中节点的顺序是按照原始中的顺序排列的,可设该值为 0。
  • Reference: https://echarts.apache.org/zh/option.html#series-sankey.layoutIterations

@YanCore
Copy link
Author

YanCore commented Nov 27, 2023

得把这个条件判断去掉吧。layout_iterations 设置为0 ,他也会重置成32 。

        if layout_iterations < 32:
            layout_iterations = 32

@sunhailin-Leo
Copy link
Member

@YanCore

  • 等 2.0.5 版本发了就修好了~

@Chillaxwow
Copy link

2.0.5 版本好像只修了那个会自动设置layout_iterations=32的问题,还是需要把Sankey源文件里的
"layoutIterations": layout_iterations,
修改为
"layoutIterations": 0,
才能让他不自动排序

@sunhailin-Leo
Copy link
Member

@Chillaxwow

@Chillaxwow
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants