HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
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 seriesData_1 = 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 seriesData_2 = 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 seriesData_3 = 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 seriesData_4 = 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)
44
.stroke('3 #fff 1');
45
series.hovered().stroke('3 #fff 1');
46
};
47
48
// temp variable to store series instance
49
var series;
50
51
// create first series with mapped data
52
series = chart.column(seriesData_1);
53
setupSeriesLabels(series, 'Florida');
54
55
// create second series with mapped data
56
series = chart.column(seriesData_2);
57
setupSeriesLabels(series, 'Texas');
58
59
// create third series with mapped data
60
series = chart.column(seriesData_3);
61
setupSeriesLabels(series, 'Arizona');
62
63
// create fourth series with mapped data
64
series = chart.column(seriesData_4);
65
setupSeriesLabels(series, 'Nevada');
66
67
// turn on legend
68
chart.legend().enabled(true).fontSize(13).padding([0, 0, 20, 0]);
69
// set yAxis labels formatter
70
chart.yAxis().labels().format('{%Value}{groupsSeparator: }');
71
72
// set titles for axes
73
chart.xAxis().title('Products by Revenue');
74
chart.yAxis().title('Revenue in Dollars');
75
76
// set interactivity hover
77
chart.interactivity().hoverMode('by-x');
78
79
chart.tooltip()
80
.valuePrefix('$')
81
.displayMode('union');
82
83
// show average category labels
84
var categoryLabels = chart.getSeries(0).labels().enabled(true);
85
categoryLabels.format("{%categoryYAverage}");
86
87
// set container id for the chart
88
chart.container('container');
89
90
// initiate chart drawing
91
chart.draw();
92
});