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', 6814, 3054, 4376, 4229],
5
['Eyebrow pencil', 7012, 5067, 8987, 3932],
6
['Lipstick', 8814, 9054, 4376, 9256]
7
]);
8
9
// map data for the first series, take x from the zero column and value from the first column of data set
10
var firstSeriesData = dataSet.mapAs({ x: 0, value: 1 });
11
12
// map data for the second series, take x from the zero column and value from the second column of data set
13
var secondSeriesData = dataSet.mapAs({ x: 0, value: 2 });
14
15
// map data for the third series, take x from the zero column and value from the third column of data set
16
var thirdSeriesData = dataSet.mapAs({ x: 0, value: 3 });
17
18
// map data for the fourth series, take x from the zero column and value from the fourth column of data set
19
var fourthSeriesData = dataSet.mapAs({ x: 0, value: 4 });
20
21
// create bar chart
22
var chart = anychart.bar();
23
24
// turn on chart animation
25
chart.animation(true);
26
27
chart.padding([10, 40, 5, 20]);
28
29
// set chart title text settings
30
chart.title('Top 3 Products with Region Sales Data');
31
32
// set scale minimum
33
chart.yScale().minimum(0);
34
35
chart.xAxis().labels().rotation(-90).padding([0, 0, 20, 0]);
36
37
chart.yAxis().labels().format('{%Value}{groupsSeparator: }');
38
39
// set titles for Y-axis
40
chart.yAxis().title('Revenue in Dollars');
41
42
// helper function to setup settings for series
43
var setupSeries = function (series, name) {
44
series.name(name);
45
series.hovered().labels(false);
46
47
series
48
.labels()
49
.enabled(true)
50
.position('right-center')
51
.anchor('left-center')
52
.format('${%Value}{groupsSeparator: }');
53
54
series
55
.tooltip()
56
.position('right')
57
.anchor('left-center')
58
.offsetX(5)
59
.offsetY(0)
60
.titleFormat('{%X}')
61
.format('{%SeriesName} : ${%Value}{groupsSeparator: }');
62
};
63
64
// temp variable to store series instance
65
var series;
66
67
// create first series with mapped data
68
series = chart.bar(firstSeriesData);
69
setupSeries(series, 'Florida');
70
71
// create second series with mapped data
72
series = chart.bar(secondSeriesData);
73
setupSeries(series, 'Texas');
74
75
// create third series with mapped data
76
series = chart.bar(thirdSeriesData);
77
setupSeries(series, 'Arizona');
78
79
// create fourth series with mapped data
80
series = chart.bar(fourthSeriesData);
81
setupSeries(series, 'Nevada');
82
83
// turn on legend
84
chart.legend().enabled(true).fontSize(13).padding([0, 0, 20, 0]);
85
86
chart.interactivity().hoverMode('single');
87
chart.tooltip().positionMode('point');
88
89
// set container id for the chart
90
chart.container('container');
91
// initiate chart drawing
92
chart.draw();
93
});