-var chart = c3.generate({
- bindto: '#chart',
- data: {
- url: '/data',
- mimeType: 'json',
- xs: {
- 'water_temperature': 'water_temperature_x',
- 'air_temperature': 'air_temperature_x'
- },
- names: {
- 'water_temperature': 'Wassertemperatur',
- 'air_temperature': 'Lufttemperatur'
- }
- },
- axis: {
- x: {
- type: 'timeseries',
- tick: {
- format: '%Y-%m-%d'
+var api_base_url = '/api/1/sensor/type/Wassertemperatur?mode=consolidated&format=c3';
+
+function dayschart(element, interval, days, xtickformat) {
+ var chart = c3.generate({
+ bindto: '#' + element,
+ size: {
+ width: document.getElementById(element).parentElement.clientWidth * 80 / 100
+ },
+ transition: {
+ duration: null
+ },
+ title: {
+ text: 'Der See (' + interval + ')'
+ },
+ data: {
+ url: api_base_url + beginend(days),
+ mimeType: 'json',
+ xs: {
+ '0316a2193bff': '0316a2193bff_x',
+ '0316a21383ff': '0316a21383ff_x'
+ },
+ names: {
+ '0316a2193bff': 'Wassertemperatur in 4 m Tiefe',
+ '0316a21383ff': 'Wassertemperatur in 5 m Tiefe'
+ },
+ xFormat: '%Y-%m-%d %H:%M:%S',
+ colors: {
+ '0316a2193bff': 'lightblue',
+ '0316a21383ff': 'darkblue'
}
},
- y: {
- label: {
- text: '°C',
- position: 'outer-middle'
+ axis: {
+ x: {
+ type: 'timeseries',
+ tick: {
+ format: xtickformat,
+ fit: false,
+ multiline: true, // broken? so →
+ rotate: -90
+ }
+ },
+ y: {
+ label: {
+ text: 'Temperatur in °C',
+ position: 'outer-middle'
+ },
+ tick: {
+ format: function (d) {
+ return d.toFixed(1) + '°';
+ }
+ }
}
- }
- },
- zoom: {
- enabled: true
- }
-});
+ },
+ zoom: {
+ enabled: true,
+ onzoomend: function (domain) {
+ // UNIX epoch
+ var start = domain[0].getTime()/1000;
+ var end = domain[1].getTime()/1000;
+ var duration = end - start;
+ // duration is now the diff of the "viewport in seconds"
+ // FIXME
+ var days = Math.round(duration / 86400 / 2);
+ if (days > 0) {
+ loaddays(days);
+ }
+ }
+ },
+ tooltip: {
+ format: {
+ name: function (name, ratio, id, index) {
+ var re = /\d m/;
+ var res = re.exec(name);
+ return res;
+ },
+ value: function (value, ratio, id, index) {
+ return value.toFixed(2) + '°';
+ },
+ title: function (x) {
+ // TODO: format datetime into something readable
+ return x;
+ }
+ }
+ },
+ grid: {
+ y: {
+ show: true
+ }
+ },
+ regions: [
+ {axis: 'y', start: -999, end: 0, class: 'region-frozen'},
+ {axis: 'y', start: 0, end: 25, class: 'region-ok'},
+ {axis: 'y', start: 25, end: 100, class: 'region-warm'},
+ {axis: 'y', start: 100, end: 999, class: 'region-boiling'}
+ ]
+ });
+ };
+
+function loaddays(days) {
+ chart.load({
+ url: api_base_url + beginend(days),
+ mimeType: 'json'
+ });
+}
+
+function beginend(days) {
+ var now = Date.now();
+ var end = new Date(now).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
+ var begin = new Date(now - days * 60*60*24 * 1000).toISOString().substr(0, 19);
+ return '&begin=' + begin + '&end=' + end;
+}
+dayschart('chart1', 'Tag', 1, '%H:%M');
+dayschart('chart7', 'Woche', 7, '%a %d');
+dayschart('chart31', 'Monat', 31, 'Woche %V');
+dayschart('chart365', 'Jahr', 365, '%b %d');