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
// add data
4
var data = [
5
["2003", 1, 0, 0],
6
["2004", 4, 0, 0],
7
["2005", 6, 0, 0],
8
["2006", 9, 1, 0],
9
["2007", 12, 2, 0],
10
["2008", 13, 5, 1],
11
["2009", 15, 6, 1],
12
["2010", 16, 9, 1],
13
["2011", 16, 10, 4],
14
["2012", 17, 11, 5],
15
["2013", 17, 13, 6],
16
["2014", 17, 14, 7],
17
["2015", 17, 14, 10],
18
["2016", 17, 14, 12],
19
["2017", 19, 16, 12],
20
["2018", 20, 17, 14],
21
["2019", 20, 19, 16],
22
["2020", 20, 20, 17],
23
["2021", 20, 20, 20],
24
["2022", 20, 22, 20]
25
];
26
27
// create a data set
28
var dataSet = anychart.data.set(data);
29
30
// map the data for all series
31
var firstSeriesData = dataSet.mapAs({x: 0, value: 1});
32
var secondSeriesData = dataSet.mapAs({x: 0, value: 2});
33
var thirdSeriesData = dataSet.mapAs({x: 0, value: 3});
34
35
// create a line chart
36
var chart = anychart.line();
37
38
// create the series and name them
39
var firstSeries = chart.line(firstSeriesData);
40
firstSeries.name("Roger Federer");
41
var secondSeries = chart.line(secondSeriesData);
42
secondSeries.name("Rafael Nadal");
43
var thirdSeries = chart.line(thirdSeriesData);
44
thirdSeries.name("Novak Djokovic");
45
46
// add a legend and customize it
47
chart.legend().enabled(true).fontSize(14).padding([10, 0, 10, 0]);
48
49
// add a title and customize it
50
chart
51
.title()
52
.enabled(true)
53
.useHtml(true)
54
.text(
55
'<span style="color: #006331; font-size: 20px;">Big Three's Grand Slam Title Race</span>' +
56
'<br/><span style="font-size: 16px;">(Triumphs at Australian Open, French Open, Wimbledon, U.S. Open)</span>'
57
);
58
59
// name the axes
60
chart.yAxis().title("Titles won");
61
chart.xAxis().title("Year");
62
63
// customize the series markers
64
firstSeries.hovered().markers().type("circle").size(4);
65
secondSeries.hovered().markers().type("circle").size(4);
66
thirdSeries.hovered().markers().type("circle").size(4);
67
68
// turn on crosshairs and remove the y hair
69
chart.crosshair().enabled(true).yStroke(null).yLabel(false);
70
71
// change the tooltip position
72
chart.tooltip().positionMode("point");
73
chart.tooltip().position("right").anchor("left-center").offsetX(5).offsetY(5);
74
75
// customize the series stroke in the normal state
76
firstSeries.normal().stroke("#7b60a2", 2.5);
77
secondSeries.normal().stroke("#db7346", 2.5);
78
thirdSeries.normal().stroke("#43a7dc", 2.5);
79
80
// specify where to display the chart
81
chart.container("container");
82
83
// draw the resulting chart
84
chart.draw();
85
86
});