@@ -67,49 +67,57 @@ <h1>MyStrom Results</h1>
6767< script src ="https://cdnjs.cloudflare.com/ajax/libs/apexcharts/3.35.3/apexcharts.min.js "> </ script >
6868
6969< script type ="application/javascript ">
70- var shown = [ ]
70+ let shown = [ ] ;
7171
7272 function resetDates ( el ) {
73- id = String ( el . id ) . split ( "-" ) [ 2 ]
74- document . getElementById ( "start-date-" + id ) . value = ""
75- document . getElementById ( "end-date-" + id ) . value = ""
73+ const id = String ( el . id ) . split ( "-" ) [ 2 ] ;
74+ document . getElementById ( "start-date-" + id ) . value = "" ;
75+ document . getElementById ( "end-date-" + id ) . value = "" ;
7676 }
7777
7878 function toggleChart ( id ) {
79- startDateElement = document . getElementById ( "start-date-" + id )
80- endDateElement = document . getElementById ( "end-date-" + id )
81- elementId = "chart-" + id
79+ const startDateElement = document . getElementById ( "start-date-" + id ) ;
80+ const endDateElement = document . getElementById ( "end-date-" + id ) ;
81+ const elementId = "chart-" + id ;
8282 if ( shown . includes ( id ) ) {
83- document . getElementById ( elementId ) . outerHTML = ""
83+ document . getElementById ( elementId ) . outerHTML = "" ;
8484
85- document . getElementById ( id ) . innerHTML = "Show Chart for Device " + id
86- shown = shown . filter ( shownId => shownId != id )
85+ document . getElementById ( id ) . innerHTML = "Show Chart for Device " + id ;
86+ shown = shown . filter ( shownId => shownId != id ) ;
8787 } else {
88- createdElement = document . createElement ( "div" )
89- createdElement . setAttribute ( "id" , elementId )
90- document . getElementById ( "chartContainer" ) . appendChild ( createdElement )
88+ const createdElement = document . createElement ( "div" ) ;
89+ createdElement . setAttribute ( "id" , elementId ) ;
90+ document . getElementById ( "chartContainer" ) . appendChild ( createdElement ) ;
9191
92- informationElement = document . createElement ( "div" )
93- informationElement . setAttribute ( "id" , elementId + "-information" )
92+ const informationElement = document . createElement ( "div" ) ;
93+ informationElement . setAttribute ( "id" , elementId + "-information" ) ;
9494 createdElement . appendChild ( informationElement ) ;
9595
96- chartElement = document . createElement ( "div" )
97- chartElement . setAttribute ( "id" , elementId + "-chart" )
98- createdElement . appendChild ( chartElement )
96+ const chartElement = document . createElement ( "div" ) ;
97+ chartElement . setAttribute ( "id" , elementId + "-chart" ) ;
98+ createdElement . appendChild ( chartElement ) ;
9999
100- chart = loadChart ( id , elementId , startDateElement . value , endDateElement . value )
100+ loadChart ( id , elementId , startDateElement . value , endDateElement . value ) ;
101101
102- document . getElementById ( id ) . innerHTML = "Hide Chart for Device " + id
103- shown . push ( id )
102+ document . getElementById ( id ) . innerHTML = "Hide Chart for Device " + id ;
103+ shown . push ( id ) ;
104104 }
105105
106106 }
107107
108108 function loadChart ( id , elementId , startDate , endDate ) {
109- startQuery = startDate == "" ? "" : "?start=" + startDate
110- endQuery = endDate == "" ? "" : ( startDate == "" ? "?" : "&" ) + "end=" + endDate
111- console . log ( "{% url 'rest_device_index' %}" + id + "/results/" + startQuery + endQuery )
109+ const startQuery = startDate == "" ? "" : "?start=" + startDate ;
110+ const endQuery = endDate == "" ? "" : ( startDate == "" ? "?" : "&" ) + "end=" + endDate ;
111+ console . log ( "Requesting REST: {% url 'rest_device_index' %}" + id + "/results/" + startQuery + endQuery ) ;
112112 $ . get ( "{% url 'rest_device_index' %}" + id + "/results/" + startQuery + endQuery , function ( data ) {
113+ const chartElement = document . getElementById ( elementId + "-chart" ) ;
114+ const informationElement = document . getElementById ( elementId + "-information" ) ;
115+
116+ if ( data . length == 0 ) {
117+ informationElement . innerHTML = '<h3 class="text-danger">Query of device ' + id + ' is empty</h3>' ;
118+ console . log ( "Data is empty. Can not show any data" ) ;
119+ return ;
120+ }
113121 const firstDate = new Date ( data [ 0 ] . date ) ;
114122 const lastDate = new Date ( data [ data . length - 1 ] . date ) ;
115123 const differenceBetweenDates = lastDate - firstDate ;
@@ -118,15 +126,15 @@ <h1>MyStrom Results</h1>
118126 const powerList = data . map ( entry => entry . power ) ;
119127 const WsList = data . map ( entry => entry . ws ) ;
120128 const temperatureList = data . map ( entry => entry . temperature ) ;
121- var total = 0 ;
129+ let total = 0 ;
122130 for ( var power of powerList ) {
123131 total += power ;
124132 }
125133 const average = total / powerList . length ;
126134 const totalProducedkWh = ( average * hoursDifferenceBetweedDates ) / 1000 ;
127135
128- document . getElementById ( elementId + "-information" ) . innerHTML = "<h5>Average of " + hoursDifferenceBetweedDates . toFixed ( 2 ) + " hours: " + average . toFixed ( 2 ) + "Wh</h5>"
129- document . getElementById ( elementId + "-information" ) . innerHTML += "<h5>Leads to <b>" + totalProducedkWh . toFixed ( 2 ) + "kWh</b> produced in " + hoursDifferenceBetweedDates . toFixed ( 2 ) + " hours</h5>" ;
136+ informationElement . innerHTML = "<h5>Average of " + hoursDifferenceBetweedDates . toFixed ( 2 ) + " hours: " + average . toFixed ( 2 ) + "Wh</h5>" ;
137+ informationElement . innerHTML += "<h5>Leads to <b>" + totalProducedkWh . toFixed ( 2 ) + "kWh</b> produced in " + hoursDifferenceBetweedDates . toFixed ( 2 ) + " hours</h5>" ;
130138
131139 const options = {
132140 series : [ {
@@ -176,7 +184,7 @@ <h1>MyStrom Results</h1>
176184 }
177185 } ;
178186
179- const chart = new ApexCharts ( document . querySelector ( "#" + elementId + "-chart" ) , options ) ;
187+ const chart = new ApexCharts ( chartElement , options ) ;
180188 chart . render ( ) ;
181189
182190 } )
0 commit comments