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 for the first chart
7
var data1 = anychart.data.set([
8
["Q 1", 10000, 12000, 16000],
9
["Q 2", 12000, 15000, 17000],
10
["Q 3", 20000, 19000, 21000],
11
["Q 4", 16000, 21000, 22000]
12
]);
13
14
// create a data set for the second chart
15
var data2 = anychart.data.set([
16
["Q 1", 13000, 15000, 18000],
17
["Q 2", 10000, 12000, 11000],
18
["Q 3", 18000, 21000, 13000],
19
["Q 4", 21000, 20000, 22000]
20
]);
21
22
// create and configure the first chart
23
var chart1 = anychart.line();
24
chart1.spline(data1.mapAs({x: 0, value: 1})).name("John");
25
chart1.spline(data1.mapAs({x: 0, value: 2})).name("Larry");
26
chart1.spline(data1.mapAs({x: 0, value: 3})).name("Marta");
27
chart1.bounds(0, "10%", "50%", "90%");
28
chart1.title("2017");
29
// set the chart container
30
chart1.container(stage);
31
// initiate drawing the chart
32
chart1.draw();
33
34
// create and configure the second chart
35
var chart2 = anychart.line();
36
chart2.spline(data2.mapAs({x: 0, value: 1})).name("John");
37
chart2.spline(data2.mapAs({x: 0, value: 2})).name("Larry");
38
chart2.spline(data2.mapAs({x: 0, value: 3})).name("Marta");
39
chart2.bounds("50%", "10%", "50%", "90%");
40
chart2.title("2018");
41
// set the chart container
42
chart2.container(stage);
43
// initiate drawing the chart
44
chart2.draw();
45
46
// create a standalone legend
47
var legend = anychart.standalones.legend();
48
49
// create an array for storing legend items
50
var legendItems = [];
51
52
// push legend items to the array
53
for (var i = 0; i < chart1.getSeriesCount(); i++) {
54
var series = chart1.getSeriesAt(i);
55
legendItems.push({
56
text: series.name(),
57
iconType: "spline",
58
iconStroke: {color: series.color(), thickness: 3}
59
});
60
}
61
62
// add items to the legend
63
legend.items(legendItems);
64
65
// set the padding of the legend
66
legend.padding(10);
67
68
/* listen to the legendItemClick event,
69
enable/disable the series on both charts,
70
and configure the appearance of the legend item */
71
legend.listen("legendItemClick", function(e) {
72
var index = e.itemIndex;
73
var series1 = chart1.getSeriesAt(index);
74
var series2 = chart2.getSeriesAt(index);
75
if (series1.enabled()) {
76
series1.enabled(false);
77
series2.enabled(false);
78
legendItems[index].iconStroke = "3 #999999";
79
legendItems[index].fontColor = "#999999";
80
legend.itemsFormatter(function() {return legendItems});
81
} else {
82
series1.enabled(true);
83
series2.enabled(true);
84
legendItems[index].iconStroke = {
85
color: series1.color(),
86
thickness: 3
87
}
88
legendItems[index].fontColor = series1.color();
89
legend.itemsFormatter(function() {return legendItems});
90
}
91
});
92
93
/* listen to the legendItemMouseOver event
94
and configure the appearance of the series on both charts */
95
legend.listen("legendItemMouseOver", function(e) {
96
var series1 = chart1.getSeriesAt(e.itemIndex);
97
var series2 = chart2.getSeriesAt(e.itemIndex);
98
series1.stroke(anychart.color.lighten(series1.color()), 5);
99
series2.stroke(anychart.color.lighten(series2.color()), 5);
100
});
101
102
/* listen to the legendItemMouseOver event
103
and reset the appearance of the series on both charts */
104
legend.listen("legendItemMouseOut", function(e) {
105
var series1 = chart1.getSeriesAt(e.itemIndex);
106
var series2 = chart2.getSeriesAt(e.itemIndex);
107
series1.stroke(series1.color(), 1.5);
108
series2.stroke(series2.color(), 1.5);
109
});
110
111
// set the container for the legend
112
legend.container(stage);
113
114
// draw the legend
115
legend.draw();
116
});