HTMLcopy
1
<script src="https://cdn.anychart.com/releases/8.10.0/js/anychart-base.min.js"></script>
2
<div id="container"></div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create a data set on our data
4
var dataSet = anychart.data.set(getData());
5
6
// map data for the first series,
7
// take x from the zero column and value from the first column
8
var firstSeriesData = dataSet.mapAs({ x: 0, value: 1 });
9
10
// map data for the second series,
11
// take x from the zero column and value from the second column
12
var secondSeriesData = dataSet.mapAs({ x: 0, value: 2 });
13
14
// map data for the third series,
15
// take x from the zero column and value from the third column
16
var thirdSeriesData = dataSet.mapAs({ x: 0, value: 3 });
17
18
// map data for the fourth series,
19
// take x from the zero column and value from the fourth column
20
var fourthSeriesData = dataSet.mapAs({ x: 0, value: 4 });
21
22
// create a line chart
23
var chart = anychart.line();
24
25
// turn on the line chart animation
26
chart.animation(true);
27
28
// configure the chart title text settings
29
chart.title('Acceptance of same-sex relationships in the US over the last 2 decades');
30
31
// set the y axis title
32
chart.yAxis().title('% of people who accept same-sex relationships');
33
34
// turn on the crosshair
35
chart.crosshair().enabled(true).yLabel(false).yStroke(null);
36
37
// create the first series with the mapped data
38
var firstSeries = chart.line(firstSeriesData);
39
firstSeries
40
.name('18-34')
41
.stroke('3 #f49595')
42
.tooltip()
43
.format("Age group 18-34 : {%value}%");
44
45
// create the second series with the mapped data
46
var secondSeries = chart.line(secondSeriesData);
47
secondSeries
48
.name('35-49')
49
.stroke('3 #f9eb97')
50
.tooltip()
51
.format("Age group 35-49 : {%value}%");
52
53
// create the third series with the mapped data
54
var thirdSeries = chart.line(thirdSeriesData);
55
thirdSeries
56
.name('50-64')
57
.stroke('3 #a8d9f6')
58
.tooltip()
59
.format("Age group 50-64 : {%value}%");
60
61
// create the fourth series with the mapped data
62
var fourthSeries = chart.line(fourthSeriesData);
63
fourthSeries
64
.name('65+')
65
.stroke('3 #e2bbfd')
66
.tooltip()
67
.format("Age group 65+ : {%value}%");
68
69
// turn the legend on
70
chart.legend().enabled(true);
71
72
// set the container id for the line chart
73
chart.container('container');
74
75
// draw the line chart
76
chart.draw();
77
78
});
79
80
function getData() {
81
return [
82
['1990',16.9,12.2,10.2,5.2],
83
['1991',17,17.8,10,4.8],
84
['1993',26.5,23.8,16.8,6.6],
85
['1994',28.7,22,17.3,9.1],
86
['1996',35.7,24,22.6,9.2],
87
['1998',37.2,24.6,22.4,11.2],
88
['2000',36.5,26.2,23.7,9.9],
89
['2002',40,34.4,23.8,16.4],
90
['2004',33.3,28.8,32.5,14.3],
91
['2006',40.2,32.1,27.5,15.1],
92
['2008',49.3,37.2,31.4,17.1],
93
['2010',51.9,42.5,36.1,28.5],
94
['2012',53.1,43.8,36,24.6],
95
['2014',63.7,45.9,44.7,31.3],
96
['2016',66.3,52,42.3,37.2],
97
['2018',70.1,57.7,49.2,39]
98
];
99
}