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 column chart
3
var chart = anychart.bar();
4
5
// data for the sample (completely fiction)
6
var surveyData = [
7
[
8
'Formation and evolution<br/>of the Solar System',
9
70,
10
145,
11
79,
12
416,
13
290
14
],
15
[
16
'Giant impact hypothesis<br/>(Earth\'s Moon formation)',
17
232,
18
305,
19
20,
20
306,
21
137
22
],
23
['Origin of water on Earth', 120, 545, 86, 156, 93],
24
['Conception of Darwinism', 203, 304, 92, 311, 90],
25
['Linnaean classification<br/>of organisms', 110, 144, 69, 321, 356],
26
['Historical materialism<br/>by Karl Marx', 100, 447, 39, 119, 295]
27
];
28
29
// helper function to create series and customize them
30
var createSeries = function (columnNumber, name) {
31
var data = [];
32
for (var i = 0; i < surveyData.length; i++) {
33
var value = surveyData[i][columnNumber] / 10;
34
var center = surveyData[i][3] / 10 / 2;
35
if (name === 'Don\'t care') {
36
data.push({
37
x: surveyData[i][0],
38
low: -value / 2,
39
high: value / 2,
40
value: value
41
});
42
} else if (name === 'Not completely agree') {
43
data.push({
44
x: surveyData[i][0],
45
low: center,
46
high: center + value,
47
value: value
48
});
49
} else if (name === 'Agree') {
50
data.push({
51
x: surveyData[i][0],
52
low: center + surveyData[i][4] / 10,
53
high: center + surveyData[i][4] / 10 + value,
54
value: value
55
});
56
} else if (name === 'Not completely disagree') {
57
data.push({
58
x: surveyData[i][0],
59
low: -center,
60
high: -center - value,
61
value: value
62
});
63
} else if (name === 'Disagree') {
64
data.push({
65
x: surveyData[i][0],
66
low: -center - surveyData[i][2] / 10,
67
high: -center - surveyData[i][2] / 10 - value,
68
value: value
69
});
70
}
71
}
72
var series = chart.rangeBar(data);
73
series.name(name).stroke('3 #fff 1').selectionMode('none');
74
series.hovered().stroke('3 #fff 1');
75
series.tooltip().title().useHtml(true).fontSize(12);
76
};
77
78
// creates series
79
createSeries(1, 'Disagree');
80
createSeries(2, 'Not completely disagree');
81
createSeries(3, 'Don\'t care');
82
createSeries(4, 'Not completely agree');
83
createSeries(5, 'Agree');
84
85
// sets title for map chart and customizes it
86
chart
87
.title()
88
.enabled(true)
89
.useHtml(true)
90
.padding([0, 0, 10, 0])
91
.text(
92
'Diverging Chart<br/>' +
93
'<span style="color:#212121; font-size: 13px;">(According to survey 1000 in ACME corp.)</span>'
94
);
95
96
// changes palette for this sample
97
chart.palette(
98
anychart.palettes
99
.distinctColors()
100
.items(['#ef6c00', '#ffa000', '#96a6a6', '#42a5f5', '#1976d2'])
101
);
102
chart
103
.legend()
104
.enabled(true)
105
.padding([20, 0, 10, 0])
106
.position('center-bottom');
107
108
// creates stack bar chart from multi series chart
109
chart.yScale().stackMode('value');
110
111
// changes settings for axis
112
chart.xAxis().ticks(false);
113
chart
114
.xAxis()
115
.title()
116
.enabled(true)
117
.text('Theories')
118
.padding([0, 0, 10, 0]);
119
chart
120
.xAxis()
121
.labels()
122
.useHtml(true)
123
.hAlign('right')
124
.fontSize(11)
125
.fontColor('#474747')
126
.padding([0, 10, 0, 0]);
127
chart.yAxis(1).orientation('top');
128
chart
129
.yAxis(0)
130
.labels()
131
.format(function () {
132
return Math.abs(this.value) + '%';
133
});
134
135
chart
136
.tooltip()
137
.useHtml(true)
138
.fontSize(12)
139
.titleFormat(function () {
140
return (
141
this.getData('value') * 10 +
142
' people ' +
143
'<span style="font-size: 13px; color: #cbcbcb">(' +
144
this.getData('value') +
145
'%)</span>'
146
);
147
})
148
.format(function () {
149
if (this.seriesName === 'Don\'t care') {
150
return (
151
this.seriesName +
152
' about <br/><span style="color: #cbcbcb">' +
153
this.getData('x') +
154
'</span>'
155
);
156
}
157
return (
158
this.seriesName +
159
' with <br/><span style="color: #cbcbcb">' +
160
this.getData('x') +
161
'</span>'
162
);
163
});
164
165
// creates line marker at 0
166
chart.lineMarker().value(0).stroke('#CECECE');
167
168
// set container id for the chart
169
chart.container('container');
170
// initiate chart drawing
171
chart.draw();
172
});