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 column chart
3
var chart = anychart.column();
4
5
var data = [
6
['Rouge', '80540'],
7
['Foundation', '94190'],
8
['Mascara', '102610'],
9
['Lip gloss', '110430'],
10
['Lipstick', '128000'],
11
['Nail polish', '143760'],
12
['Eyebrow pencil', '170670'],
13
['Eyeliner', '213210'],
14
['Eyeshadows', '249980']
15
];
16
17
// set chart padding
18
chart.padding().top(10);
19
20
// set chart title text settings
21
chart.title().enabled(true).text('Top 10 Cosmetic Products by Revenue');
22
23
// enable no data label
24
chart.noData().label(true);
25
26
// create area series with passed data
27
var series = chart.column(data);
28
29
// set series tooltip settings
30
series.tooltip().titleFormat('{%X}');
31
32
series
33
.tooltip()
34
.position('center-top')
35
.anchor('center-bottom')
36
.offsetX(0)
37
.offsetY(5)
38
.format('${%Value}{groupsSeparator: }');
39
40
// set scale minimum
41
chart.yScale().minimum(0);
42
43
// set yAxis labels formatter
44
chart.yAxis().labels().format('${%Value}{groupsSeparator: }');
45
46
// tooltips position and interactivity settings
47
chart.tooltip().positionMode('point');
48
chart.interactivity().hoverMode('by-x');
49
50
// axes titles
51
chart.xAxis().title('Product');
52
chart.yAxis().title('Revenue');
53
54
// set container id for the chart
55
chart.container('container');
56
57
// initiate chart drawing
58
chart.draw();
59
60
// add button to container
61
$('#container').append(
62
'<button class="switch-btn switch-btn-primary" id="switch-data-btn">Switch Data</button>'
63
);
64
65
// event to switch data
66
$('#switch-data-btn').on('click', function () {
67
// if data rows === 0 then empty data and show label
68
series.data().getRowsCount() ? series.data(null) : series.data(data); // eslint-disable-line no-unused-expressions
69
});
70
});