HTMLcopy
1
<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
// create a line chart
3
var chart = anychart.line();
4
5
// add a chart title
6
chart.title('Perceived threat of COVID-19 by political affiliation (March–August 2020)');
7
8
// create a dataset (democrats)
9
var data1 = anychart.data
10
.set([
11
['Mar', 4.26, 0.1, 0.1],
12
['Apr', 4.46, 0.11, 0.11],
13
['May', 4.36, 0.126, 0.126],
14
['June', 4.29, 0.132, 0.132],
15
['July', 4.49, 0.124, 0.124],
16
['Aug', 4.47, 0.124, 0.124]
17
])
18
.mapAs({
19
x: 0,
20
value: 1,
21
valueLowerError: 2,
22
valueUpperError: 3
23
});
24
25
// create a dataset (republicans)
26
var data2 = anychart.data
27
.set([
28
['Mar', 3.9, 0.144, 0.144],
29
['Apr', 4.09, 0.172, 0.172],
30
['May', 3.71, 0.196, 0.196],
31
['June', 3.54, 0.198, 0.198],
32
['July', 3.78, 0.196, 0.196],
33
['Aug', 3.7, 0.194, 0.195]
34
])
35
.mapAs({
36
x: 0,
37
value: 1,
38
valueLowerError: 2,
39
valueUpperError: 3
40
});
41
42
// set the y-axis parameters
43
var yScale = chart.yScale();
44
yScale.minimum(3.2);
45
yScale.maximum(4.8);
46
47
var ticks = chart.yScale().ticks();
48
ticks.interval(0.2);
49
50
// set the grid
51
chart.yGrid().enabled(true).stroke({
52
// customize the stroke color
53
color: '#ededed'
54
});
55
56
// create a line series for the first dataset
57
var series1 = chart.line(data1).stroke('#031bbb', 3);
58
59
// create error bars for the first series
60
var error1 = series1.error();
61
error1.valueErrorStroke('#031bbb', 1);
62
63
// set markers for the first series
64
chart.marker(data1).type('circle').size(5).stroke('#031bbb', 1).fill('#0066cc');
65
66
// name the first series
67
series1
68
.name('Democrats');
69
70
// create a line series for the second dataset
71
var series2 = chart.line(data2).stroke('#de0100', 3);
72
73
// create error bars for the second series
74
var error2 = series2.error();
75
error2.valueErrorStroke('#de0100', 1);
76
77
// set markers for the second series
78
chart.marker(data2).type('square').size(5).stroke('#de0100', 1).fill('#de0100');
79
80
// name the second series
81
series2
82
.name('Republicans');
83
84
// configure the tooltip
85
var tooltip = chart.tooltip();
86
tooltip.displayMode('separated');
87
88
// configure the tooltip formatter
89
tooltip
90
.fontColor('#E1E1E1')
91
.titleFormat('{%seriesName}')
92
.format(function () {
93
return (
94
'Mean value: ' +
95
this.value +
96
'\nUpper error value: ' +
97
this.valueLowerError +
98
'\nLower error value: ' +
99
this.valueUpperError
100
);
101
});
102
103
// set the tooltip position
104
chart.tooltip().positionMode('point');
105
106
// set a container id for the chart
107
chart.container('container');
108
109
// command to draw the chart
110
chart.draw();
111
112
});