2020-06-06 16:14:11 +09:00
|
|
|
import * as CalHeatMap from 'cal-heatmap';
|
|
|
|
import * as Chart from 'chart.js';
|
2020-06-06 18:01:56 +09:00
|
|
|
import { addMonths, format } from 'date-fns';
|
2019-03-05 01:04:20 +09:00
|
|
|
|
2020-06-06 19:12:00 +09:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2020-06-06 16:14:11 +09:00
|
|
|
const graphData = JSON.parse(document.getElementById('graph-data')!.textContent as string);
|
2019-08-04 01:31:53 +09:00
|
|
|
|
2020-06-06 16:14:11 +09:00
|
|
|
function createLineGraph(id: string, labels: string[], data: any) {
|
|
|
|
const context = (document.getElementById(id) as HTMLCanvasElement).getContext('2d');
|
2020-06-06 19:12:00 +09:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2020-06-06 16:14:11 +09:00
|
|
|
return new Chart(context!, {
|
2019-03-05 01:04:20 +09:00
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: labels,
|
2020-06-06 18:01:56 +09:00
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
data: data,
|
|
|
|
backgroundColor: 'rgba(255, 99, 132, 0.2)',
|
|
|
|
borderColor: 'rgba(255, 99, 132, 1)',
|
|
|
|
borderWidth: 1,
|
|
|
|
},
|
|
|
|
],
|
2019-03-05 01:04:20 +09:00
|
|
|
},
|
|
|
|
options: {
|
|
|
|
legend: {
|
2020-06-06 18:01:56 +09:00
|
|
|
display: false,
|
2019-03-05 01:04:20 +09:00
|
|
|
},
|
|
|
|
elements: {
|
|
|
|
line: {
|
2020-06-06 18:01:56 +09:00
|
|
|
tension: 0,
|
|
|
|
},
|
2019-03-05 01:04:20 +09:00
|
|
|
},
|
|
|
|
scales: {
|
2020-06-06 18:01:56 +09:00
|
|
|
yAxes: [
|
|
|
|
{
|
|
|
|
ticks: {
|
|
|
|
beginAtZero: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-02-03 01:12:52 +09:00
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
2020-06-06 18:01:56 +09:00
|
|
|
},
|
|
|
|
},
|
2019-03-05 01:04:20 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-06 16:14:11 +09:00
|
|
|
function createBarGraph(id: string, labels: string[], data: any) {
|
|
|
|
const context = (document.getElementById(id) as HTMLCanvasElement).getContext('2d');
|
2020-06-06 19:12:00 +09:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2020-06-06 16:14:11 +09:00
|
|
|
new Chart(context!, {
|
2019-03-05 01:04:20 +09:00
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
labels: labels,
|
2020-06-06 18:01:56 +09:00
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
data: data,
|
|
|
|
backgroundColor: 'rgba(255, 99, 132, 0.2)',
|
|
|
|
borderColor: 'rgba(255, 99, 132, 1)',
|
|
|
|
borderWidth: 1,
|
|
|
|
},
|
|
|
|
],
|
2019-03-05 01:04:20 +09:00
|
|
|
},
|
|
|
|
options: {
|
|
|
|
legend: {
|
2020-06-06 18:01:56 +09:00
|
|
|
display: false,
|
2019-03-05 01:04:20 +09:00
|
|
|
},
|
|
|
|
scales: {
|
2020-06-06 18:01:56 +09:00
|
|
|
yAxes: [
|
|
|
|
{
|
|
|
|
ticks: {
|
|
|
|
beginAtZero: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-02-03 01:12:52 +09:00
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
2020-06-06 18:01:56 +09:00
|
|
|
},
|
|
|
|
},
|
2019-03-05 01:04:20 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-06 16:14:11 +09:00
|
|
|
function createMonthlyGraphData(from: Date) {
|
2019-08-04 01:31:53 +09:00
|
|
|
const keys = [];
|
|
|
|
const values = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
const current = addMonths(from, i);
|
|
|
|
const yearAndMonth = format(current, 'YYYY/MM');
|
|
|
|
keys.push(yearAndMonth);
|
|
|
|
values.push(graphData.monthlySum[yearAndMonth] || 0);
|
|
|
|
}
|
|
|
|
|
2020-06-06 18:01:56 +09:00
|
|
|
return { keys, values };
|
2019-08-04 01:31:53 +09:00
|
|
|
}
|
2019-03-05 01:04:20 +09:00
|
|
|
|
2020-06-06 16:14:11 +09:00
|
|
|
function getCurrentYear(): number {
|
|
|
|
const year = location.pathname.split('/').pop() || '';
|
|
|
|
if (/^(20[0-9]{2})$/.test(year)) {
|
|
|
|
return parseInt(year, 10);
|
|
|
|
} else {
|
|
|
|
throw 'Invalid year';
|
|
|
|
}
|
2020-05-24 17:27:42 +09:00
|
|
|
}
|
2019-08-04 00:57:35 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
if (document.getElementById('cal-heatmap')) {
|
|
|
|
new CalHeatMap().init({
|
|
|
|
itemSelector: '#cal-heatmap',
|
|
|
|
domain: 'month',
|
|
|
|
subDomain: 'day',
|
|
|
|
domainLabelFormat: '%Y/%m',
|
|
|
|
weekStartOnMonday: false,
|
|
|
|
start: new Date(getCurrentYear(), 0, 1, 0, 0, 0, 0),
|
|
|
|
range: 12,
|
|
|
|
data: graphData.dailySum,
|
2020-06-06 18:01:56 +09:00
|
|
|
legend: [1, 2, 3, 4],
|
2020-05-24 17:27:42 +09:00
|
|
|
});
|
|
|
|
}
|
2019-08-04 01:31:53 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
if (document.getElementById('monthly-graph')) {
|
2020-06-06 18:01:56 +09:00
|
|
|
const { keys: monthlyKey, values: monthlySum } = createMonthlyGraphData(
|
|
|
|
new Date(getCurrentYear(), 0, 1, 0, 0, 0, 0)
|
|
|
|
);
|
2020-05-24 17:27:42 +09:00
|
|
|
createLineGraph('monthly-graph', monthlyKey, monthlySum);
|
2019-08-04 01:31:53 +09:00
|
|
|
}
|
2020-05-24 17:27:42 +09:00
|
|
|
if (document.getElementById('yearly-graph')) {
|
|
|
|
createLineGraph('yearly-graph', graphData.yearlyKey, graphData.yearlySum);
|
|
|
|
}
|
|
|
|
if (document.getElementById('hourly-graph')) {
|
|
|
|
createBarGraph('hourly-graph', graphData.hourlyKey, graphData.hourlySum);
|
|
|
|
}
|
|
|
|
if (document.getElementById('dow-graph')) {
|
|
|
|
createBarGraph('dow-graph', ['日', '月', '火', '水', '木', '金', '土'], graphData.dowSum);
|
2019-08-04 01:31:53 +09:00
|
|
|
}
|