question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart won't update with state change

See original GitHub issue

I’m testing an app that toggles a chart between two data objects: chart1 and chart2. I have an App component which handles the rendering of a Bar chart and a Button. When the Button is clicked I call a function in App to update the state to reference either chart1 or chart2 depending on a boolean.

This works successfully the first time I click the button. The state changes in App and it re-renders the Bar component with the new data. However, if I click the button again the state doesn’t change and the chart doesn’t update. Only the initial button click does anything. I’ve tested without react-chartjs-2 using plain components and everything works fine - each button click changes the data.

Am I using the Bar component wrong? I’m passing the state of App as its props and it works fine initially, then stops updating after the first button click.

import React from 'react';
import ReactDOM from 'react-dom';
import { Bar } from 'react-chartjs-2';

const chart1 = {
  labels: ['Team1', 'Team2', 'Team3', 'Team4', 'Team5'],
  datasets: [{
    label: 'Team points',
    data: [503, 385, 270, 133, 65],
    backgroundColor: [
      '#4DB6AC',
      '#E57373',
      '#7986CB',
      '#F06292',
      '#E0E0E0'
    ]
  }]
};
const chart2 = {
  labels: ['Team1', 'Team2', 'Team3', 'Team4', 'Team5'],
  datasets: [{
    label: 'Team points 2',
    data: [303, 185, 470, 313, 65],
    backgroundColor: [
      '#4DB6AC',
      '#E57373',
      '#7986CB',
      '#F06292',
      '#E0E0E0'
    ]
  }]
};

const Button = (props) => (
  <button id="update-chart" onClick={props.handleOnClick}>Update</button>
);

class App extends React.Component {
  constructor(props) {
    super(props);

    this.handleUpdate = this.handleUpdate.bind(this);

    this.updated = false;

    this.state = {
      chartData: chart1
    }

    console.log(this.state.chartData);
    console.log(this.updated);
  }

  // Toggle between chart1 and chart2 based on value of updated
  handleUpdate() {

    const chartData = this.updated ? chart1 : chart2;
    this.setState({chartData}, () => {
      this.updated = this.updated ? false : true;
      console.log(this.state.chartData);
    });
  }

  render() {
    return(
      <div>
        <Bar data={this.state.chartData} width={650} height={400} />
        <Button handleOnClick={this.handleUpdate} />
      </div>
    );
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('chart-container')
);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:12

github_iconTop GitHub Comments

12reactions
valentinorusconicommented, Mar 2, 2018

I have the same issue and my state change but the chart just update with the first click.

4reactions
graysonhickscommented, Oct 5, 2017

Okay, so you can keep the chart1 and chart2 where they are and keep the handleUpdate the same. Make chart1 and chart2 anonymous functions that return the data object, like this:

const chart1 = () => {
	return {
		labels: ["Team1", "Team2", "Team3", "Team4", "Team5"],
		datasets: [
			{
				label: "Team points",
				data: [503, 385, 270, 133, 65],
				backgroundColor: ["#4DB6AC", "#E57373", "#7986CB", "#F06292", "#E0E0E0"]
			}
		]
	};
};

const chart2 = () => {
	return {
		labels: ["Team1", "Team2", "Team3", "Team4", "Team5"],
		datasets: [
			{
				label: "Team points 2",
				data: [303, 185, 470, 313, 65],
				backgroundColor: ["#4DB6AC", "#E57373", "#7986CB", "#F06292", "#E0E0E0"]
			}
		]
	};
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart not updating on state change - javascript - Stack Overflow
The problem now is that the chart is not directly updating when state changes, (until I force refresh), so what may be causing...
Read more >
Update the data in an existing chart - Microsoft Support
Learn how to update the data in an existing chart from its source. Edit a chart in Excel, create a chart from a...
Read more >
Pie chart graph not updating | MrExcel Message Board
I have a spreadsheet with a number of pie charts on it however when the information feeding into them is update, pie charts...
Read more >
Excel chart won't update, based on calculated cells - Super User
Everything else works and calculates fine but this one chart does not update when the data changes (and the data calculates with a...
Read more >
How to auto update a chart after entering new data in Excel?
Supposing you have created a chart to track the daily sales based on a range of data in your workbook. But you need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found