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: getwidth(element)
63 onresized: function () {
65 width: getwidth(element)
83 format: function (d) {
84 var strftimeDE = strftime.localizeByIdentifier('de_DE');
85 return strftimeDE(xtickformat, new Date(d));
88 multiline: true, // broken? so →
94 text: 'Temperatur in °C',
95 position: 'outer-middle'
98 format: function (d) {
99 return d.toFixed(1) + '°';
107 onzoomend: function (domain) {
108 var duration = (domain[1] - domain[0]) / 1000; // duration is now the diff of the "viewport in seconds"
110 var days = duration / 86400;
112 loaddays(this, element, days); // this = chart
119 // TODO: maybe move to top
120 name: function (name, ratio, id, index) {
122 if (element.substr(0, 7) == 'chart_w') {
128 var result = re.exec(name);
129 return prefix + result;
131 value: function (value, ratio, id, index) {
132 return value.toFixed(2) + '°';
134 title: function (datetime) {
135 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
138 year: 'numeric', month: 'numeric', day: 'numeric',
139 hour: 'numeric', minute: 'numeric', second: 'numeric',
140 timeZone: 'Europe/Vienna', timeZoneName: 'short'
142 return datetime.toLocaleString('de-AT', options);
152 {axis: 'y', start: -999, end: 0, class: 'region-frozen'},
153 {axis: 'y', start: 0, end: 25, class: 'region-ok'},
154 {axis: 'y', start: 25, end: 100, class: 'region-warm'},
155 {axis: 'y', start: 100, end: 999, class: 'region-boiling'}
163 url: api_base_url_sensors + beginend(days),
172 function loaddays(chart, element, days) {
173 // TODO: maybe move to top or factor out somehow
174 if (element.substr(0, 7) == 'chart_w') {
176 url: api_base_url_sensors + beginend(days),
181 url: api_base_url_weather + beginend(days),
187 function beginend(days) {
188 var now = Date.now();
190 // TODO: this timezone calculation relies on the browser being in Europe/Vienna
191 var refdate = new Date(now);
192 var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
194 var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
195 var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
196 return '&begin=' + begin + '&end=' + end;
199 function getwidth(element) {
200 return document.getElementById(element).parentElement.clientWidth * 80 / 100;
203 dayschart('chart_water_1', 'Der See (Tag)', 1, '%H:%M');
204 dayschart('chart_water_7', 'Der See (Woche)', 7, '%a %d');
205 dayschart('chart_water_31', 'Der See (Monat)', 31, '%Y-%m-%d');
206 dayschart('chart_water_365', 'Der See (Jahr)', 365, '%b %Y');
208 dayschart('chart_air_1', 'Die Luft (Tag)', 1, '%H:%M');
209 dayschart('chart_air_7', 'Die Luft (Woche)', 7, '%a %d');
210 dayschart('chart_air_31', 'Die Luft (Monat)', 31, '%Y-%m-%d');
211 dayschart('chart_air_365', 'Die Luft (Jahr)', 365, '%b %Y');