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
// create data set on our data
3
var dataSet = anychart.data.set([
4
5
['#01', 0, 40, 20, 40, 0],
6
['#02', 0, 60, 0, 40, 0],
7
['#03', 0, 40, 40, 0, 20],
8
['#04', 0, 60, 20, 20, 0],
9
['#05', 0, 40, 40, 20, 0],
10
['#06', 0, 40, 40, 20, 0],
11
['#07', 0, 60, 20, 20, 0],
12
['#08', 40, 0, 40, 20, 0],
13
['#09', 0, 20, 0, 40, 40],
14
['#10', 0, 20, 40, 40, 0],
15
['#11', 20, 60, 0, 20, 0],
16
['#12', 0, 40, 20, 40, 0],
17
['#13', 0, 80, 0, 0, 20],
18
['#14', 0, 40, 40, 20, 0],
19
['#15', 0, 40, 40, 20, 0],
20
['#16', 0, 60, 20, 20, 0],
21
['#17', 0, 40, 0, 20, 40]
22
]);
23
24
// map data for the first series, take x from the zero column and value from the first column of data set
25
var firstSeriesData = dataSet.mapAs({ x: 0, value: 1 });
26
27
// map data for the second series, take x from the zero column and value from the second column of data set
28
var secondSeriesData = dataSet.mapAs({ x: 0, value: 2 });
29
30
// map data for the second series, take x from the zero column and value from the third column of data set
31
var thirdSeriesData = dataSet.mapAs({ x: 0, value: 3 });
32
33
// map data for the fourth series, take x from the zero column and value from the fourth column of data set
34
var fourthSeriesData = dataSet.mapAs({ x: 0, value: 4 });
35
36
// map data for the fourth series, take x from the zero column and value from the fifth column of data set
37
var fifthSeriesData = dataSet.mapAs({ x: 0, value: 5 });
38
39
// create bar chart
40
var chart = anychart.bar();
41
42
// turn on chart animation
43
chart.animation(true);
44
45
// force chart to stack values by Y scale.
46
chart.yScale().stackMode('percent');
47
48
// set chart title text settings
49
chart.title('DIMENSION 1');
50
51
// set yAxis labels formatting, force it to add % to values
52
chart.yAxis(0).labels().format('{%Value}%');
53
54
// helper function to setup label settings for all series, including color
55
var setupSeriesLabels = function (series, name, color) {
56
series.name(name).stroke('3 #fff 1').fill(color); // Set series fill color
57
series.hovered().stroke('3 #fff 1').fill(anychart.color.darken(color)); // Darken color on hover
58
};
59
60
// temp variable to store series instance
61
var series;
62
63
// create first series with mapped data and assign color red
64
series = chart.bar(firstSeriesData);
65
setupSeriesLabels(series, 'No answer', 'red');
66
67
// create second series with mapped data and assign color orange
68
series = chart.bar(secondSeriesData);
69
setupSeriesLabels(series, 'Level 1', 'orange');
70
71
// create third series with mapped data and assign color yellow
72
series = chart.bar(thirdSeriesData);
73
setupSeriesLabels(series, 'Level 2', 'yellow');
74
75
// create fourth series with mapped data and assign color green
76
series = chart.bar(fourthSeriesData);
77
setupSeriesLabels(series, 'Level 3', 'green');
78
79
// create fifth series with mapped data and assign color blue
80
series = chart.bar(fifthSeriesData);
81
setupSeriesLabels(series, 'Level 4', 'blue');
82
83
// turn on legend
84
chart.legend().enabled(true).fontSize(14).padding([0, 0, 10, 0]);
85
86
chart.interactivity().hoverMode('by-x');
87
88
chart.tooltip().displayMode('union').valuePrefix('%');
89
90
// set container id for the chart
91
chart.container('container');
92
// initiate chart drawing
93
chart.draw();
94
});