Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 9e676a5

Browse files
committed
Daily work bar chart
1 parent 58fd677 commit 9e676a5

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

php/load/graphs/BarDailyWork.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function createGraph(combiData, plainData, singleDayData, canvas){
2+
/**
3+
* The DOM has jQuery as $ and the ChartJS library.
4+
* The data can be found in the three first data parameters and canvas is the canvas for the chart.
5+
* The function should return the ChartJS object.
6+
*/
7+
8+
let plotdata = {};
9+
plainData.forEach(data => {
10+
let date = new Date(data.begin * 1000);
11+
let day = (date.getDate() > 9 ? '' : '0') + date.getDate();
12+
let month = (date.getMonth() + 1 > 9 ? '' : '0') + (date.getMonth() + 1);
13+
14+
let key = `${day}.${month}.${date.getFullYear()}`;
15+
if (!plotdata.hasOwnProperty(key)) {
16+
plotdata[key] = 0;
17+
}
18+
19+
plotdata[key] += data.duration;
20+
});
21+
22+
Object.keys(plotdata).forEach(key => {
23+
plotdata[key] = Math.round((plotdata[key] / 3600) * 100) / 100;
24+
});
25+
26+
let plotdataSorted = {};
27+
Object.keys(plotdata).sort().forEach(key => {
28+
plotdataSorted[key] = plotdata[key];
29+
});
30+
31+
/**
32+
* Colors from
33+
* chartjs-plugin-colorschemes MIT License
34+
* Copyright (c) 2019 Akihiko Kusanagi
35+
* https://github.com/nagix/chartjs-plugin-colorschemes/blob/master/src/colorschemes/colorschemes.tableau.js
36+
*/
37+
let config = {
38+
type: 'bar',
39+
data: {
40+
datasets: [{
41+
data: Object.values(plotdataSorted),
42+
label: 'Daily work',
43+
backgroundColor: '#A0CBE8',
44+
borderColor: '#4E79A7',
45+
borderWidth: 1
46+
}],
47+
labels: Object.keys(plotdataSorted)
48+
},
49+
options: {
50+
responsive: true,
51+
tooltips: {
52+
callbacks: {
53+
label: function(tooltipItem, chartData) {
54+
return `Daily work: ${chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]} hours`;
55+
}
56+
}
57+
}
58+
}
59+
};
60+
61+
return new Chart(canvas, config);
62+
}

php/load/graphs/PieCategory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function createGraph(combiData, plainData, singleDayData, canvas){
4343
callbacks: {
4444
label: function(tooltipItem, chartData) {
4545
return chartData.labels[tooltipItem.index] + ': ' +
46-
chartData.datasets[0].data[tooltipItem.index] + ' hours';
46+
chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index] + ' hours';
4747
}
4848
}
4949
}

php/load/graphs/PieTask.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function createGraph(combiData, plainData, singleDayData, canvas){
4343
callbacks: {
4444
label: function(tooltipItem, chartData) {
4545
return chartData.labels[tooltipItem.index] + ': ' +
46-
chartData.datasets[0].data[tooltipItem.index] + ' hours';
46+
chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index] + ' hours';
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)