1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/web/ synced 2025-03-16 00:00:03 +01:00
VILLASweb/app/components/line-chart.js

125 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-10-09 09:22:35 +02:00
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'div',
2015-10-09 09:22:35 +02:00
classNames: ['line-chart'],
attributeBindings: ['style'],
xaxisLength: 300,
2015-10-22 15:44:25 -04:00
minValue: '',
maxValue: '',
label: '',
2015-10-13 14:12:34 +02:00
updateTime: 100,
height: '100%',
useLabel: true,
2015-10-09 09:22:35 +02:00
init: function() {
this._super();
this.addObserver('data', this.dataDidChange);
},
2015-10-09 09:22:35 +02:00
didInsertElement: function() {
this._drawPlot();
Ember.run.later(this, function() {
this._drawPlot();
2015-10-13 14:12:34 +02:00
}, this.updateTime);
},
dataDidChange: function() {
this._drawPlot();
},
style: function() {
return "height: " + this.get('height') + ";";
}.property('height'),
2015-10-09 09:22:35 +02:00
_drawPlot: function() {
2015-10-13 14:12:34 +02:00
var element = this.get('element');
if (element && element.id) {
2015-10-22 15:44:25 -04:00
if (this.data) {
var values = this.data[0].data;
if (values.length > 0) {
// get first and last time stamp for plot
var firstTimestamp = values[0][0];
var lastTimestamp = values[values.length - 1][0];
var diff = lastTimestamp - firstTimestamp;
var diffValue = this.xaxisLength * 1000;
if (diff > diffValue) {
firstTimestamp = lastTimestamp - diffValue;
} else {
lastTimestamp = +firstTimestamp + +diffValue;
}
// generate plot options
var options = {
series: {
lines: {
show: true,
lineWidth: 2
},
shadowSize: 0
},
xaxis: {
mode: 'time',
timeformat: '%M:%S',
min: firstTimestamp,
max: lastTimestamp
},
yaxis: {
2015-10-22 15:44:25 -04:00
tickDecimals: 2
}
}
// set y axis scale
if (this.data.get('minValue')) {
options.yaxis.min = this.data.get('minValue');
2015-10-22 15:44:25 -04:00
} else if (this.get('minValue') !== '') {
options.yaxis.min = this.get('minValue');
}
if (this.data.get('maxValue')) {
options.yaxis.max = this.data.get('maxValue');
2015-10-22 15:44:25 -04:00
} else if (this.get('maxValue') !== '') {
options.yaxis.max = this.get('maxValue');
}
2015-10-22 15:44:25 -04:00
// setup plot data
var plotData = {
data: values,
color: "rgb(51, 153, 255)"
}
if (this.get('useLabel')) {
2015-10-22 15:44:25 -04:00
if (this.get('label') !== '') {
plotData.label = this.get('label');
} else {
plotData.label = this.data.get('name') + " [" + this.data.get('type') + "]";
}
}
2015-10-22 15:44:25 -04:00
// draw plot
2015-10-22 15:44:25 -04:00
$.plot('#' + element.id, this.data, options);
} else {
// display empty chart
$.plot('#' + element.id, [[]], {
xaxis: {
show: false
},
yaxis: {
show: false
}
});
}
}
}
2015-10-13 14:12:34 +02:00
// try again
Ember.run.later(this, function() {
this._drawPlot();
}, this.updateTime);
2015-10-09 09:22:35 +02:00
}
});