-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummary.js
86 lines (78 loc) · 2.23 KB
/
summary.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
/* eslint-disable no-unused-vars */
/* global Chart, stats, pixelToMeter, updateDistance, updateScrollDistances */
async function createSummary() {
/** @type {Chart} */
let chart;
const weekData = await stats.getWeeklyStats();
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const todayIndex = new Date().getDay();
let focusedDay = todayIndex;
const isFocused = (index) => index === focusedDay;
/** @type {Chart.ChartData} */
const data = {
labels: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
datasets: [
{
label: 'Daily scroll distance',
// highlight the selected day, gray out the rest
backgroundColor: ({ dataIndex }) => (isFocused(dataIndex) ? '#0984FF' : '#B9B4C7'),
borderColor: ({ dataIndex }) => (isFocused(dataIndex) ? '#0984FF' : '#B9B4C7'),
borderWidth: 1,
data: weekData.map(({ distance }) => pixelToMeter(distance)),
minBarLength: 2,
},
],
};
/** @type {Chart.ChartOptions} */
const options = {
title: {
text: 'Summary',
},
plugins: {
legend: {
onClick: () => {},
},
tooltip: {
callbacks: {
title: (tooltipItem) => {
return days[tooltipItem[0].dataIndex];
},
label: (tooltipItem) => `${tooltipItem.formattedValue}m`,
},
},
},
maintainAspectRatio: true,
scales: {
y: {
stacked: true,
grid: {
display: true,
},
},
x: {
grid: {
display: false,
},
},
},
onClick: (event, chartElements) => {
if (chartElements.length > 0) {
const clickedBarIndex = chartElements[0].index;
focusedDay = clickedBarIndex;
const selectedDate = new Date();
selectedDate.setDate(selectedDate.getDate() - (todayIndex - clickedBarIndex));
stats.getDayStats(selectedDate).then(({ totalDistance, domains }) => {
updateDistance(selectedDate, totalDistance);
updateScrollDistances(domains);
});
chart.update();
}
},
};
return (chart = new Chart('scroll-summary', {
type: 'bar',
options: options,
data: data,
}));
}
createSummary();