47bd334326207a051bfe7a2e5683ddc6553265ef
[chrisu/seepark.git] / web / static / seepark_web.js
1 var api_base_url_sensors = '/api/1/sensor/type/Wassertemperatur?mode=consolidated&format=c3';
2 var api_base_url_weather = '/api/1/openweathermap/city/3319578?mode=consolidated&format=c3';
3
4 function dayschart(element, title, days, xtickformat) {
5         var chartdata;
6         if (element.substr(0, 7) == 'chart_w') {
7                 chartdata = {
8                                 url: api_base_url_sensors + beginend(days),
9                                 mimeType: 'json',
10                                 xs: {
11                                         '0416a1ac66ff': '0416a1ac66ff_x',
12                                         '0516a207a4ff': '0516a207a4ff_x',
13                                         '0416a1bab9ff': '0416a1bab9ff_x',
14                                         '0316a2193bff': '0316a2193bff_x'
15                                 },
16                                 names: {
17                                         '0416a1ac66ff': 'Wassertemperatur 30cm Tiefe',
18                                         '0516a207a4ff': 'Wassertemperatur 50cm Tiefe',
19                                         '0416a1bab9ff': 'Wassertemperatur 1m Tiefe',
20                                         '0316a2193bff': 'Wassertemperatur in 30 cm Tiefe'
21                                 },
22                                 xFormat: '%Y-%m-%d %H:%M:%S',
23                                 // https://en.wikipedia.org/wiki/Web_colors
24                                 colors: {
25                                         '0416a1ac66ff': 'lightskyblue',
26                                         '0516a207a4ff': 'royalblue',
27                                         '0416a1bab9ff': 'mediumblue',
28                                         '0316a2193bff': 'midnightblue'
29                                 },
30                                 /*
31                                 xSort: false,
32                                 order: function (a, b) {
33                                         // gets 2 x objects. seems to be a "sort function" for js's sort()
34                                         // -1: a before b etc.
35                                         // only handle cases that appear in the wild
36                                         // console.log("a.id", a.id, "b.id", b.id);
37                                         if (a.id == '0416a1ac66ff') {return -1};
38                                         if (a.id == '0416a1bab9ff') {return  1};
39                                 }
40                                 */
41                 };
42         } else {
43                 chartdata = {
44                                 url: api_base_url_weather + beginend(days),
45                                 mimeType: 'json',
46                                 xs: {
47                                         '3319578': '3319578_x'
48                                 },
49                                 names: {
50                                         '3319578': 'Lufttemperatur in Obsteig'
51                                 },
52                                 xFormat: '%Y-%m-%d %H:%M:%S',
53                                 colors: {
54                                         '3319578': 'red'
55                                 }
56                 };
57         };
58         var chart = c3.generate({
59                 bindto: '#' + element,
60                 size: {
61                         width: getwidth(element)
62                 },
63                 onresized: function () {
64                         chart.resize({
65                                 width: getwidth(element)
66                         });
67                         // chart.flush();
68                 },
69                 transition: {
70                         duration: null
71                 },
72                 title: {
73                         text: title
74                 },
75                 data: chartdata,
76                 point: {
77                         r: 1.5 // default 2.5
78                 },
79                 axis: {
80                         x: {
81                                 type: 'timeseries',
82                                 tick: {
83                                         format: function (d) {
84                                                 var strftimeDE = strftime.localizeByIdentifier('de_DE');
85                                                 return strftimeDE(xtickformat, new Date(d));
86                                         },
87                                         fit: false,
88                                         multiline: true, // broken? so →
89                                         rotate: -90
90                                 }
91                         },
92                         y: {
93                                 label: {
94                                         text: 'Temperatur in °C',
95                                         position: 'outer-middle'
96                                 },
97                                 tick: {
98                                         format: function (d) {
99                                                 return d.toFixed(1) + '°';
100                                         }
101                                 }
102                         }
103                 },
104                 zoom: {
105                         enabled: true,
106                         /*
107                         onzoomend: function (domain) {
108                                 // UNIX epoch
109                                 var start = domain[0].getTime()/1000;
110                                 var end   = domain[1].getTime()/1000;
111                                 var duration = end - start;
112                                 // duration is now the diff of the "viewport in seconds"
113                                 // FIXME
114                                 var days = Math.round(duration / 86400 / 2);
115                                 if (days > 0) {
116                                         loaddays(this, element, days); // this = chart
117                                 }
118                         }
119                         */
120                 },
121                 tooltip: {
122                         format: {
123                                 // TODO: maybe move to top
124                                 name: function (name, ratio, id, index) {
125                                         var prefix = '';
126                                         if (element.substr(0, 7) == 'chart_w') {
127                                                 var re = /\d+ ?c?m/;
128                                                 prefix = 'Wasser ';
129                                         } else {
130                                                 var re = /Luft/;
131                                         };
132                                         var result = re.exec(name);
133                                         return prefix + result;
134                                 },
135                                 value: function (value, ratio, id, index) {
136                                         return value.toFixed(2) + '°';
137                                 },
138                                 title: function (datetime) {
139                                         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
140                                         options = {
141                                                 weekday: 'short',
142                                                 year: 'numeric', month: 'numeric', day: 'numeric',
143                                                 hour: 'numeric', minute: 'numeric', second: 'numeric',
144                                                 timeZone: 'Europe/Vienna', timeZoneName: 'short'
145                                         };
146                                         return datetime.toLocaleString('de-AT', options);
147                                 }
148                         }
149                 },
150                 grid: {
151                         y: {
152                                 show: true
153                         }
154                 },
155                 regions: [
156                         {axis: 'y', start: -999, end:   0, class: 'region-frozen'},
157                         {axis: 'y', start:    0, end:  25, class: 'region-ok'},
158                         {axis: 'y', start:   25, end: 100, class: 'region-warm'},
159                         {axis: 'y', start:  100, end: 999, class: 'region-boiling'}
160                 ]
161         });
162 };
163
164 function loaddays(chart, element, days) {
165         // TODO: maybe move to top or factor out somehow
166         if (element.substr(0, 7) == 'chart_w') {
167                 chart.load({
168                         url: api_base_url_sensors + beginend(days),
169                         mimeType: 'json'
170                 });
171         } else {
172                 chart.load({
173                         url: api_base_url_weather + beginend(days),
174                         mimeType: 'json'
175                 });
176         }
177 }
178
179 function beginend(days) {
180         var now = Date.now();
181
182         // TODO: this timezone calculation relies on the browser being in Europe/Vienna
183         var refdate = new Date(now);
184         var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
185
186         var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
187         var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
188         return '&begin=' + begin + '&end=' + end;
189 }
190
191 function getwidth(element) {
192         return document.getElementById(element).parentElement.clientWidth * 80 / 100;
193 }
194
195 dayschart('chart_water_1',   'Der See (Tag)',     1, '%H:%M');
196 dayschart('chart_water_7',   'Der See (Woche)',   7, '%a %d');
197 dayschart('chart_water_31',  'Der See (Monat)',  31, '%Y-%m-%d');
198 dayschart('chart_water_365', 'Der See (Jahr)',  365, '%b %Y');
199
200 dayschart('chart_air_1',     'Die Luft (Tag)',     1, '%H:%M');
201 dayschart('chart_air_7',     'Die Luft (Woche)',   7, '%a %d');
202 dayschart('chart_air_31',    'Die Luft (Monat)',  31, '%Y-%m-%d');
203 dayschart('chart_air_365',   'Die Luft (Jahr)',  365, '%b %Y');