@@ -67,49 +67,57 @@ <h1>MyStrom Results</h1>
67
67
< script src ="https://cdnjs.cloudflare.com/ajax/libs/apexcharts/3.35.3/apexcharts.min.js "> </ script >
68
68
69
69
< script type ="application/javascript ">
70
- var shown = [ ]
70
+ let shown = [ ] ;
71
71
72
72
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 = "" ;
76
76
}
77
77
78
78
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 ;
82
82
if ( shown . includes ( id ) ) {
83
- document . getElementById ( elementId ) . outerHTML = ""
83
+ document . getElementById ( elementId ) . outerHTML = "" ;
84
84
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 ) ;
87
87
} 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 ) ;
91
91
92
- informationElement = document . createElement ( "div" )
93
- informationElement . setAttribute ( "id" , elementId + "-information" )
92
+ const informationElement = document . createElement ( "div" ) ;
93
+ informationElement . setAttribute ( "id" , elementId + "-information" ) ;
94
94
createdElement . appendChild ( informationElement ) ;
95
95
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 ) ;
99
99
100
- chart = loadChart ( id , elementId , startDateElement . value , endDateElement . value )
100
+ loadChart ( id , elementId , startDateElement . value , endDateElement . value ) ;
101
101
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 ) ;
104
104
}
105
105
106
106
}
107
107
108
108
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 ) ;
112
112
$ . 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
+ }
113
121
const firstDate = new Date ( data [ 0 ] . date ) ;
114
122
const lastDate = new Date ( data [ data . length - 1 ] . date ) ;
115
123
const differenceBetweenDates = lastDate - firstDate ;
@@ -118,15 +126,15 @@ <h1>MyStrom Results</h1>
118
126
const powerList = data . map ( entry => entry . power ) ;
119
127
const WsList = data . map ( entry => entry . ws ) ;
120
128
const temperatureList = data . map ( entry => entry . temperature ) ;
121
- var total = 0 ;
129
+ let total = 0 ;
122
130
for ( var power of powerList ) {
123
131
total += power ;
124
132
}
125
133
const average = total / powerList . length ;
126
134
const totalProducedkWh = ( average * hoursDifferenceBetweedDates ) / 1000 ;
127
135
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>" ;
130
138
131
139
const options = {
132
140
series : [ {
@@ -176,7 +184,7 @@ <h1>MyStrom Results</h1>
176
184
}
177
185
} ;
178
186
179
- const chart = new ApexCharts ( document . querySelector ( "#" + elementId + "-chart" ) , options ) ;
187
+ const chart = new ApexCharts ( chartElement , options ) ;
180
188
chart . render ( ) ;
181
189
182
190
} )
0 commit comments