2019-03-05 01:04:20 +09:00
|
|
|
import CalHeatMap from 'cal-heatmap';
|
|
|
|
import Chart from 'chart.js';
|
2020-05-24 17:27:42 +09:00
|
|
|
import {addMonths, format} from 'date-fns';
|
2019-03-05 01:04:20 +09:00
|
|
|
|
2019-08-04 01:31:53 +09:00
|
|
|
const graphData = JSON.parse(document.getElementById('graph-data').textContent);
|
|
|
|
|
2019-03-05 01:04:20 +09:00
|
|
|
function createLineGraph(id, labels, data) {
|
|
|
|
const context = document.getElementById(id).getContext('2d');
|
2019-08-04 01:31:53 +09:00
|
|
|
return new Chart(context, {
|
2019-03-05 01:04:20 +09:00
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: labels,
|
|
|
|
datasets: [{
|
|
|
|
data: data,
|
|
|
|
backgroundColor: 'rgba(255, 99, 132, 0.2)',
|
|
|
|
borderColor: 'rgba(255, 99, 132, 1)',
|
|
|
|
borderWidth: 1
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
elements: {
|
|
|
|
line: {
|
|
|
|
tension: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
yAxes: [{
|
|
|
|
ticks: {
|
|
|
|
beginAtZero: true
|
|
|
|
}
|
|
|
|
}]
|
2020-02-03 01:12:52 +09:00
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
2019-03-05 01:04:20 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createBarGraph(id, labels, data) {
|
|
|
|
const context = document.getElementById(id).getContext('2d');
|
|
|
|
new Chart(context, {
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
labels: labels,
|
|
|
|
datasets: [{
|
|
|
|
data: data,
|
|
|
|
backgroundColor: 'rgba(255, 99, 132, 0.2)',
|
|
|
|
borderColor: 'rgba(255, 99, 132, 1)',
|
|
|
|
borderWidth: 1
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
yAxes: [{
|
|
|
|
ticks: {
|
|
|
|
beginAtZero: true
|
|
|
|
}
|
|
|
|
}]
|
2020-02-03 01:12:52 +09:00
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
2019-03-05 01:04:20 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-04 01:31:53 +09:00
|
|
|
/**
|
|
|
|
* @param {Date} from
|
|
|
|
*/
|
|
|
|
function createMonthlyGraphData(from) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {keys, values};
|
|
|
|
}
|
2019-03-05 01:04:20 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
function getCurrentYear() {
|
|
|
|
const year = location.pathname.split('/').pop();
|
|
|
|
return /^(20[0-9]{2})$/.test(year) ? year : null;
|
|
|
|
}
|
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,
|
|
|
|
legend: [1, 2, 3, 4]
|
|
|
|
});
|
|
|
|
}
|
2019-08-04 01:31:53 +09:00
|
|
|
|
2020-05-24 17:27:42 +09:00
|
|
|
if (document.getElementById('monthly-graph')) {
|
|
|
|
const {keys: monthlyKey, values: monthlySum} = createMonthlyGraphData(new Date(getCurrentYear(), 0, 1, 0, 0, 0, 0));
|
|
|
|
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
|
|
|
}
|