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
// create data set
3
dataSet = anychart.data.set([
4
["1800", 170, 161],
5
["1820", 171, 164],
6
["1840", 174, 168],
7
["1860", 175, 170],
8
["1880", 175, 172],
9
["1900", 180, 174],
10
["1920", 181, 174],
11
["1940", 184, 176],
12
["1960", 185, 180],
13
["1980", 186, 182],
14
["2000", 188, 182]
15
]);
16
17
// map data for the first series
18
data_maleMartians = dataSet.mapAs({x: 0, value: 1});
19
20
// map data for the second series
21
data_femaleMartians = dataSet.mapAs({x: 0, value: 2});
22
23
// create a line chart
24
var chart = anychart.line();
25
26
// create the first series with mapped data
27
maleMartians = chart.line(data_maleMartians);
28
maleMartians.name("Male Martians");
29
maleMartians.color("#398CEA");
30
31
// create the second series with mapped data
32
femaleMartians = chart.line(data_femaleMartians);
33
femaleMartians.name("Female Martians");
34
femaleMartians.color("#E93F39");
35
36
// get the number of points in the series
37
// (in this case, the number of points is the same for the both series)
38
numberOfPoints = maleMartians.getStat("seriesPointCount");
39
40
// get links to the latest points in both series
41
latestPointMaleMartians = maleMartians.getPoint(numberOfPoints - 1);
42
latestPointFemaleMartians = femaleMartians.getPoint(numberOfPoints - 1);
43
44
// get the values of the latest points from both series and use them in the title
45
mainTitleText = "The Height of Martians Today: Males ~ " + latestPointMaleMartians.getStat("value") + ", Females ~ " + latestPointFemaleMartians.getStat("value");
46
47
// set the title of the chart
48
chart.title(mainTitleText);
49
50
// enable HTML in the title
51
chart.title().useHtml(true);
52
53
// listen to the pointsSelect event
54
chart.listen("pointsSelect", function(e){
55
// get categoryName of the selected points
56
selectedPointYear = e.point.getStat("categoryName");
57
// begin creating a subtitle with information about the selected points
58
subtitleText = "<span style='font-size:12'>" + selectedPointYear + ": ";
59
// loop through the array of the selected points
60
for (var i = 0; i < e.points.length; i++) {
61
// get the name of the series a selected point belongs to and the value of the point
62
subtitleText += e.points[i].getSeries().name() + " ~ " + e.points[i].getStat("value") + ", ";
63
}
64
// remove the extra comma at the end of the subtitle and close the <span> tag
65
subtitleText = subtitleText.slice(0, subtitleText.length - 2) + "</span>";
66
// update the title with the subtitle
67
chart.title(mainTitleText + "<br>" + subtitleText);
68
});
69
70
// set the title of the y-axis
71
chart.yAxis().title("Average Height (cm)");
72
73
// turn the legend on
74
chart.legend(true);
75
76
// set the container id for the chart
77
chart.container("container");
78
79
// initiate drawing the chart
80
chart.draw();
81
});