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
['Powder', 11861, 10919, 8034, 18012],
5
['Mascara', 11261, 10419, 6134, 18712],
6
['Lip gloss', 22998, 12043, 4572, 4008],
7
['Foundation', 10342, 10119, 5231, 13701],
8
['Eyeliner', 12321, 15067, 3417, 5432],
9
['Eyeshadows', 12998, 12043, 4572, 3308],
10
['Lipstick', 8814, 9054, 4376, 9256],
11
['Rouge', 11624, 7004, 3574, 5221],
12
['Eyebrow pencil', 13012, 5067, 3987, 3932],
13
['Nail polish', 12814, 3054, 4376, 4229]
14
]);
15
16
// map data for the first series, take x from the zero column and value from the first column of data set
17
var firstSeriesData = dataSet.mapAs({ x: 0, value: 1 });
18
19
// map data for the second series, take x from the zero column and value from the second column of data set
20
var secondSeriesData = dataSet.mapAs({ x: 0, value: 2 });
21
22
// map data for the third series, take x from the zero column and value from the third column of data set
23
var thirdSeriesData = dataSet.mapAs({ x: 0, value: 3 });
24
25
// map data for the fourth series, take x from the zero column and value from the fourth column of data set
26
var fourthSeriesData = dataSet.mapAs({ x: 0, value: 4 });
27
28
// create bar chart
29
var chart = anychart.bar();
30
31
// turn on chart animation
32
chart.animation(true);
33
34
chart.padding([10, 40, 5, 20]);
35
36
// set chart title text settings
37
chart.title('Top 10 Products by Revenue');
38
39
// set scale minimum
40
chart.yScale().minimum(0);
41
42
// force chart to stack values by Y scale.
43
chart.yScale().stackMode('value');
44
chart.yAxis().labels().format('{%Value}{groupsSeparator: }');
45
46
// set titles for axises
47
chart.xAxis().title('Products');
48
chart.yAxis().title('Revenue in Dollars');
49
50
// helper function to setup label settings for all series
51
var setupSeries = function (series, name) {
52
series.name(name);
53
series.stroke('3 #fff 1');
54
series.hovered().stroke('3 #fff 1');
55
};
56
57
// temp variable to store series instance
58
var series;
59
60
// create first series with mapped data
61
series = chart.bar(firstSeriesData);
62
setupSeries(series, 'Florida');
63
64
// create second series with mapped data
65
series = chart.bar(secondSeriesData);
66
setupSeries(series, 'Texas');
67
68
// create third series with mapped data
69
series = chart.bar(thirdSeriesData);
70
setupSeries(series, 'Arizona');
71
72
// create fourth series with mapped data
73
series = chart.bar(fourthSeriesData);
74
setupSeries(series, 'Nevada');
75
76
// turn on legend
77
chart.legend().enabled(true).fontSize(13).padding([0, 0, 20, 0]);
78
79
chart.interactivity().hoverMode('by-x');
80
chart.tooltip().valuePrefix('$').displayMode('union');
81
82
// set container id for the chart
83
chart.container('container');
84
// initiate chart drawing
85
chart.draw();
86
});