@iotflows/iotflows-smart-widget
Version:
IoTFlows Smart Widget Node Module.
152 lines (141 loc) • 4.2 kB
JavaScript
import React from "react";
import Highcharts from "highcharts/highcharts.js";
import highchartsMore from "highcharts/highcharts-more.js";
import HighchartsReact from "highcharts-react-official";
// darkUnica(Highcharts);
highchartsMore(Highcharts);
export class IoTFlowsMultiLineChart extends React.Component {
constructor(props) {
super(props);
this.chartComponent = React.createRef();
this.childKey = 0
this.state = {
data:[]
}
}
componentDidMount() {
}
static getDerivedStateFromProps(nextProps, prevState){
if(nextProps.data!==prevState.data){
return { data: nextProps.data};
}
else return null;
}
// no need anymore because of shouldComponentUpdate
// componentDidUpdate(prevProps, prevState) {
// }
// PREVENT RE-RENDER of highcharts!
shouldComponentUpdate(nextProps, nextState) {
if(this.state.data !== nextState.data)
{
// this.chartComponent.current.chart.series[0].points[0].update(nextProps.data);
return false;
}
else
return true
}
render() {
const options = {
chart: {
type: 'spline'
},
title: {
text: 'Utilization Hours Per Month'
},
credits: {
text: 'iotflows.com',
href: 'http://www.iotflows.com'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%e. %b',
year: '%b'
},
title: {
text: 'Date'
}
},
yAxis: {
title: {
text: 'Utilization (hours)'
},
min: 0
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x:%e. %b}: {point.y:.2f} hours'
},
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
colors: [ '#539DDB', '#084A83'],
// Define the data points. All series have a dummy year
// of 1970/71 in order to be compared on the same x axis. Note
// that in JavaScript, months start at 0 for January, 1 for February etc.
series: [{
name: "2020",
data: [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
}, {
name: "2021",
data: [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
]
}],
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
plotOptions: {
series: {
marker: {
radius: 2.5
}
}
}
}
}]
}
}
++this.childKey;
return (
<HighchartsReact
containerProps={{ style: { height: "100%" } }}
ref={this.chartComponent}
highcharts={Highcharts}
options={options}
key={this.childKey}
/>
);
}
}