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