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
// create data set on our data
3
var dataSet = anychart.data.set([
4
['1', 13, 65, 21, 0, 0],
5
['2', 13, 67, 19, 0, 0],
6
['3', 82, 12, 0, 0, 6],
7
['4', 1, 68, 19, 11, 0],
8
['5', 11, 67, 21, 0, 0],
9
['6', 12, 67, 20, 1, 0],
10
['7', 8, 79, 13, 1, 0],
11
['8', 22, 59, 15, 2, 1],
12
['8a', 38, 17, 38, 3, 3],
13
['8b', 27, 54, 9, 0, 9],
14
['9', 19, 49, 30, 0, 2],
15
['11', 7, 67, 24, 1, 0],
16
['12', 2, 80, 16, 1, 1],
17
['13', 2, 80, 16, 1, 1],
18
['15', 7, 64, 25, 2, 1],
19
['16', 11, 71, 16, 0, 1],
20
['17', 5, 67, 25, 0, 2],
21
['18', 13, 63, 20, 2, 1],
22
['20', 1, 73, 23, 2, 0],
23
['21', 9, 57, 34, 0, 0],
24
['24', 13, 54, 31, 1, 0],
25
['25', 16, 66, 16, 0, 1],
26
]);
27
28
// map data for the first series, take x from the zero column and value from the first column of data set
29
var seriesData_1 = dataSet.mapAs({'x': 0, 'value': 1});
30
31
// map data for the second series, take x from the zero column and value from the second column of data set
32
var seriesData_2 = dataSet.mapAs({'x': 0, 'value': 2});
33
34
// map data for the second series, take x from the zero column and value from the third column of data set
35
var seriesData_3 = dataSet.mapAs({'x': 0, 'value': 3});
36
37
// map data for the fourth series, take x from the zero column and value from the fourth column of data set
38
var seriesData_4 = dataSet.mapAs({'x': 0, 'value': 4});
39
40
// map data for the fourth series, take x from the zero column and value from the fourth column of data set
41
var seriesData_5 = dataSet.mapAs({'x': 0, 'value': 5});
42
43
// create bar chart
44
var chart = anychart.bar();
45
46
// turn on chart animation
47
chart.animation(true);
48
49
// force chart to stack values by Y scale.
50
chart.yScale().stackMode('percent');
51
52
// set container id for the chart
53
chart.container('container');
54
55
// set yAxis labels formatting, force it to add % to values
56
chart.yAxis(0).labels().format(function (info) {
57
return info.value + '%';
58
});
59
60
// helper function to setup label settings for all series
61
var setupSeriesLabels = function (series, name) {
62
series.name(name);
63
series.tooltip().valuePrefix('%');
64
series.stroke('3 #fff 1');
65
series.hovered().stroke('3 #fff 1');
66
};
67
68
// temp variable to store series instance
69
var series;
70
71
// change color
72
chart.palette(anychart.palettes.defaultPalette);
73
74
// create first series with mapped data
75
series = chart.bar(seriesData_1).fill("green");
76
setupSeriesLabels(series, 'Oui');
77
78
79
// create second series with mapped data
80
series = chart.bar(seriesData_2).fill("red");
81
setupSeriesLabels(series, 'Non');
82
83
// create third series with mapped data
84
series = chart.bar(seriesData_3).fill("gold");
85
setupSeriesLabels(series, 'Pas certain');
86
87
// create fourth series with mapped data
88
series = chart.bar(seriesData_4).fill("grey");
89
setupSeriesLabels(series, 'Sans objet');
90
91
// create fifth series with mapped data
92
series = chart.bar(seriesData_5).fill("black");
93
setupSeriesLabels(series, 'Sans réponse');
94
95
// turn on legend
96
chart.legend().enabled(true).fontSize(14).padding([0, 0, 15, 0]);
97
chart.interactivity().hoverMode('by-x');
98
chart.tooltip().displayMode('union');
99
// initiate chart drawing
100
chart.draw();
101
});