HTMLcopy
1
<label><input onclick="legendPositionMode('outside')" type="radio" name="mode">Outside</label>
2
<label><input onclick="legendPositionMode('inside')" type="radio" name="mode" checked>Inside</label>
3
<div id="container"></div>
CSScopy
16
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
label {
8
display: inline-block;
9
margin: 10px 0 0 10px;
10
}
11
#container {
12
position: absolute;
13
width: 100%;
14
top: 35px;
15
bottom: 0;
16
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create a data set
4
var data = anychart.data.set([
5
["January", 9000, 10000, 7000, 11000],
6
["February", 15000, 12000, 9000, 13000],
7
["March", 16000, 15000, 14000, 12000],
8
["April", 15000, 11000, 13000, 10000],
9
["May", 10000, 9000, 11000, 6000]
10
]);
11
12
// map the data
13
var seriesData1 = data.mapAs({x: 0, value: 1});
14
var seriesData2 = data.mapAs({x: 0, value: 2});
15
var seriesData3 = data.mapAs({x: 0, value: 3});
16
var seriesData4 = data.mapAs({x: 0, value: 4});
17
18
// create a chart
19
chart = anychart.line();
20
21
// create series, set the data and names
22
var series1 = chart.line(seriesData1);
23
var series2 = chart.line(seriesData2);
24
var series3 = chart.line(seriesData3);
25
var series4 = chart.line(seriesData4);
26
series1.name("2015");
27
series2.name("2016");
28
series3.name("2017");
29
series4.name("2018");
30
31
// set the background of the data area
32
chart.dataArea().background().enabled(true);
33
chart.dataArea().background().fill("#96a6a6 0.3");
34
35
// enable the legend
36
legend = chart.legend();
37
legend.enabled(true);
38
39
// set the layout of the legend
40
legend.itemsLayout("vertical");
41
42
// set the position mode of the legend
43
legend.positionMode("inside");
44
45
// set the position of the legend
46
legend.position("right");
47
48
// set the alignment of the legend
49
legend.align("top");
50
51
// set the chart title
52
chart.listen("chartDraw", function () {
53
chart.title("Legend: Position Mode = " +
54
chart.legend().positionMode());
55
});
56
57
// set the container id
58
chart.container("container");
59
60
// initiate drawing the chart
61
chart.draw();
62
});
63
// set the position mode of the legend
64
function legendPositionMode(mode) {
65
legend.positionMode(mode);
66
}