chart not updating
I'm trying to get my chart to update dynamically as new data comes in (from socket.io) so I have a method onChange(data) that is used as the callback function of setState called when the new data comes in.
The Chart however does not update itself... unless I refresh/resize the browser. If I set redraw={true}, then the chart updates but it seems to refresh the whole page (?) and kills the performance for the rest of the page...
Any idea what I'm doing wrong here?
import React from 'react'
import ReactDOM from 'react-dom'
import {Line} from 'react-chartjs-2'
module.exports = class MyChart extends React.Component {
constructor(props)
{
super(props)
this.buffersize
this.data = {
labels: [],
datasets: [
{/
label: 'My Chart',
data: []
}
]
}
}
onChange(newdata) {
const size = this.data.datasets[0].data.length
if (size >= this.buffersize)
{
this.data.datasets[0].data.shift()
this.data.labels.shift()
}
this.data.labels.push(newdata.label)
this.data.datasets[0].data.push(newdata.value)
}
render()
{
return (
<Line data={this.data}/>
)
}
}
hey @dmichel76 ,
Try to force a refresh by setting the key dynamically to any value. This would be a quick fix (if it works).
I'll try to check your example during this week.
Is it because you're not passing a fresh object to the Line Component so it can't tell the data has been updated? Could you tie your updates through your component's lifecycle to see if that would help? I know I've used GraphQL and react-chartjs-2 and I've had to make another wrapper for my graph object so it could detect changes.
@gor181 What do you mean by setting the key ? Do you mean the labels array ? if yes, then I do do that already with a this.data.labels.push()
@jerairrest I've just moved from using react-chart.js (which is limited to using Chart.js v1.1.1) and the same code works just fine, so it would seem unnecessary to create a new object.
By component lifecyle, do you mean using state ? If so, then I'm doing that already (although I admit it's not clear in my code) as the onChange() method is called by the setState() method. Or do you mean something else?
Sorry, so you're going this.setState(this.onChange(newData))
@jerairrest yes, I'm actually using socket.io to get data from the server so I've got something like:
socket.on(message, data => {
this.setState( lastupdated: data.timestamp, this.onChange(data))
})
Apologies that this was not clear in my initial post. I didn't want to pollute the code with Socket.io stuff as I thought it would distract from the actual issue.
@dmichel76 Don't you need to use the redraw prop like so? (This did it for me)
<Line data={this.data} redraw />
@nealoke Yes, as I said in my original post, when addingredraw={true}, the update occurs but it seems to refresh the whole page (one can see the reloading icon of the browser constancy on) rather than just the widget, affecting the performance of the whole page....
Same here. My charts all blinks and redraws. Is there another way to update chart?
After tinkering around, it seems that react-chartjs-2 "memoizes" properties of the chart, which creates an issue where chart.js is attempting to find a metadata value while updating the chart and throws an error:
Uncaught TypeError: Cannot read property 'skip' of undefined
Removing https://github.com/gor181/react-chartjs-2/blob/master/src/index.js#L155 seems to allow dynamic chart data updates without needing to use redraw.
So should we keep only the dataset to verify if we need to update the component?
Hi @dmichel76, I think I had the same issue at the beginning - cannot redraw the chart when the new data coming. So I tried to reload the whole chart configuration, here is the my code:
...
updateChart(chartData) {
this.setState(prevState => ({
chartLabel: chartData.dateArray,
volData: chartData.volTotalArray,
tempData: chartData.tempArray,
pressIn: chartData.PInArray,
pressOut: chartData.POutArray
}));
}
reloadChart() {
return {
labels: this.state.chartLabel,
datasets: [{
type: 'line',
label: 'Temperature',
data: this.state.tempData,
fill: false,
backgroundColor: '#f6d155',
borderColor: '#f6d155',
hoverBackgroundColor: '#20C80C',
hoverBorderColor: 'red',
yAxisID: 'y-axis-1'
},{
type: 'line',
label: 'Pressure In',
data: this.state.pressIn,
fill: false,
backgroundColor: '#f2552c',
borderColor: '#f2552c',
hoverBackgroundColor: '#20C80C',
hoverBorderColor: 'red',
yAxisID: 'y-axis-1'
},{
type: 'line',
label: 'Pressure Out',
data: this.state.pressOut,
fill: false,
backgroundColor: '#ce3175',
borderColor: '#ce3175',
hoverBackgroundColor: '#20C80C',
hoverBorderColor: 'red',
yAxisID: 'y-axis-1'
},{
label: 'Volume',
type:'bar',
data: this.state.volData,
fill: false,
borderColor: '#5484A4',
backgroundColor: '#5484A4',
pointBorderColor: '#00b377',
pointBackgroundColor: '#EC932F',
pointHoverBackgroundColor: '#EC932F',
pointHoverBorderColor: '#EC932F',
yAxisID: 'y-axis-2'
}]
};
}
....
render() {
return (
<div>
<Bar
data={this.reloadChart()}
options={options}
getElemmentAtEvent={clickedData => this.getClickData(clickedData)}
/>
</div>
)
}
I don't use socket.io here, but maybe you can try
socket.on(message, data => {
updateChart(data);
})
Hope this can be helpful for you.
@pokemonmegaman L155 seems to be empty line in current master branch. Could you update your answer please?
I got a similar issue where currentDatasets is copied from chart_instance.config.data which contains invalid _meta.controller.index. Thus failing to get the meta data.
Any ideas?
I m having the same issue. I have made, datasets.data = this.state.data. but Updating state does not update chartjs
Same problem : If I'm using redraw then the whole page is updating and I have a blink before changes appear. If I don't use reraw, there is an error showing in console from chartjs core.controller.js : TypeError: Cannot set property 'options' of undefined
Has anyone figured this out ?
Thanks
Same problem, I managed to trick the update by adding a state.change boolean that I change inside setState every time the data get updated. Using the chrome React extension I saw that there is clearly a problem in passing the update of the data between the Line component (in react-chartjs-2.js) and the ChartComponent in lib/index.js. Hope it helps to have a definitive fix soon.
@ludiofines Here's the updated location for the post @pokemonmegaman put.
This did not fix my issue with the graph redrawing though :(
Hello, as far as I got: usine redraw and shouldComponeneUpdate allows chart to redraw, once data updated. And only one time. Hope it will help.
Hello, this should do the trick https://stackoverflow.com/questions/49316914/react-chartjs-2-not-updating-graph-when-state-updates You don't need redraw or anything else, just make sure you do a copy of the labels and datasets. It worked for me.
@gor181 Thanks!
just using key={Math.random()} in my chartjs component did the trick.