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(getData());
4
5
// map data for the first series, take x from the zero area and value from the first area of data set
6
var firstSeriesData = dataSet.mapAs({ x: 0, value: 1 });
7
8
// map data for the second series, take x from the zero area and value from the second area of data set
9
var secondSeriesData = dataSet.mapAs({ x: 0, value: 2 });
10
11
// map data for the third series, take x from the zero area and value from the third area of data set
12
var thirdSeriesData = dataSet.mapAs({ x: 0, value: 3 });
13
14
// map data for the third series, take x from the zero area and value from the third area of data set
15
var fourthSeriesData = dataSet.mapAs({ x: 0, value: 4 });
16
17
// create area chart
18
var chart = anychart.area();
19
20
// turn on chart animation
21
chart.animation(true);
22
23
// turn on the crosshair
24
var crosshair = chart.crosshair();
25
crosshair.enabled(true).yLabel(false).yStroke(null).xStroke('#fff');
26
27
// set chart title text settings
28
chart.title(
29
'Company Profit Trends in Census Bureau-designated regions'
30
);
31
32
// set interactivity and tooltips settings
33
chart.interactivity().hoverMode('by-x');
34
chart.tooltip().displayMode('union');
35
36
// set Y axis title
37
chart.yAxis(0).title('Profit');
38
39
// set custom formatter for Y axis labels
40
chart.yAxis(0).labels().format('{%Value}k.');
41
42
// create additional xAxis to place category labels on top
43
chart.xAxis(1).orientation('top');
44
45
// create additional xAxis to place category labels on top
46
chart.yAxis(1).orientation('right').labels(false);
47
48
chart.tooltip().format('{%SeriesName}: {%Value}k');
49
50
// helper function to setup label settings for all series
51
var setupSeries = function (series, name) {
52
series.name(name);
53
series.markers().zIndex(100);
54
series
55
.hovered()
56
.markers()
57
.enabled(true)
58
.type('circle')
59
.size(4)
60
.stroke('1.5 #fff');
61
};
62
63
// temp variable to store series instance
64
var series;
65
66
// create first series with mapped data
67
series = chart.area(firstSeriesData);
68
setupSeries(series, 'Northeast');
69
70
// create second series with mapped data
71
series = chart.area(secondSeriesData);
72
setupSeries(series, 'Midwest');
73
74
// create third series with mapped data
75
series = chart.area(thirdSeriesData);
76
setupSeries(series, 'South');
77
78
// create fourth series with mapped data
79
series = chart.area(fourthSeriesData);
80
setupSeries(series, 'West');
81
82
// turn on legend
83
chart.legend().enabled(true).fontSize(13).padding([0, 0, 20, 0]);
84
85
// enable grids
86
chart.yGrid().stroke('#ddd');
87
chart.xGrid().stroke('#ddd');
88
89
// set container id for the chart
90
chart.container('container');
91
92
// initiate chart drawing
93
chart.draw();
94
});
95
96
function getData() {
97
return [
98
['1996', 322, 242, 142, 162],
99
['1997', 324, 254, 154, 90],
100
['1998', 329, 226, 126, 50],
101
['1999', 342, 232, 132, 77],
102
['2000', 348, 268, 168, 35],
103
['2001', 334, 254, 154, -45],
104
['2002', 325, 235, 135, -88],
105
['2003', 316, 266, 166, -120],
106
['2004', 318, 288, 188, -156],
107
['2005', 330, 220, 120, -123],
108
['2006', 355, 215, 115, -88],
109
['2007', 366, 236, 136, -66],
110
['2008', 337, 247, 147, -45],
111
['2009', 352, 172, 72, -29],
112
['2010', 377, 37, 7, -45],
113
['2011', 383, 23, 3, -88],
114
['2012', 344, 34, 4, -132],
115
['2013', 366, 46, 6, -146],
116
['2014', 389, 59, 9, -169],
117
['2015', 334, 44, 4, -184]
118
];
119
}