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+ }
0 commit comments