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';
4 function dayschart(element, title, days, xtickformat) {
6 if (element.substr(0, 7) == 'chart_w') {
8 url: api_base_url_sensors + beginend(days),
11 '0416a1ac66ff': '0416a1ac66ff_x',
12 '0516a207a4ff': '0516a207a4ff_x',
13 '0416a1bab9ff': '0416a1bab9ff_x',
14 '0316a2193bff': '0316a2193bff_x'
17 '0416a1ac66ff': 'Wassertemperatur 30cm Tiefe',
18 '0516a207a4ff': 'Wassertemperatur 50cm Tiefe',
19 '0416a1bab9ff': 'Wassertemperatur 1m Tiefe',
20 '0316a2193bff': 'Wassertemperatur in 30 cm Tiefe'
22 xFormat: '%Y-%m-%d %H:%M:%S',
23 // https://en.wikipedia.org/wiki/Web_colors
25 '0416a1ac66ff': 'lightskyblue',
26 '0516a207a4ff': 'royalblue',
27 '0416a1bab9ff': 'mediumblue',
28 '0316a2193bff': 'midnightblue'
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};
44 url: api_base_url_weather + beginend(days),
47 '3319578': '3319578_x'
50 '3319578': 'Lufttemperatur in Obsteig'
52 xFormat: '%Y-%m-%d %H:%M:%S',
58 var chart = c3.generate({
59 bindto: '#' + element,
61 width: document.getElementById(element).parentElement.clientWidth * 80 / 100
63 onresized: function () {
65 width: document.getElementById(element).parentElement.clientWidth * 80 / 100
85 multiline: true, // broken? so →
91 text: 'Temperatur in °C',
92 position: 'outer-middle'
95 format: function (d) {
96 return d.toFixed(1) + '°';
104 onzoomend: function (domain) {
106 var start = domain[0].getTime()/1000;
107 var end = domain[1].getTime()/1000;
108 var duration = end - start;
109 // duration is now the diff of the "viewport in seconds"
111 var days = Math.round(duration / 86400 / 2);
113 loaddays(this, element, days); // this = chart
120 // TODO: maybe move to top
121 name: function (name, ratio, id, index) {
123 if (element.substr(0, 7) == 'chart_w') {
129 var result = re.exec(name);
130 return prefix + result;
132 value: function (value, ratio, id, index) {
133 return value.toFixed(2) + '°';
135 title: function (datetime) {
136 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
139 year: 'numeric', month: 'numeric', day: 'numeric',
140 hour: 'numeric', minute: 'numeric', second: 'numeric',
141 timeZone: 'Europe/Vienna', timeZoneName: 'short'
143 return datetime.toLocaleString('de-AT', options);
153 {axis: 'y', start: -999, end: 0, class: 'region-frozen'},
154 {axis: 'y', start: 0, end: 25, class: 'region-ok'},
155 {axis: 'y', start: 25, end: 100, class: 'region-warm'},
156 {axis: 'y', start: 100, end: 999, class: 'region-boiling'}
161 function loaddays(chart, element, days) {
162 // TODO: maybe move to top or factor out somehow
163 if (element.substr(0, 7) == 'chart_w') {
165 url: api_base_url_sensors + beginend(days),
170 url: api_base_url_weather + beginend(days),
176 function beginend(days) {
177 var now = Date.now();
179 // TODO: this timezone calculation relies on the browser being in Europe/Vienna
180 var refdate = new Date(now);
181 var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
183 var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
184 var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
185 return '&begin=' + begin + '&end=' + end;
188 dayschart('chart_water_1', 'Der See (Tag)', 1, '%H:%M');
189 dayschart('chart_water_7', 'Der See (Woche)', 7, '%a %d');
190 dayschart('chart_water_31', 'Der See (Monat)', 31, '%Y-%m-%d');
191 dayschart('chart_water_365', 'Der See (Jahr)', 365, '%b %Y');
193 dayschart('chart_air_1', 'Die Luft (Tag)', 1, '%H:%M');
194 dayschart('chart_air_7', 'Die Luft (Woche)', 7, '%a %d');
195 dayschart('chart_air_31', 'Die Luft (Monat)', 31, '%Y-%m-%d');
196 dayschart('chart_air_365', 'Die Luft (Jahr)', 365, '%b %Y');