HTMLcopy
1
<div id="container"></div>
CSScopy
x
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
9
10
.anychart-tooltip {
11
font-size: 0.6em;
12
background: #fff;
13
color: #333;
14
box-shadow: 0 0 5px #333;
15
padding: 20px;
16
}
17
18
.anychart-tooltip h5 {
19
margin: 0 0 5px;
20
}
21
22
.anychart-tooltip ul {
23
margin: 0 0 5px;
24
}
JavaScriptcopy
132
1
anychart.onDocumentReady(function () {
2
// create sankey chart
3
var chart = anychart.sankey();
4
5
// set title for chart
6
chart.title('Energy Flows (TWh/y)');
7
8
// set chart's data
9
chart.data(getData()); // eslint-disable-line no-undef
10
11
// set chart's padding
12
chart.padding(20, 80, 20, 40);
13
14
// set chart's flows curvature
15
chart.curveFactor(0.2);
16
17
// set chart's node width
18
chart.nodeWidth(50);
19
20
// set chart's node padding
21
chart.nodePadding(30);
22
23
// set node's label position
24
chart
25
.node()
26
.normal()
27
.labels()
28
.anchor('center-bottom')
29
.position('center-top');
30
31
// set tooltips position for node
32
chart.node().tooltip().anchor('center-bottom');
33
34
// disable tooltips for flows and dropoffs
35
chart.flow().tooltip().enabled(false);
36
chart.dropoff().tooltip().enabled(false);
37
38
// enable HTML-mode for node's tooltip and set formatter for it
39
chart
40
.node()
41
.tooltip()
42
.useHtml(true)
43
.format(function () {
44
var tooltip = '';
45
var i;
46
var ul;
47
var income;
48
var outcome;
49
var conflict;
50
if (this.income.length) {
51
income = 0;
52
ul = '<ul>';
53
for (i = 0; i < this.income.length; i++) {
54
ul +=
55
'<li>' +
56
this.income[i].name +
57
': ' +
58
this.income[i].value +
59
'</li>';
60
income += this.income[i].value;
61
}
62
ul += '</ul>';
63
tooltip += '<h5>Income(' + income + '):</h5>' + ul;
64
}
65
if (this.outcome.length) {
66
outcome = 0;
67
ul = '<ul>';
68
for (i = 0; i < this.outcome.length; i++) {
69
ul +=
70
'<li>' +
71
this.outcome[i].name +
72
': ' +
73
this.outcome[i].value +
74
'</li>';
75
outcome += this.outcome[i].value;
76
}
77
ul += '</ul>';
78
tooltip += '<h5>Outcome(' + outcome + '):</h5>' + ul;
79
}
80
if (this.dropoff > 0) {
81
tooltip += '<h5>Dropoff: ' + this.dropoff + '</h5>';
82
}
83
if (this.isConflict) {
84
if (income > outcome + this.dropoff) {
85
conflict = income - (outcome + this.dropoff);
86
tooltip +=
87
'<h5>Conflict:</h5><ul><li> income is greater than outcome by ' +
88
conflict +
89
'</li></ul>';
90
} else {
91
conflict = outcome + this.dropoff - income;
92
tooltip +=
93
'<h5>Conflict:</h5><ul><li>outcome is greater than income by ' +
94
conflict +
95
'</li></ul>';
96
}
97
}
98
return tooltip;
99
});
100
101
// set node's label formatter
102
chart
103
.node()
104
.labels()
105
.useHtml(true)
106
.format(function () {
107
if (this.isConflict) {
108
return '<b style="color: red">' + this.name + '</b>';
109
}
110
return this.name;
111
});
112
113
// Set flow's normal and hovered fill
114
chart.flow({
115
normal: {
116
fill: function () {
117
return anychart.color.lighten(this.sourceColor, 0.5) + ' ' + 0.3;
118
}
119
},
120
hovered: {
121
fill: function () {
122
return this.sourceColor + ' ' + 0.9;
123
}
124
}
125
});
126
127
// set container id for the chart
128
chart.container('container');
129
130
// initiate chart drawing
131
chart.draw();
132
});