def show_sankey_figure(self, figure_name, title):
     sankey = Sankey(init_opts=opts.InitOpts(width='600px', height='600px'))
     sankey.add(
         "",
         self.format_nodes,
         self.links,
         levels=[
             opts.SankeyLevelsOpts(
                 depth=0,
                 linestyle_opts=opts.LineStyleOpts(color="source",
                                                   curve=0.5,
                                                   opacity=0.6),
                 itemstyle_opts=opts.ItemStyleOpts(border_width=0),
             ),
             opts.SankeyLevelsOpts(
                 depth=1,
                 itemstyle_opts=opts.ItemStyleOpts(border_width=0),
                 linestyle_opts=opts.LineStyleOpts(color="target",
                                                   curve=0.5,
                                                   opacity=0.6),
             ),
             opts.SankeyLevelsOpts(
                 depth=2,
                 itemstyle_opts=opts.ItemStyleOpts(border_width=0)),
         ],
         pos_right="13%",
         node_gap=1,
         label_opts=opts.LabelOpts(position="right"),
     )
     sankey.set_global_opts(
         title_opts=opts.TitleOpts(title=title, pos_left='center'))
     sankey.render("figure_html\{}.html".format(figure_name))
def page_Sankey(nodes_links, title_name):
    # 使用Sankey()函数创建对象赋值给sankey
    # 使用InitOpts(),传入参数theme="dark",bg_color="#253441",赋值给init_opts
    sankey = Sankey(init_opts=opts.InitOpts(theme="dark", bg_color="#253441"))

    # 为创建的实例增加名字(sankey)、传入实验组节点和信息流列表
    sankey.add(
        "sankey",
        nodes=nodes_links[0],
        links=nodes_links[1],
        # 使用LineStyleOpts(),传入参数opacity=0.3, curve=0.5, color="source",赋值给linestyle_opt
        linestyle_opt=opts.LineStyleOpts(opacity=0.3,
                                         curve=0.5,
                                         color="source"),
        # 使用LabelOpts(),传入参数position="right",color="white",font_size=10,赋值给label_opts
        label_opts=opts.LabelOpts(position="right",
                                  color="white",
                                  font_size=10),
        # 使用SankeyLevelsOpts(),传入参数depth为0-5、itemstyle_opts,赋值给列表levels
        levels=[
            opts.SankeyLevelsOpts(
                depth=0,
                # 使用ItemStyleOpts(),传入参数color="#FF8947",border_color="#FF8947"
                itemstyle_opts=opts.ItemStyleOpts(color="#FF8947",
                                                  border_color="#FF8947")),
            opts.SankeyLevelsOpts(
                depth=1,
                # 使用ItemStyleOpts(),传入参数color="#96D15C",border_color="#96D15C"
                itemstyle_opts=opts.ItemStyleOpts(color="#96D15C",
                                                  border_color="#96D15C")),
            opts.SankeyLevelsOpts(
                depth=2,
                # 使用ItemStyleOpts(),传入参数color="#479BED",border_color="#479BED"
                itemstyle_opts=opts.ItemStyleOpts(color="#479BED",
                                                  border_color="#479BED")),
            opts.SankeyLevelsOpts(
                depth=3,
                # 使用ItemStyleOpts(),传入参数color="#55C4CA",border_color="#55C4CA"
                itemstyle_opts=opts.ItemStyleOpts(color="#55C4CA",
                                                  border_color="#55C4CA")),
            opts.SankeyLevelsOpts(
                depth=4,
                # 使用ItemStyleOpts(),传入参数color="#E7BF4F",border_color="#E7BF4F"
                itemstyle_opts=opts.ItemStyleOpts(color="#E7BF4F",
                                                  border_color="#E7BF4F"))
        ])
    # 使用TitleOpts(),传入参数title,赋值给title_opts
    # 使用LegendOpts(),传入参数is_show=False,赋值给legend_opts
    # 调用set_global_opts()
    sankey.set_global_opts(title_opts=opts.TitleOpts(title=title_name),
                           legend_opts=opts.LegendOpts(is_show=False))
    # 使用return返回sankey
    return sankey
