]> ToastFreeware Gitweb - chrisu/seepark.git/blob - web/static/seepark_web.js
js: comment out onzoomed
[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: document.getElementById(element).parentElement.clientWidth * 80 / 100
62                 },
63                 transition: {
64                         duration: null
65                 },
66                 title: {
67                         text: title
68                 },
69                 data: chartdata,
70                 point: {
71                         r: 1.5 // default 2.5
72                 },
73                 axis: {
74                         x: {
75                                 type: 'timeseries',
76                                 tick: {
77                                         format: xtickformat,
78                                         fit: false,
79                                         multiline: true, // broken? so →
80                                         rotate: -90
81                                 }
82                         },
83                         y: {
84                                 label: {
85                                         text: 'Temperatur in °C',
86                                         position: 'outer-middle'
87                                 },
88                                 tick: {
89                                         format: function (d) {
90                                                 return d.toFixed(1) + '°';
91                                         }
92                                 }
93                         }
94                 },
95                 zoom: {
96                         enabled: true,
97                         /*
98                         onzoomend: function (domain) {
99                                 // UNIX epoch
100                                 var start = domain[0].getTime()/1000;
101                                 var end   = domain[1].getTime()/1000;
102                                 var duration = end - start;
103                                 // duration is now the diff of the "viewport in seconds"
104                                 // FIXME
105                                 var days = Math.round(duration / 86400 / 2);
106                                 if (days > 0) {
107                                         loaddays(this, element, days); // this = chart
108                                 }
109                         }
110                         */
111                 },
112                 tooltip: {
113                         format: {
114                                 // TODO: maybe move to top
115                                 name: function (name, ratio, id, index) {
116                                         var prefix = '';
117                                         if (element.substr(0, 7) == 'chart_w') {
118                                                 var re = /\d+ ?c?m/;
119                                                 prefix = 'Wasser ';
120                                         } else {
121                                                 var re = /Luft/;
122                                         };
123                                         var result = re.exec(name);
124                                         return prefix + result;
125                                 },
126                                 value: function (value, ratio, id, index) {
127                                         return value.toFixed(2) + '°';
128                                 },
129                                 title: function (datetime) {
130                                         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
131                                         options = {
132                                                 weekday: 'short',
133                                                 year: 'numeric', month: 'numeric', day: 'numeric',
134                                                 hour: 'numeric', minute: 'numeric', second: 'numeric',
135                                                 timeZone: 'Europe/Vienna', timeZoneName: 'short'
136                                         };
137                                         return datetime.toLocaleString('de-AT', options);
138                                 }
139                         }
140                 },
141                 grid: {
142                         y: {
143                                 show: true
144                         }
145                 },
146                 regions: [
147                         {axis: 'y', start: -999, end:   0, class: 'region-frozen'},
148                         {axis: 'y', start:    0, end:  25, class: 'region-ok'},
149                         {axis: 'y', start:   25, end: 100, class: 'region-warm'},
150                         {axis: 'y', start:  100, end: 999, class: 'region-boiling'}
151                 ]
152         });
153 };
154
155 function loaddays(chart, element, days) {
156         // TODO: maybe move to top or factor out somehow
157         if (element.substr(0, 7) == 'chart_w') {
158                 chart.load({
159                         url: api_base_url_sensors + beginend(days),
160                         mimeType: 'json'
161                 });
162         } else {
163                 chart.load({
164                         url: api_base_url_weather + beginend(days),
165                         mimeType: 'json'
166                 });
167         }
168 }
169
170 function beginend(days) {
171         var now = Date.now();
172
173         // TODO: this timezone calculation relies on the browser being in Europe/Vienna
174         var refdate = new Date(now);
175         var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
176
177         var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
178         var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
179         return '&begin=' + begin + '&end=' + end;
180 }
181
182 dayschart('chart_water_1',   'Der See (Tag)',     1, '%H:%M');
183 dayschart('chart_water_7',   'Der See (Woche)',   7, '%a %d');
184 dayschart('chart_water_31',  'Der See (Monat)',  31, '%Y-%m-%d');
185 dayschart('chart_water_365', 'Der See (Jahr)',  365, '%b %Y');
186
187 dayschart('chart_air_1',     'Die Luft (Tag)',     1, '%H:%M');
188 dayschart('chart_air_7',     'Die Luft (Woche)',   7, '%a %d');
189 dayschart('chart_air_31',    'Die Luft (Monat)',  31, '%Y-%m-%d');
190 dayschart('chart_air_365',   'Die Luft (Jahr)',  365, '%b %Y');