HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create data
4
var data = [
5
{from: "Canada", to: "France", weight: 2230000, custom_field: "info 1"},
6
{from: "Canada", to: "Germany", weight: 1990000, custom_field: "info 2"},
7
{from: "Canada", to: "Italy", weight: 1180000, custom_field: "info 3"},
8
{from: "Canada", to: "Spain", weight: 990000, custom_field: "info 4"},
9
{from: "USA", to: "China", weight: 1250000, custom_field: "info 5"},
10
{from: "USA", to: "France", weight: 950000, custom_field: "info 6"},
11
{from: "USA", to: "Germany", weight: 2020000, custom_field: "info 7"},
12
{from: "USA", to: "Spain", weight: 1110000, custom_field: "info 8"},
13
{from: "France", to: "China", weight: 1100000, custom_field: "info 9"},
14
{from: "France", to: "Japan", weight: 1050000, custom_field: "info 10"},
15
{from: "France", to: "India", weight: 1030000, custom_field: "info 11"},
16
{from: "Germany", to: "China", weight: 2150000, custom_field: "info 12"},
17
{from: "Germany", to: "Japan", weight: 660000, custom_field: "info 13"},
18
{from: "Germany", to: "India", weight: 1200000, custom_field: "info 14"},
19
{from: "Italy", to: "China", weight: 1180000, custom_field: "info 15"},
20
{from: "Spain", to: "China", weight: 1120000, custom_field: "info 16"},
21
{from: "Spain", to: "Japan", weight: 980000, custom_field: "info 17"}
22
];
23
24
// create a chart and set the data
25
var chart = anychart.sankey(data);
26
27
// set the width of nodes
28
chart.nodeWidth("50%");
29
30
// configure labels of nodes
31
chart.node().labels().useHtml(true);
32
chart.node().labels().format(function() {
33
return "<span style='font-weight:bold'>" + this.name +
34
"</span><br>" + Math.round(this.value/100000)/10 + " mln";
35
});
36
37
// configure labels of flows
38
chart.flow().labels().format(function() {
39
return Math.round(this.value/100000)/10 + " mln";
40
});
41
42
// configure tooltip titles of nodes
43
chart.node().tooltip().titleFormat(function() {
44
return this.name + " (" + Math.round(this.value/100000)/10 +
45
" mln)";
46
});
47
48
// configure tooltips of nodes
49
chart.node().tooltip().format(function() {
50
51
var incomeText = "";
52
var outcomeText = "";
53
54
for (i = 0; i < this.income.length; i++) {
55
incomeText += Math.round(this.income[i].value/100000)/10 +
56
" mln <- " + this.income[i].name + "\n";
57
}
58
59
for (i = 0; i < this.outcome.length; i++) {
60
outcomeText += Math.round(this.outcome[i].value/100000)/10 +
61
" mln -> " + this.outcome[i].name + "\n";
62
}
63
64
if (this.outcome.length > 0) {
65
incomeText = incomeText + "\n";
66
}
67
68
return incomeText + outcomeText;
69
});
70
71
// configure tooltips of flows
72
chart.flow().tooltip().format(function() {
73
return Math.round(this.value/100000)/10 + " mln" +
74
"\n\n" + this.getData("custom_field");
75
});
76
77
// set the chart title
78
chart.title("Sankey Diagram: Labels and Tooltips (Formatting Functions)");
79
80
// set the container id
81
chart.container("container");
82
83
// initiate drawing the chart
84
chart.draw();
85
});