首页编程开发ECharts【数据可视化(EChart...

【数据可视化(ECharts篇)】17.ECharts图表绘制——桑基图

本系列文章配套代码获取有以下两种途径:

  • 通过百度网盘获取:
链接:https://pan.baidu.com/s/1W26KrZK7RjwgxvulccTA9w?pwd=mnsj 提取码:mnsj
  • 前往GitHub获取
https://github.com/returu/Data_Visualization





桑基图是一种特殊的流图(可以看作是有向无环图)。它主要用来表示原材料、能量等如何从最初形式经过中间过程的加工或转化达到最终状态。
桑基图中延伸的分支宽度(粗细)代表能力的大小,桑基图的主要特征就是首末端的能力相等,表现在图中就是宽度总和是相等的。

ECharts中绘制桑基图,需要将series中的type设置为"sankey",另外需要在data(nodes)中定义桑基图节点数据以及在links(edges)节点间边的信息。

需要注意: 桑基图理论上只支持有向无环图(DAG, Directed Acyclic Graph),所以请确保输入的边是无环的。

例如,通过以下代码,就可以生成一个简单的桑基图:

 1    // 设置图表的参数
2    var option = {
3        title: {
4            text: "桑基图",
5        },
6
7        tooltip: {},
8
9        // 数据系列
10        series: [{
11            name: "系列1",
12            type: "sankey",
13
14            // 桑基图节点数据列表
15            data: [{
16                    name: 'Total'
17                },{
18                    name: 'A'
19                },{
20                    name: 'B'
21                },{
22                    name: 'A1'
23                }, {
24                    name: 'A2'
25                },{
26                    name: 'B1'
27                },{
28                    name: 'B2'
29                },{
30                    name: 'A11'
31                },{
32                    name: 'A12'
33                },{
34                    name: 'B11'
35                },{
36                    name: 'B12'
37                },
38            ],
39
40            // 节点间的边
41            links: [{
42                    source: 'Total',
43                    target: 'A',
44                    value10
45                },{
46                    source: 'Total',
47                    target: 'B',
48                    value8
49                },{
50                    source: 'A',
51                    target: 'A1',
52                    value5
53                },{
54                    source: 'A',
55                    target: 'A2',
56                    value3
57                },{
58                    source: 'A',
59                    target: 'B1',
60                    value2
61                },{
62                    source: 'B',
63                    target: 'B1',
64                    value4
65                },{
66                    source: 'B',
67                    target: 'B2',
68                    value4
69                },{
70                    source: 'A1',
71                    target: 'A11',
72                    value3
73                },{
74                    source: 'A1',
75                    target: 'A12',
76                    value2
77                },{
78                    source: 'A2',
79                    target: 'A12',
80                    value3
81                },{
82                    source: 'B1',
83                    target: 'B11',
84                    value4
85                },{
86                    source: 'B2',
87                    target: 'B12',
88                    value3
89                },
90            ],
91        }],
92    };

可视化结果如下图所示:

01

自定义层级样式


可以通过data中的depth参数指定节点所在的层(取值从 0 开始),然后通过levels桑基图的每一层进行单独设置。

代码如下所示:

  1    // 设置图表的参数
2    var option = {
3        title: {
4            text: "桑基图",
5        },
6
7        tooltip: {},
8
9        // 数据系列
10        series: [{
11            name: "系列1",
12            type: "sankey",
13
14            // 桑基图节点数据列表
15            data: [{
16                name: 'Total'
17            }, {
18                name: 'A'
19            }, {
20                name: 'B'
21            }, {
22                name: 'A1'
23            }, {
24                name: 'A2'
25            }, {
26                name: 'B1'
27            }, {
28                name: 'B2'
29            }, {
30                name: 'A11'
31            }, {
32                name: 'A12'
33            }, {
34                name: 'B11'
35            }, {
36                name: 'B12'
37            }, ],
38
39            levels: [{
40                    depth: 0,
41                    itemStyle: {
42                        color: '#fbb4ae'
43                    },
44                    lineStyle: {
45                        color: 'source',
46                        opacity: 0.6
47                    }
48                },
49                {
50                    depth: 1,
51                    itemStyle: {
52                        color: '#b3cde3'
53                    },
54                    lineStyle: {
55                        color: 'source',
56                        opacity: 0.6
57                    }
58                },
59                {
60                    depth: 2,
61                    itemStyle: {
62                        color: '#ccebc5'
63                    },
64                    lineStyle: {
65                        color: 'source',
66                        opacity: 0.6
67                    }
68                },
69                {
70                    depth: 3,
71                    itemStyle: {
72                        color: '#decbe4'
73                    },
74                    lineStyle: {
75                        color: 'source',
76                        opacity: 0.6
77                    }
78                }
79            ],
80
81            // 节点间的边
82            links: [{
83                source: 'Total',
84                target: 'A',
85                value10
86            }, {
87                source: 'Total',
88                target: 'B',
89                value8
90            }, {
91                source: 'A',
92                target: 'A1',
93                value5
94            }, {
95                source: 'A',
96                target: 'A2',
97                value3
98            }, {
99                source: 'A',
100                target: 'B1',
101                value2
102            }, {
103                source: 'B',
104                target: 'B1',
105                value4
106            }, {
107                source: 'B',
108                target: 'B2',
109                value4
110            }, {
111                source: 'A1',
112                target: 'A11',
113                value3
114            }, {
115                source: 'A1',
116                target: 'A12',
117                value2
118            }, {
119                source: 'A2',
120                target: 'A12',
121                value3
122            }, {
123                source: 'B1',
124                target: 'B11',
125                value4
126            }, {
127                source: 'B2',
128                target: 'B12',
129                value3
130            }, ],
131        }],
132    };

可视化结果如下图所示:

另外,可以通过label设置描述每个矩形节点中文本标签的样式,通过itemStyle设置桑基图节点矩形的样式、通过lineStyle设置桑基图边的样式,设置方式与之前图表类似,这里就不做详细介绍。

更多参数和配置可以查看官方文档:

1https://echarts.apache.org/zh/option.html


本篇文章来源于微信公众号: 码农设计师

RELATED ARTICLES

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments