How to Update chart in react-chartjs-2?
Asked Answered
T

3

12

I call the update() function, but it does not work.

TypeError: line.update is not a function.

Why is update() not a function?

I have seen this example on http://jsfiddle.net/zpnx8ppb/26/ where the update function does work

Here is my code:

import React, { Component } from 'react';
import { Line } from 'react-chartjs-2';
import Chart from 'chart.js';

const line = {
    labels: [],
    datasets: [
       {
          label: 'My First dataset',
          fill: false,
          data: []
       }
    ]
};

setInterval(function(){
    line.labels.push(Math.floor(Math.random() * 100));
    line.datasets[0].data.push(Math.floor(Math.random() * 100));
    line.update();
}, 5000);

class LineChart extends Component {
    render() {
        return (
            <div className="chart">
                <Line
                    data={this.state}
                    height={5}
                    width={20}
                />
            </div>          
        )
    }
}

export default LineChart;
Thomism answered 5/9, 2017 at 10:56 Comment(2)
line you are trying to update is an object. set the ref for the Line component and try to update that ref. ( this is just a guess because I never used react-chartjs before )Planetstruck
const line = { -> line.update() there is not function defined update in the lin object that you made yourselfHuppert
H
2

You need to update the chart, line is just a config setting on the chart, this update needs to flow back to the handler

To set you on the right path, here is an example of what I mean

var config = {};

class Chart extends Component {
    constructor() {
        super();
        this.ctx = document.getElementById(this._rootNodeID).getContext("2d");
        this.chart = new Chart(ctx, config);

    }


    changeHandler(value) {
        this.chart.update();
    }

    render() {
        return (
            <canvas id={this._rootNodeID}>
                <LineChart value={this.state.value} 
                           config={this.config} 
                           onChange={this.changeHandler}/>
            </canvas>
        );
    }
}


const line = {
    labels: [],
    datasets: [
        {
            label: 'My First dataset',
            fill: false,
            data: []
        }
    ]
};



class LineChart extends Component {

    constructor(props) {
        super(props);

        this.props.config = line;
        setInterval(function(){
            this.props.config.labels.push(Math.floor(Math.random() * 100));
            this.props.config.datasets[0].data.push(Math.floor(Math.random() * 100));
            this.props.changeHandler();
        }, 5000);

    }


    render() {
        return (
            <div className="chart">
                <Line
                    data={this.state}
                    height={5}
                    width={20}
                />
            </div>
        )
    }
}

export default Chart;
export default LineChart;
Huppert answered 5/9, 2017 at 11:24 Comment(0)
I
18

So according to https://www.npmjs.com/package/react-chartjs-2

One can access the chart reference using a ref such as

chartReference = {};
 
componentDidMount() {
  console.log(this.chartReference); // returns a Chart.js instance reference
}
 
render() {
  return (<Doughnut ref={(reference) => this.chartReference = reference } data={data} />)
}

So what you could do is put a ref in your Chart and access it wherever you like.

<Line
     data={this.state}
     height={5}
     width={20}
     ref = {(reference) => this.reference = reference}
/>

In the method you wish to cause the update you could access this reference and its chartInstance and call the update function on this instance.

let lineChart = this.reference.chartInstance
lineChart.update();
Is answered 8/5, 2019 at 4:55 Comment(3)
Does this still apply today with react-chartjs-2? I'm attempting to get my linecharts to destroy when the api data changes and I'm attempting this method but getting chartReference is undefinedGerhan
I am not really sure if it applies till now. I would recommend either checking their documentation again else just trying to see the Javascript object and determining if this still applies.Is
@Gerhan It still works but in my code I had to change the last piece of code calling the function update() directly from the reference. So it's simply this.reference.update();Silicic
H
2

You need to update the chart, line is just a config setting on the chart, this update needs to flow back to the handler

To set you on the right path, here is an example of what I mean

var config = {};

class Chart extends Component {
    constructor() {
        super();
        this.ctx = document.getElementById(this._rootNodeID).getContext("2d");
        this.chart = new Chart(ctx, config);

    }


    changeHandler(value) {
        this.chart.update();
    }

    render() {
        return (
            <canvas id={this._rootNodeID}>
                <LineChart value={this.state.value} 
                           config={this.config} 
                           onChange={this.changeHandler}/>
            </canvas>
        );
    }
}


const line = {
    labels: [],
    datasets: [
        {
            label: 'My First dataset',
            fill: false,
            data: []
        }
    ]
};



class LineChart extends Component {

    constructor(props) {
        super(props);

        this.props.config = line;
        setInterval(function(){
            this.props.config.labels.push(Math.floor(Math.random() * 100));
            this.props.config.datasets[0].data.push(Math.floor(Math.random() * 100));
            this.props.changeHandler();
        }, 5000);

    }


    render() {
        return (
            <div className="chart">
                <Line
                    data={this.state}
                    height={5}
                    width={20}
                />
            </div>
        )
    }
}

export default Chart;
export default LineChart;
Huppert answered 5/9, 2017 at 11:24 Comment(0)
A
0

I recommend do not use setInterval, try to pas instance of chart to the dispatcher etc, and update it when request is done

   ChartJS.data = response.data;
   ChartJS.update();
Arboreous answered 4/3, 2019 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.