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
3
// create a data set
4
var data = anychart.data.set([
5
["John", 1300, 1000, 1800],
6
["Larry", 1500, 1200, 2100],
7
["Marta", 1800, 1100, 1300]
8
]);
9
10
// map the data
11
var seriesData1 = data.mapAs({x: 0, value: 1});
12
var seriesData2 = data.mapAs({x: 0, value: 2});
13
var seriesData3 = data.mapAs({x: 0, value: 3});
14
15
// create a chart
16
var chart = anychart.column();
17
18
// create series, set the data and names
19
var series1 = chart.column(seriesData1);
20
var series2 = chart.column(seriesData2);
21
var series3 = chart.column(seriesData3);
22
series1.name("2016");
23
series2.name("2017");
24
series3.name("2018");
25
26
// enable the legend
27
var legend = chart.legend();
28
legend.enabled(true);
29
30
// set the layout of the legend
31
legend.itemsLayout("vertical");
32
33
// set the position of the legend
34
legend.position("right");
35
36
// create an array for storing legend items
37
var legendItems = [];
38
39
// push legend items to the array
40
for (var i = 0; i < chart.getSeriesCount(); i++) {
41
var series = chart.getSeriesAt(i);
42
for (var k = 0; k < series.data().getRowsCount(); k++) {
43
legendItems.push({
44
text: series.name() + ": " + data.data()[k][0] +
45
" - $" + data.data()[k][i + 1],
46
iconType: "square",
47
iconFill: series.color()
48
});
49
}
50
}
51
52
// add custom legend items
53
legend.items(legendItems);
54
55
// set the chart title
56
chart.title("Custom Legend Items");
57
58
// set the container id
59
chart.container("container");
60
61
// initiate drawing the chart
62
chart.draw();
63
});