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 stage
4
var stage = anychart.graphics.create("container");
5
6
// create a data set
7
var data = anychart.data.set([
8
["John", 1300, 1000, 1800],
9
["Larry", 1500, 1200, 2100],
10
["Marta", 1800, 1100, 1300]
11
]);
12
13
// create a chart
14
var chart = anychart.column();
15
// create series, set the data and names
16
chart.column(data.mapAs({x: 0, value: 1})).name("2016");
17
chart.column(data.mapAs({x: 0, value: 2})).name("2017");
18
chart.column(data.mapAs({x: 0, value: 3})).name("2018");
19
// set the chart bounds
20
chart.bounds(0, 0, "80%", "100%");
21
// set the container for the chart
22
chart.container(stage);
23
// draw the chart
24
chart.draw();
25
26
/* prevent points from being selected
27
when they are clicked on */
28
chart.interactivity().selectionMode("none");
29
30
/* prevent points from being deselected
31
when a click happens outside of them */
32
chart.interactivity().unselectOnClickOutOfPoint(false);
33
34
// a function for crating legends
35
function createLegend(dataRow, alignment) {
36
37
// create a standalone legend
38
var legend = anychart.standalones.legend();
39
40
// create an array for storing legend items
41
var legendItems = [];
42
43
// get the palette of the chart
44
var palette = chart.palette().items();
45
46
// push legend items to the array
47
for (var i = 1; i < data.data()[dataRow].length; i++) {
48
legendItems.push({
49
text: "$" + data.data()[dataRow][i],
50
iconFill: palette[i-1],
51
});
52
}
53
54
// add items to the legend
55
legend.items(legendItems);
56
57
// set the alignment of the legend
58
legend.align(alignment);
59
60
// set the layout of the legend
61
legend.itemsLayout("vertical");
62
// set the position of the legend
63
legend.position("right");
64
// set the legend height
65
legend.height("30%");
66
// set the legend width
67
legend.width("20%");
68
// set the padding of the legend
69
legend.padding(10, 10, 10, 0);
70
// set the spacing between legend items
71
legend.itemsSpacing(5);
72
// set the legend title
73
legend.title(data.data()[dataRow][0]);
74
legend.title().padding(10);
75
legend.title().orientation("left");
76
77
/* listen to the legendItemClick event,
78
select / deselect the point,
79
and configure the appearance of the legend item */
80
legend.listen("legendItemClick", function(e) {
81
var index = e.itemIndex;
82
var chartPoint = chart.getSeriesAt(index).getPoint(dataRow);
83
if (!chartPoint.selected()) {
84
chartPoint.selected(true);
85
legendItems[index].iconFill = "#455a64";
86
legend.itemsFormatter(function() {return legendItems});
87
} else {
88
chartPoint.selected(false);
89
legendItems[index].iconFill = palette[index];
90
legend.itemsFormatter(function() {return legendItems});
91
}
92
});
93
94
/* listen to the legendItemMouseOver event
95
and enable the hover state of the chart point */
96
legend.listen("legendItemMouseOver", function(e) {
97
var index = e.itemIndex;
98
var point = chart.getSeriesAt(index).getPoint(dataRow);
99
point.hovered(true);
100
/* if the chart point is selected,
101
prevent the default behavior of the legend */
102
if (point.selected()) {e.preventDefault();}
103
});
104
105
/* listen to the legendItemMouseOut event
106
and disable the hover state of the chart point */
107
legend.listen("legendItemMouseOut", function(e) {
108
var index = e.itemIndex;
109
var point = chart.getSeriesAt(index).getPoint(dataRow);
110
point.hovered(false);
111
});
112
113
// set the container for the legend
114
legend.container(stage);
115
116
// draw the legend
117
legend.draw();
118
119
return legend;
120
}
121
122
// create the first standalone legend
123
var legend1 = createLegend(0, "top");
124
125
// create the second standalone legend
126
var legend2 = createLegend(1, "center");
127
128
// create the third standalone legend
129
var legend3 = createLegend(2, "bottom");
130
});