-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
100 lines (77 loc) · 2.32 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var TITLE = 'Air Transport, Passengers Carried (1970–2018)';
// Caption underneath the chart
var CAPTION = 'Source: The World Bank';
// Add download link to the caption
CAPTION += '<br><a href="https://github.com/HandsOnDataViz/highcharts-line-annotated" style="color:blue">View data and code</a>, \
created by <a href="https://handsondataviz.org/" style="color: blue">HandsOnDataViz</a> \
with <a href="https://www.highcharts.com/" style="color: blue">Highcharts</a>';
// x-axis label and label in tooltip
var X_AXIS = 'Year';
// y-axis label and label in tooltip
var Y_AXIS = 'Passengers';
// Should y-axis start from 0? `true` or `false`
var BEGIN_AT_ZERO = true;
// `true` to show the legend, `false` to hide
var SHOW_LEGEND = true;
$(document).ready(function() {
// Read data file and create a chart
$.get('./data.csv', function(csvString) {
var data = Papa.parse(csvString).data;
var nSeries = data[0].length - 2
var annotationPoints = data
.filter(function(x) {return x[nSeries+1] !== ""})
.map(function(x) {
return {
point: {
xAxis: 0,
yAxis: 0,
x: x[0],
y: x[1]
},
text: x[nSeries+1]
}
})
var series = []
for (var i = 1; i <= nSeries; i++) {
series.push({
data: data.map(function(x) { return [parseFloat(x[0]), parseFloat(x[i])] }),
name: data[0][i],
marker: { enabled: false },
})
}
// Now create the chart
Highcharts.chart('container', {
chart: {
type: 'line',
zoomType: 'x',
panning: true,
panKey: 'shift',
scrollablePlotArea: {
minWidth: 600
}
},
title: { text: TITLE },
caption: { text: CAPTION },
credits: { enabled: false },
annotations: [{
labelOptions: {
backgroundColor: 'rgba(255,255,255,0.8)',
verticalAlign: 'top',
y: 10
},
labels: annotationPoints
}],
xAxis: {
title: { text: X_AXIS },
},
yAxis: {
startOnTick: true,
min: BEGIN_AT_ZERO ? 0 : null,
title: { text: Y_AXIS },
labels: { formatter: function(x) {return x.value.toLocaleString()} }
},
legend: { enabled: SHOW_LEGEND },
series: series,
});
});
});