예제 #3
0
def sankey_with_level_setting() -> Sankey:
    with open(os.path.join("fixtures", "product.json"), "r",
              encoding="utf-8") as f:
        j = json.load(f)
    c = (Sankey().add(
        "sankey",
        nodes=j["nodes"],
        links=j["links"],
        pos_top="10%",
        focus_node_adjacency=True,
        levels=[
            opts.SankeyLevelsOpts(
                depth=0,
                itemstyle_opts=opts.ItemStyleOpts(color="#fbb4ae"),
                linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            ),
            opts.SankeyLevelsOpts(
                depth=1,
                itemstyle_opts=opts.ItemStyleOpts(color="#b3cde3"),
                linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            ),
            opts.SankeyLevelsOpts(
                depth=2,
                itemstyle_opts=opts.ItemStyleOpts(color="#ccebc5"),
                linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            ),
            opts.SankeyLevelsOpts(
                depth=3,
                itemstyle_opts=opts.ItemStyleOpts(color="#decbe4"),
                linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            ),
        ],
        linestyle_opt=opts.LineStyleOpts(curve=0.5),
    ).set_global_opts(
        title_opts=opts.TitleOpts(title="Sankey-Level Settings"),
        tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"),
    ))
    return c
예제 #4
0
def test_sankey_new_opts(fake_writer):
    nodes = [
        {
            "name": "a"
        },
        {
            "name": "b"
        },
        {
            "name": "a1"
        },
        {
            "name": "b1"
        },
        {
            "name": "c"
        },
        {
            "name": "e"
        },
    ]
    links = [
        {
            "source": "a",
            "target": "a1",
            "value": 5
        },
        {
            "source": "e",
            "target": "b",
            "value": 3
        },
        {
            "source": "a",
            "target": "b1",
            "value": 3
        },
        {
            "source": "b1",
            "target": "a1",
            "value": 1
        },
        {
            "source": "b1",
            "target": "c",
            "value": 2
        },
        {
            "source": "b",
            "target": "c",
            "value": 1
        },
    ]
    c = Sankey().add(
        "sankey",
        nodes,
        links,
        pos_bottom="10%",
        focus_node_adjacency="allEdges",
        orient="vertical",
        levels=[
            opts.SankeyLevelsOpts(
                depth=0,
                itemstyle_opts=opts.ItemStyleOpts(color="#eee"),
                linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            )
        ],
        linestyle_opt=opts.LineStyleOpts(opacity=0.2,
                                         curve=0.5,
                                         color="source"),
        label_opts=opts.LabelOpts(position="right"),
    )
    c.render()
    _, content = fake_writer.call_args[0]
    assert_in("bottom", content)
    assert_in("orient", content)
    assert_in("levels", content)
    assert_in("focusNodeAdjacency", content)
예제 #5
0
import json
from pyecharts.charts import Sankey

with open("product.json", "r", encoding="utf-8") as f:
    j = json.load(f)

c = (Sankey().add(
    "sankey",
    nodes=j["nodes"],
    links=j["links"],
    pos_top="10%",
    focus_node_adjacency=True,
    levels=[
        opts.SankeyLevelsOpts(
            depth=0,
            itemstyle_opts=opts.ItemStyleOpts(color="#fbb4ae"),
            linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
        ),
        opts.SankeyLevelsOpts(
            depth=1,
            itemstyle_opts=opts.ItemStyleOpts(color="#b3cde3"),
            linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
        ),
        opts.SankeyLevelsOpts(
            depth=2,
            itemstyle_opts=opts.ItemStyleOpts(color="#ccebc5"),
            linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
        ),
        opts.SankeyLevelsOpts(
            depth=3,
            itemstyle_opts=opts.ItemStyleOpts(color="#decbe4"),