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

Commit 219ad47

Browse files
authored
Merge pull request #4 from Malte311/master
Improved chart labels on hover && added new chart
2 parents 25083cd + 92ec761 commit 219ad47

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
latest
2-
0.2.1
2+
0.2.2
33
0.2
44
0

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/BarDayTimes.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ function createGraph(combiData, plainData, singleDayData, canvas){
7070
type: 'bar',
7171
data: chartData,
7272
options: {
73-
responsive: true
73+
responsive: true,
74+
tooltips: {
75+
callbacks: {
76+
label: function(tooltipItem, chartData) {
77+
return `${chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]} hours`;
78+
}
79+
}
80+
}
7481
}
7582
};
7683
return new Chart(canvas, config);

php/load/graphs/BarDays.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ function createGraph(combiData, plainData, singleDayData, canvas){
4949
type: 'bar',
5050
data: chartData,
5151
options: {
52-
responsive: true
52+
responsive: true,
53+
tooltips: {
54+
callbacks: {
55+
label: function(tooltipItem, chartData) {
56+
return `${chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]} hours`;
57+
}
58+
}
59+
}
5360
}
5461
};
5562
return new Chart(canvas, config);

php/load/graphs/PieCategory.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ function createGraph(combiData, plainData, singleDayData, canvas){
3838
labels: Object.keys(plotdata)
3939
},
4040
options: {
41-
responsive: true
41+
responsive: true,
42+
tooltips: {
43+
callbacks: {
44+
label: function(tooltipItem, chartData) {
45+
return chartData.labels[tooltipItem.index] + ': ' +
46+
chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index] + ' hours';
47+
}
48+
}
49+
}
4250
}
4351
};
4452
return new Chart(canvas, config);

php/load/graphs/PieTask.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ function createGraph(combiData, plainData, singleDayData, canvas){
3838
labels: Object.keys(plotdata)
3939
},
4040
options: {
41-
responsive: true
41+
responsive: true,
42+
tooltips: {
43+
callbacks: {
44+
label: function(tooltipItem, chartData) {
45+
return chartData.labels[tooltipItem.index] + ': ' +
46+
chartData.datasets[tooltipItem.datasetIndex].data[tooltipItem.index] + ' hours';
47+
}
48+
}
49+
}
4250
}
4351
};
4452
return new Chart(canvas, config);

0 commit comments

Comments
 (0)