2019-03-05 01:04:20 +09:00
|
|
|
import CalHeatMap from 'cal-heatmap';
|
|
|
|
import Chart from 'chart.js';
|
2019-08-04 00:57:35 +09:00
|
|
|
import {addMonths, format, startOfMonth, subMonths} 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
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
new CalHeatMap().init({
|
|
|
|
itemSelector: '#cal-heatmap',
|
|
|
|
domain: 'month',
|
|
|
|
subDomain: 'day',
|
|
|
|
domainLabelFormat: '%Y/%m',
|
|
|
|
weekStartOnMonday: false,
|
|
|
|
start: new Date().setMonth(new Date().getMonth() - 9),
|
|
|
|
range: 10,
|
2019-03-05 01:08:17 +09:00
|
|
|
data: graphData.dailySum,
|
2019-03-05 01:04:20 +09:00
|
|
|
legend: [1, 2, 3, 4]
|
|
|
|
});
|
|
|
|
|
2019-08-04 00:57:35 +09:00
|
|
|
// 直近1年の月間グラフのデータを準備
|
|
|
|
const monthlyTermFrom = subMonths(startOfMonth(new Date()), 11);
|
2019-08-04 01:31:53 +09:00
|
|
|
const {keys: monthlyKey, values: monthlySum} = createMonthlyGraphData(monthlyTermFrom);
|
2019-08-04 00:57:35 +09:00
|
|
|
|
2019-08-04 01:31:53 +09:00
|
|
|
const monthlyGraph = createLineGraph('monthly-graph', monthlyKey, monthlySum);
|
2019-03-05 01:08:17 +09:00
|
|
|
createLineGraph('yearly-graph', graphData.yearlyKey, graphData.yearlySum);
|
|
|
|
createBarGraph('hourly-graph', graphData.hourlyKey, graphData.hourlySum);
|
2019-08-04 00:57:35 +09:00
|
|
|
createBarGraph('dow-graph', ['日', '月', '火', '水', '木', '金', '土'], graphData.dowSum);
|
2019-08-04 01:31:53 +09:00
|
|
|
|
|
|
|
// 月間グラフの期間セレクターを準備
|
|
|
|
const monthlyTermSelector = document.getElementById('monthly-term');
|
2019-12-16 22:56:11 +09:00
|
|
|
const earliestYear = [...new Set(Object.keys(graphData.monthlySum).map(v => v.substr(0, 4)))].shift();
|
|
|
|
for (let year = earliestYear; year <= new Date().getFullYear(); year++) {
|
2019-08-04 01:31:53 +09:00
|
|
|
const opt = document.createElement('option');
|
|
|
|
opt.setAttribute('value', year);
|
2019-08-04 01:33:54 +09:00
|
|
|
opt.textContent = `${year}年`;
|
2019-08-04 01:31:53 +09:00
|
|
|
monthlyTermSelector.insertBefore(opt, monthlyTermSelector.firstChild);
|
|
|
|
}
|
|
|
|
if (monthlyTermSelector.children.length) {
|
|
|
|
monthlyTermSelector.selectedIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
monthlyTermSelector.addEventListener('change', function (e) {
|
|
|
|
let monthlyTermFrom;
|
|
|
|
if (e.target.selectedIndex === 0) {
|
|
|
|
// 今年のデータを表示する時は、直近12ヶ月を表示
|
|
|
|
monthlyTermFrom = subMonths(startOfMonth(new Date()), 11);
|
|
|
|
} else {
|
|
|
|
// 過去のデータを表示する時は、選択年の1〜12月を表示
|
|
|
|
monthlyTermFrom = new Date(e.target.value, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {keys, values} = createMonthlyGraphData(monthlyTermFrom);
|
|
|
|
|
|
|
|
monthlyGraph.data.labels = keys;
|
|
|
|
monthlyGraph.data.datasets[0].data = values;
|
|
|
|
monthlyGraph.update();
|
|
|
|
});
|