项目中需要使用桑基图的图表,对比百度的echarts和d3.js中的桑基图的例子,抱着学习的态度,选用了d3.js (version 4), 采坑记录

废话不多说,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Sankey</title>
<style>
body {
padding: 10px;
min-width: 600px;
max-width: 1200px;
margin: auto;
-moz-user-select: none;
-webkit-user-select: none;
}
#chart {
height: 500px;
font: 13px sans-serif;
}
.chart-sankey {
background: #fff;
box-shadow: 2px 5px 6px rgba(0, 0, 0, 0.5);
}
.node rect {
fill-opacity: .9;
shape-rendering: crispEdges;
stroke-width: 0;
cursor: move;
}
.node text {
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #ddd;
stroke-opacity: 0.5;
cursor: pointer;
}
.link title {
visibility: hidden;
}
.link:hover {
/*stroke-opacity: 1;*/
}
.link:hover title{
visibility: visible;
}
.tooltip {
position: absolute;
padding: 8px 10px;
background: rgba(255, 255, 255, 0.8);
border-radius: 3px;
pointer-events: none;
max-width: 200px;
display: none;
box-shadow: 1px 3px 4px rgba(0, 0, 0, 0.3);
line-height: 1.2;
font-size: 12px;
}
</style>
<script src="../src/d3/d3.min.js"></script>
<script src="../src/d3-sankey/d3-sankey.js"></script>
</head>
<body>
<div id="chart">
<div id="tooltip" class="tooltip"></div>
</div>
<script>
var margin = {top: 1, right: 1, bottom: 6, left: 1},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var timer;
var formatNumber = d3.format(",.0f"), //decimal places
format = function(d) { return formatNumber(d) + " TWh"; },
color = d3.scaleOrdinal(d3.schemeCategory20);
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var sankey = d3.sankey()
.nodeWidth(30)
.nodePadding(10)
.size([width, height]);

var path = sankey.link();
var toolTips = document.getElementById('tooltip');
var mouseHas = false

var energy= {
nodes: [
{
name: "加入购物车",
value: 3
},
{
name: "下单",
value: 2
},
{
name:"付款",
value:2
},
{
name:"加入购物车",
value:3
},
{
name:"下单",
value:5
},
{
name:"付款",
value:3
},
{
name:"收货",
value:2
},
{
name:"下单",
value:2
},
{
name:"付款",
value:8
},
{
name:"申请退款",
value:1
},
{
name:"收货",
value:1
},
{
name:"加入购物车",
value:1
},
{
name:"付款",
value:4
},
{
name:"收货",
value:3
},
{
name:"付款",
value:1
},
{
name:"申请退款",
value:3
},
{
name:"收货",
value:7
},
{
name:"退货",
value:2
},
{
name:"收货",
value:1
}
],
links: [
{
source:5,
target:10,
value:1
},
{
source:2,
target:6,
value:2
},
{
source:1,
target:5,
value:2
},
{
source:8,
target:16,
value:4
},
{
source:3,
target:8,
value:2
},
{
source:7,
target:12,
value:2
},
{
source:8,
target:13,
value:2
},
{
source:3,
target:7,
value:1
},
{
source:12,
target:15,
value:1
},
{
source:5,
target:13,
value:1
},
{
source:13,
target:17,
value:1
},
{
source:0,
target:5,
value:1
},
{
source:14,
target:18,
value:1
},
{
source:11,
target:14,
value:1
},
{
source:10,
target:17,
value:1
},
{
source:8,
target:15,
value:2
},
{
source:0,
target:4,
value:2
},
{
source:12,
target:16,
value:3
},
{
source:4,
target:8,
value:5
},
{
source:5,
target:9,
value:1
}
]
}
sankey
.nodes(energy.nodes)
.links(energy.links)
.layout(32);
// 设置 link
var link = svg.append("g").selectAll(".link")
.data(energy.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) {
return b.dy - a.dy;
})
.on('mouseover', function (d) {
var coordinates = [0, 0];
coordinates = d3.mouse(this);
var x = coordinates[0];
var y = coordinates[1];
toolTips.innerHTML= d.source.name + "->" + d.target.name + "<br/>" + format(d.value);
toolTips.style.display = 'block'
toolTips.style.top = (y - 20) + 'px'
toolTips.style.left = (x + 100) + 'px'
// tip.show(d)
})
.on('mousemove', function (d) {
var coordinates = [0, 0];
coordinates = d3.mouse(this);
var x = coordinates[0];
var y = coordinates[1];
toolTips.innerHTML= d.source.name + "->" + d.target.name + "<br/>" + format(d.value);
toolTips.style.display = 'block'
toolTips.style.top = (y - 20) + 'px'
toolTips.style.left = (x + 50) + 'px'
// tip.show(d)
})
.on('mouseout', function (d, e) {
var evt = window.event || e;
mouseHas = false
var obj=evt.toElement||evt.relatedTarget;
var pa=this;
if(pa.contains(obj)) return false;
toolTips.style.display = 'none'
// tip.show(d)
})
// 设置node
var node = svg.append("g").selectAll(".node")
.data(energy.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.drag()
.subject(function(d) {
return d;
})
.on("start", function() {
this.parentNode.appendChild(this);
})
.on("drag", dragmove));

node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) { return d.color = color(d.name.split("|")[0]); })
.style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) { return d.name + "\n" + format(d.value); });

node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.style("fill", "#666")
.text(function(d) { return d.name; })
.filter(function(d) { return d.x < width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
function dragmove(d) {
d3.select(this).attr("transform", "translate(" + d.x + "," + (d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
sankey.relayout();
link.attr("d", path);
}
</script>
</body>
</html>

代码中大多是官网上的例子,只是加了个提示框的跟随鼠标移动的提示框。
说说遇到的问题吧

问题1: 鼠标放到每个link上,如何使相应的 link 颜色加深

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//  css  样式中是这么写的
.link {
fill: none;
stroke: #ddd;
stroke-opacity: 0.5;
cursor: pointer;
}
.link title {
visibility: hidden;
}
.link:hover {
stroke-opacity: 1;
}
// 加了鼠标事件后发现这个hover效果不好用了,需求时间紧,没办法,简单粗暴,好用就行。
// 所以在鼠标mouseover 、mousemove、mouseout事件中修改了,代码如下
.on('mouseover', function (d) {
d3.select(this).style("stroke-opacity", "1")
.......
})
.on('mousemove', function (d) {
d3.select(this).style("stroke-opacity", "1")
.......
})
.on('mouseout', function (d) {
d3.select(this).style("stroke-opacity", "0.5")
.......
})

问题2: 例子中的测试数据量大小,开发中用大数据测试的时候发现页面全乱了。

1
2
3
4
数据量大的时候页面会报错
Error: <rect> attribute height: A negative value is not valid

stackoverflow答案链接:http://stackoverflow.com/questions/40579174/large-data-set-breaks-d3-sankey-diagram/40581444#40581444

如有问题,欢迎交流与学习。

github

google和baidu的一些参考资料: