@@ -76,7 +76,9 @@ function plot(canvas) {
7676 } ,
7777 options : {
7878 scales : {
79+ bounds : 'ticks' ,
7980 xAxes : [ {
81+ source : 'auto' ,
8082 type : 'time' ,
8183 time : {
8284 tooltipFormat : 'YYYY-MM-DD HH:mm.ss' ,
@@ -87,6 +89,10 @@ function plot(canvas) {
8789 hour : 'HH:mm'
8890 }
8991 } ,
92+ ticks : {
93+ min : moment . utc ( ) . subtract ( 1 , 'days' ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
94+ max : moment . utc ( ) . format ( 'YYYY-MM-DD HH:mm:ss' )
95+ } ,
9096 distribution : 'linear' ,
9197 scaleLabel : {
9298 display : true ,
@@ -97,6 +103,11 @@ function plot(canvas) {
97103 scaleLabel : {
98104 display : true ,
99105 labelString : label
106+ } ,
107+ ticks : {
108+ callback : function ( value ) {
109+ return value . toFixed ( 1 ) ;
110+ }
100111 }
101112 } ]
102113 } ,
@@ -250,11 +261,134 @@ function update_timeline() {
250261 setTimeout ( update_timeline , 60000 ) ;
251262}
252263
264+ function create_good_annotation ( good ) {
265+ return {
266+ type : 'box' ,
267+ display : true ,
268+ xScaleID : 'x-axis-0' ,
269+ yScaleID : 'y-axis-0' ,
270+ drawTime : 'beforeDatasetsDraw' ,
271+ borderWidth : 0 ,
272+ backgroundColor : good ? 'rgba(0, 255, 0, 0.5)' : 'rgba(255, 0, 0, 0.5)'
273+ }
274+ }
275+
276+ function plot_good_history ( ) {
277+ // do AJAX request
278+ $ . ajax ( {
279+ url : rootURL + 'api/history/goodweather/' ,
280+ dataType : 'json' ,
281+ } ) . done ( function ( results ) {
282+ // format data
283+ let data = [ ] ;
284+ results . sun . time . forEach ( function ( value , index ) {
285+ data . push ( { t : new moment . utc ( value ) . format ( 'YYYY-MM-DD HH:mm:ss' ) , y : results . sun . alt [ index ] } )
286+ } ) ;
287+
288+ // annotations
289+ let annotations = [ ] ;
290+ for ( let i = 0 ; i < results . changes . length ; i ++ ) {
291+ // get change
292+ let change = results . changes [ i ] ;
293+
294+ // first one needs an additional annotation
295+ if ( i === 0 ) {
296+ let ann = create_good_annotation ( ! change . good ) ;
297+ ann . xMax = moment . utc ( change . time ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
298+ annotations . push ( ann ) ;
299+ }
300+
301+ // min/max depends on first/last
302+ let ann = create_good_annotation ( change . good ) ;
303+ ann . xMin = moment . utc ( change . time ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
304+ if ( results . changes . length > i + 1 ) {
305+ ann . xMax = moment . utc ( results . changes [ i + 1 ] . time ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
306+ }
307+ annotations . push ( ann ) ;
308+ }
309+
310+ // add annotation for line at 0
311+ annotations . push ( {
312+ type : 'line' ,
313+ mode : 'horizontal' ,
314+ scaleID : 'y-axis-0' ,
315+ value : 0 ,
316+ borderColor : 'rgb(0, 0, 0, 0.5)' ,
317+ borderWidth : 1
318+ } )
319+
320+ // create plot
321+ new Chart ( $ ( '#goodhistory' ) [ 0 ] . getContext ( '2d' ) , {
322+ type : 'line' ,
323+ data : {
324+ datasets : [ {
325+ label : undefined ,
326+ data : data ,
327+ backgroundColor : 'rgb(255, 255, 100, 0.5)' ,
328+ borderColor : 'rgb(255, 255, 100, 1)' ,
329+ pointRadius : 0 ,
330+ fill : false ,
331+ lineTension : 0.2
332+ } ]
333+ } ,
334+ options : {
335+ animation : {
336+ duration : 0
337+ } ,
338+ legend : {
339+ display : false
340+ } ,
341+ scales : {
342+ bounds : 'ticks' ,
343+ xAxes : [ {
344+ type : 'time' ,
345+ source : 'auto' ,
346+ distribution : 'linear' ,
347+ time : {
348+ tooltipFormat : 'YYYY-MM-DD HH:mm.ss' ,
349+ displayFormats : {
350+ millisecond : 'HH:mm' ,
351+ second : 'HH:mm' ,
352+ minute : 'HH:mm' ,
353+ hour : 'HH:mm'
354+ }
355+ } ,
356+ ticks : {
357+ min : moment . utc ( ) . subtract ( 1 , 'days' ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
358+ max : moment . utc ( ) . format ( 'YYYY-MM-DD HH:mm:ss' )
359+ } ,
360+ scaleLabel : {
361+ display : true ,
362+ labelString : 'Time [UT]' ,
363+ }
364+ } ] ,
365+ yAxes : [ {
366+ scaleLabel : {
367+ display : true ,
368+ labelString : 'Sun/good'
369+ } ,
370+ } ]
371+ } ,
372+ annotation : {
373+ annotations : annotations
374+ }
375+ }
376+ } ) ;
377+
378+ } ) ;
379+ }
380+
381+ function update_good_history ( ) {
382+ plot_good_history ( ) ;
383+ setTimeout ( update_good_history , 60000 ) ;
384+ }
385+
253386$ ( function ( ) {
254387 Chart . defaults . global . defaultFontFamily = 'Alegreya' ;
255388
256389 $ ( window ) . on ( 'resize' , draw_timeline ) ;
257390 update_timeline ( ) ;
258391 update_plots ( ) ;
392+ update_good_history ( ) ;
259393 update_values ( ) ;
260394} ) ;
0 commit comments