Files
old-spike/client/dashboard/power/graph/graph.component.js

75 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-02-29 18:20:00 -06:00
import React from 'react';
import Templates from 'config/templates';
import House from './../../../models/house';
2016-03-08 15:21:16 -06:00
import c3 from 'c3';
2016-02-29 18:20:00 -06:00
class GraphComponent extends React.Component {
2016-03-02 12:07:31 -06:00
get house(){
2016-03-04 13:59:34 -06:00
return this.state_manager.state.house;
2016-02-29 18:20:00 -06:00
}
2016-03-04 13:59:34 -06:00
get state_manager(){
return this.props.state_manager;
2016-02-29 18:20:00 -06:00
}
2016-03-05 16:53:29 -06:00
componentDidMount(){
var power_graph = this;
power_graph.updateGraph();
}
2016-03-04 13:59:34 -06:00
componentDidUpdate(prev_props, prev_state){
2016-03-02 12:07:31 -06:00
var power_graph = this;
2016-03-04 13:59:34 -06:00
if (prev_props.house != power_graph.props.house || prev_props.power_range != power_graph.props.power_range){
power_graph.updateGraph();
}
2016-03-02 12:07:31 -06:00
}
2016-02-29 18:20:00 -06:00
updateGraph(){
var power_graph = this,
2016-03-08 15:21:16 -06:00
house = power_graph.house,
data = {
x: 'x',
json: house.power_data,
keys: {
x: 'time_to_date',
value: ['net_consumption', 'production']
},
type: 'area-spline',
groups: [['net_consumption', 'production']],
names: {
net_consumption: 'Net Consumption',
production: 'Production'
}
};
2016-02-29 18:20:00 -06:00
if (power_graph.graph === undefined){
2016-03-08 15:21:16 -06:00
power_graph.chart = c3.generate({
bindto: '#power_graph',
data: data,
axis: {
x: {
type: 'timeseries',
tick: { format: d3.time.format('%d %B %y') }
}
2016-02-29 18:20:00 -06:00
}
});
2016-03-08 15:21:16 -06:00
} else {
power_graph.chart.load({
unload: true,
data: data
2016-02-29 18:20:00 -06:00
});
}
}
render() {
var powerGraphRt = Templates.forComponent('power_graph');
return powerGraphRt.call(this);
}
}
2016-03-05 16:53:29 -06:00
GraphComponent.NAME = 'PowerGraph';
2016-03-04 13:59:34 -06:00
module.exports = GraphComponent;