I used websocket to dynamically update state, and now the curve has changed, but the value on the axis is unchanged
I want the chart to show changes with the state changes, is there any way?
version: 0.22.3
import React, { Component } from 'react';
import { ComposedChart, Line, Area, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
export default class extends Component {
static defaultProps = {
chartData: [],
};
state = {
chartData: this.props.chartData,
}
componentWillReceiveProps({ chartData }) {
if (this.props.chartData !== chartData) {
this.setState({ chartData });
}
}
render() {
const { chartData } = this.state;
return (
<div>
{
chartData.length ? <ResponsiveContainer height={250} >
<ComposedChart data={chartData} margin={{ top: 0, right: -15, left: -8, bottom: 0 }} >
<XAxis dataKey="Name" />
<YAxis yAxisId="left" orientation="left" />
<YAxis yAxisId="right" orientation="right" />
<CartesianGrid strokeDasharray="3 3" />
<Tooltip />
<Legend />
<Area yAxisId='left' type='monotone' dataKey='TotalCount' fill={'#00BCD4'} stroke={'#00BCD4'} />
<Line yAxisId='right' type='monotone' dataKey='AverageElapsed' stroke={'#00E676'} unit='ms' />
<Bar yAxisId='right' type='monotone' dataKey='TotalErrors' fill={'#F44336'} />
</ComposedChart>
</ResponsiveContainer> : <div className='chart-empty'>empty</div>
}
</div>
)
}
}
The problem for me was that the API returned the numbers in a string format which confused Recharts.
It’s still an issues on version 1.8.1
No plan to fix this issue?
workaround to pass random key to force rerender…
Version: 1.0.0-alpha.2 I don’t know if this is the right place to ask but I’m too facing a similar problem. In my case I’m changing a particular data dynamically but the chart is not getting updated. The tooltips show the updated value. But not the chart. Here’s the part that I have used:
this.props.datais of the form:[{name: 'Something', value: number}]These are the PropTypes in case you need them:Here’s the function that I used to update the data:
The data generated through this is being passed as a prop to the component.
My
datawas mapped from an object that already had anid. SettingResponsiveContainer.keyto theidsolved it for me.My solution:
@xiaoyu-tamu , yes! That’s the solution that worked.
I ended up just passing the value from the state variable to the
keyprop all along the tree and it worked. Thank you for your help and suggestion, hopefully this is also useful to others.@joaocarmo, I noticed that pass random key to axis will cause some weird behavior.
Try this. Business logic removed, the idea is force chart to re-render when chartData change.
I’m having exactly the same issue as Rud156, tooltip gets update but chart doesn’t, the only way I was able to fix this was by setting the data in my state as [] and then updating it again with the new state.