-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.js
129 lines (124 loc) · 3.32 KB
/
chart.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {fillChartData} from "./data.js";
export function drawGraph(data, canvas) {
const ctx = canvas.getContext('2d');
data.sort(function(a, b) {
// Convert dates from "dd:mm:yyyy" to "yyyy-mm-dd"
var partsA = a.x.split(":");
var dateA = new Date(partsA[2], partsA[1] - 1, partsA[0]);
var partsB = b.x.split(":");
var dateB = new Date(partsB[2], partsB[1] - 1, partsB[0]);
// Compare the dates
return dateA - dateB;
});
return new Chart(ctx, {
type: 'line',
data: {
labels: data.map(d => d.x),
datasets: [{
// label: 'Daily Average (Month)',
data: data.map(d => d.y),
borderColor: 'blue',
// backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderWidth: 1,
fill: true,
spanGaps: false,
},
// {
// label: 'Average',
// data: [{ x: data[0].x, y: average }, { x: data[data.length - 1].x, y: average }],
// borderColor: 'red',
// borderWidth: 1,
// borderDash: [5, 5],
// fill: false,
// order: 0,
// spanGaps:false,
//
// // showLine: true,
// }
],
},
options: {
maintainAspectRatio: false,
animation: {
duration: 0,
easing: 'linear',
animateRotate: false,
animateScale: false,
animateDraw: false,
},
hover: {
animationDuration: 0,
},
responsiveAnimationDuration: 0,
legend: {
display: false,
},
tooltips: {
callbacks: {
title: function(tooltipItem, data) {
const index = tooltipItem[0].index;
const dataPoint = data.datasets[0].data[index];
const date = formatDate(data.labels[index], true);
return `${date}\nAverage AOD Value: ${dataPoint}`;
},
label: function(tooltipItem) {
return '';
}
}
},
scales: {
yAxes: [
{
ticks: {
fontColor: 'black',
beginAtZero: true,
stepSize: 1,
// maxTickLimit: 4,
},
},
],
xAxes: [
{
ticks: {
fontColor: 'black',
// maxTicksLimit: 15,
autoSkip: true,
// max: parseFloat(data.datasets[0].data.toFixed(2)),
maxRotation: 150,
minRotation: 0,
callback: function(value, index, values) {
return formatDate(value);
},
},
},
],
},
elements: {
line: {
borderColor: 'rgba(0, 0, 0, 1)',
},
point: {
borderColor: 'rgba(0, 0, 0, 1)',
},
},
responsive: false,
onCreated: function(chart) {
const max = Math.max(...chart.data.datasets[0].data);
chart.options.scales.yAxes[0].ticks.max = parseFloat(max.toFixed(2));
chart.update();
},
},
});
}
function formatDate(dateString, full=false) {
const dateArr = dateString.split(':')
const date = new Date(dateArr[2], dateArr[1]-1, dateArr[0]);
let options;
if (full)
{
options = {year: 'numeric', month: 'long', day: 'numeric'}
}else {
options = {month: 'short', day: 'numeric'};
}
return date.toLocaleDateString('en-US', options);
}