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
3
var dataSet = anychart.data.set([
4
['Q2 2014', 17.982, 10.941, 9.835, 4.047, 2.841],
5
['Q3 2014', 17.574, 8.659, 6.23, 2.627, 2.242],
6
['Q4 2014', 19.75, 10.35, 6.292, 3.595, 2.136],
7
['Q1 2015', 30.6, 17.2, 16.1, 5.4, 5.2],
8
['Q2 2015', 21.316, 12.204, 16.823, 3.457, 4.21],
9
['Q3 2015', 20.209, 10.342, 13.23, 2.872, 2.959],
10
['Q4 2015', 21.773, 10.577, 12.518, 3.929, 2.704],
11
['Q1 2016', 29.3, 17.9, 18.4, 4.8, 5.4]
12
]);
13
14
// map data for the first series, take x from the zero area and value from the first area of data set
15
var firstSeriesData = dataSet.mapAs({ x: 0, value: 1 });
16
17
// map data for the second series, take x from the zero area and value from the second area of data set
18
var secondSeriesData = dataSet.mapAs({ x: 0, value: 2 });
19
20
// map data for the second series, take x from the zero area and value from the third area of data set
21
var thirdSeriesData = dataSet.mapAs({ x: 0, value: 3 });
22
23
// map data for the fourth series, take x from the zero area and value from the fourth area of data set
24
var fourthSeriesData = dataSet.mapAs({ x: 0, value: 4 });
25
26
// map data for the fifth series, take x from the zero area and value from the fourth area of data set
27
var fifthSeriesData = dataSet.mapAs({ x: 0, value: 5 });
28
29
// create bar chart
30
var chart = anychart.area();
31
32
// turn on chart animation
33
chart.animation(true);
34
35
// turn on the crosshair
36
var crosshair = chart.crosshair();
37
crosshair.enabled(true).yStroke(null).xStroke('#fff').zIndex(39);
38
crosshair.yLabel().enabled(false);
39
40
// force chart to stack values by Y scale.
41
chart.yScale().stackMode('value');
42
43
// set chart title text settings
44
chart.title('Unaudited Apple Inc. Revenue by Operating Segments');
45
46
// helper function to setup label settings for all series
47
var setupSeriesLabels = function (series, name) {
48
series
49
.name(name)
50
.stroke('3 #fff 1')
51
.fill(function () {
52
return this.sourceColor + ' 0.8';
53
});
54
series.hovered().stroke('3 #fff 1');
55
series
56
.hovered()
57
.markers()
58
.enabled(true)
59
.type('circle')
60
.size(4)
61
.stroke('1.5 #fff');
62
series.markers().zIndex(100);
63
};
64
65
// temp variable to store series instance
66
var series;
67
68
// create first series with mapped data
69
series = chart.area(firstSeriesData);
70
setupSeriesLabels(series, 'Americas');
71
72
// create second series with mapped data
73
series = chart.area(secondSeriesData);
74
setupSeriesLabels(series, 'Europe');
75
76
// create third series with mapped data
77
series = chart.area(thirdSeriesData);
78
setupSeriesLabels(series, 'Greater China');
79
80
// create fourth series with mapped data
81
series = chart.area(fourthSeriesData);
82
setupSeriesLabels(series, 'Japan');
83
84
// create fourth series with mapped data
85
series = chart.area(fifthSeriesData);
86
setupSeriesLabels(series, 'Rest of Asia Pacific');
87
88
// turn on legend
89
chart.legend().enabled(true).fontSize(13).padding([0, 0, 20, 0]);
90
91
// set titles for axises
92
chart.xAxis().title(false);
93
chart.yAxis().title('Revenue (in Billons USD)');
94
95
// interactivity and tooltip settings
96
chart.interactivity().hoverMode('by-x');
97
chart
98
.tooltip()
99
.valuePrefix('$')
100
.valuePostfix(' bln.')
101
.displayMode('union');
102
103
// set container id for the chart
104
chart.container('container');
105
106
// initiate chart drawing
107
chart.draw();
108
});