function Chart (id) { this.id = id; this.timezoneOffset = (new Date()).getTimezoneOffset() * 60 * 1000; this.startDate = null; this.endDate = null; this.step = null; this.stationLogs = null; this.data = {}; this.liveUpdateEnabled = false; } Chart.prototype.draw = function () { var self = this; $(this.id).highcharts('StockChart', { rangeSelector : { selected : 1 }, chart: { type: 'spline' }, title: { text: 'WLAN Capacity' }, xAxis: { type: 'datetime', dateTimeLabelFormats: { month: '%e. %b', year: '%b' }, title: { text: 'Date' } }, yAxis: { title: { text: 'Bandwidth in MBit/s' }, min: 0 }, tooltip: { //pointFormat: '{point.x:%e. %b}: {point.y:.2f} MBit/s' formatter: function () { var timestamp = new Date(this.x); var send = Math.round(this.points[0].y * 100) / 100; var capacity = Math.round(this.points[1].y * 100) / 100; var index = this.points[0].point.index; var frequency = self.data.frequency && self.data.frequency[index] && self.data.frequency[index][1]; frequency = frequency ? frequency + ' MHz' : ''; return '' + timestamp.toISOString() + '
' + send + ' MBit/s ' + '' + capacity + 'MBit/s ' + frequency; }, shared: true }, plotOptions: { spline: { marker: { enabled: true } } }, series: [{ name: 'send', data: this.data.send }, { name: 'capacity', data: this.data.capacity }] }); this.chart = $(this.id).highcharts(); return Promise.resolve(); }; Chart.prototype.append = function (offset) { var self = this; var send = this.chart.series[0]; this.data.send.slice(offset).forEach(function (point) { send.addPoint(point, true, true); }); var capacity = this.chart.series[1]; this.data.capacity.slice(offset).forEach(function (point) { capacity.addPoint(point, true, true); }); }; Chart.prototype.update = function (append) { var self = this; if (!append) { this.data = { send: [], capacity: [], frequency: [] }; } var offset = (this.data.send && this.data.send.length) || 0; return this.fetchStationLog().then(function () { self.buildSeries(); }).then(function () { if (!append) return self.draw(); else return self.append(offset); }).then(function () { return self.buildData(); }); }; Chart.prototype.liveUpdate = function () { var that = this; if (!this.liveUpdateEnabled) return; this.startDate = this.endDate; this.endDate = new Date(); this.update(true).then(function () { setTimeout(that.liveUpdate.bind(that), 30 * 1000); }).catch(function (error) { console.error(error); }); }; Chart.prototype.enableLiveUpdate = function (enable) { this.liveUpdateEnabled = enable; this.liveUpdate(); }; Chart.prototype.fetchStationLog = function () { var that = this; this.endDate = this.endDate || new Date(); var params = { startDate: this.startDate.toISOString(), endDate: this.endDate.toISOString(), step: this.step }; return new Promise(function (resolve) { $.getJSON('station?' + jQuery.param(params), function (stationLogs) { that.stationLogs = stationLogs; resolve(); }); }); }; Chart.prototype.buildData = function () { var that = this; this.data.frequency = this.data.frequency.concat(this.stationLogs.map(function (entry) { var frequency = entry.frequency ? parseInt(entry.frequency.split(' ').shift()) : null; return [ Date.parse(entry.timestamp) - that.timezoneOffset, frequency ]; })); return Promise.resolve(); }; Chart.prototype.buildSeries = function () { var that = this; this.data.send = this.data.send.concat(this.stationLogs.map(function (entry) { return [ Date.parse(entry.timestamp) - that.timezoneOffset, entry.sendSpeed ]; })); this.data.capacity = this.data.capacity.concat(this.stationLogs.map(function (entry) { return [ Date.parse(entry.timestamp) - that.timezoneOffset, entry.capacity ]; })); return Promise.resolve(); }; var chart = new Chart('#speed-diagram'); function update () { $('#status').text('loading...'); var past = 24*60*60*1000 * parseFloat($('#speed-diagram-range').val()); chart.step = 0.1 * parseFloat($('#speed-diagram-detail').val()); chart.startDate = new Date(Date.now() - past); chart.update().then(function () { $('#status').text(''); }); } $(document).ready(function () { update(); $('#speed-diagram-range').change(update); $('#speed-diagram-detail').change(update); $('#update').click(update); $('#live-update').change(function () { var enable = $(this).is(':checked'); chart.enableLiveUpdate(enable); }); });