+var api_base_url_sensors = '/api/1/sensor/type/Wassertemperatur?mode=consolidated&format=c3';
+var api_base_url_weather = '/api/1/openweathermap/city/3319578?mode=consolidated&format=c3';
+
+function dayschart(element, title, days, xtickformat) {
+ var chartdata;
+ if (element.substr(0, 7) == 'chart_w') {
+ chartdata = {
+ url: api_base_url_sensors + 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'
+ }
+ };
+ } else {
+ chartdata = {
+ url: api_base_url_weather + beginend(days),
+ mimeType: 'json',
+ xs: {
+ '3319578': '3319578_x'
+ },
+ names: {
+ '3319578': 'Lufttemperatur in Obsteig'
+ },
+ xFormat: '%Y-%m-%d %H:%M:%S',
+ colors: {
+ '3319578': 'red'
+ }
+ };
+ };
+ var chart = c3.generate({
+ bindto: '#' + element,
+ size: {
+ width: document.getElementById(element).parentElement.clientWidth * 80 / 100
+ },
+ transition: {
+ duration: null
+ },
+ title: {
+ text: title
+ },
+ data: chartdata,
+ 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,
+ 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: {
+ // TODO: maybe move to top
+ name: function (name, ratio, id, index) {
+ var prefix = '';
+ if (element.substr(0, 7) == 'chart_w') {
+ var re = /\d m/;
+ prefix = 'Wasser ';
+ } else {
+ var re = /Luft/;
+ };
+ var result = re.exec(name);
+ return prefix + result;
+ },
+ value: function (value, ratio, id, index) {
+ return value.toFixed(2) + '°';
+ },
+ title: function (x) {
+ // TODO: format datetime into something readable
+ return x;
+ }