HTMLcopy
1
<div id="container"></div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
// add data
3
var data = [
4
{from: 'First Class', to: 'Child', value: 6},
5
{from: 'Second Class', to: 'Child', value: 24},
6
{from: 'Third Class', to: 'Child', value: 79},
7
{from: 'Crew', to: 'Child', value: 0},
8
{from: 'First Class', to: 'Adult', value: 319},
9
{from: 'Second Class', to: 'Adult', value: 261},
10
{from: 'Third Class', to: 'Adult', value: 627},
11
{from: 'Crew', to: 'Adult', value: 885},
12
{from: 'Child', to: 'Female', value: 45},
13
{from: 'Child', to: 'Male', value: 64},
14
{from: 'Adult', to: 'Female', value: 425},
15
{from: 'Adult', to: 'Male', value: 1667},
16
{from: 'Female', to: 'Survived', value: 344},
17
{from: 'Female', to: 'Perished', value: 126},
18
{from: 'Male', to: 'Survived', value: 367},
19
{from: 'Male', to: 'Perished', value: 1364}
20
];
21
22
// create a sankey diagram instance
23
var chart = anychart.sankey();
24
25
// load the data to the sankey diagram instance
26
chart.data(data);
27
28
// set the chart's padding
29
chart.padding(20, 40);
30
31
// configure a custom color palette
32
chart.palette([
33
'#f5dc50',
34
'#e1a03c',
35
'#c87d5a',
36
'#fff0c8',
37
'#aa96b4',
38
'#6e5a7d',
39
'#e19196',
40
'#419bd2',
41
'#46afaa',
42
'#5a5050'
43
]);
44
45
// customize the nodes:
46
// set the width
47
chart.nodeWidth('40%');
48
// set the padding
49
chart.nodePadding(30);
50
// customize the labels
51
chart.node().normal().labels().fontSize(14);
52
chart.node().labels().useHtml(true);
53
chart
54
.node()
55
.labels()
56
.format('<span style="font-weight:bold">{%name}</span><br>{%value}');
57
58
// customize the links
59
chart.flow({
60
normal: {
61
fill: function () {
62
return anychart.color.lighten(this.sourceColor, 0.5) + ' ' + 0.3;
63
}
64
},
65
hovered: {
66
fill: function () {
67
return this.sourceColor + ' ' + 0.8;
68
}
69
}
70
});
71
72
// add a title and customize it
73
chart
74
.title()
75
.enabled(true)
76
.useHtml(true)
77
.text(
78
'<span style = "color: #2b2b2b; font-size:20px;">Titanic Survivors</span>' +
79
'<br/><span style="color:#00bfa5; font-size: 16px;">(by gender, age, ticket class)</span>'
80
);
81
82
// set the chart container id
83
chart.container('container');
84
85
// draw the chart
86
chart.draw();
87
88
});