react-google-charts icon indicating copy to clipboard operation
react-google-charts copied to clipboard

Charts do not re-render on props change

Open Brutalbeard opened this issue 8 years ago • 6 comments

Was making my own demo, with a function for faking data to see the charts re-render.

"Line" and "AreaChart" will update on a props change, but I've tried with ColumnChart, PieChart, and GeoChart and they all stay static.

Brutalbeard avatar Aug 17 '18 20:08 Brutalbeard

Hey @Brutalbeard,

Can you share a small example that I can check ?

rakannimer avatar Aug 18 '18 05:08 rakannimer

import React, {Component} from 'react';
import { Divider, Container, Button } from 'semantic-ui-react'

import { Chart } from "react-google-charts";

class GoogleChartsTest extends Component {

  constructor(props) {
    super(props);
    this.state={
        columnData: [
            ["Age", "Weight"], 
            [4, 5.5], 
            [8, 12]
        ],
        pieData: [
            ["Fruit", "Total"],
            ["Blueberry", 44], 
            ["Strawberry", 23], 
            ["Orange", 16]
        ],
        lineData: [
            [new Date("2017-05-13T21:33:56.303Z"), 2, 5], 
            [new Date("2017-05-14T21:33:56.303Z"), 5, 2], 
            [new Date("2017-05-15T21:33:56.303Z"), 7, 12], 
            [new Date("2017-05-16T21:33:56.303Z"), 4, 12],
            [new Date("2017-05-18T21:33:56.303Z"), 5, 11],
            [new Date("2017-05-19T21:33:56.303Z"), 3, 14],
            [new Date("2017-05-22T21:33:56.303Z"), 7, 21],
            [new Date("2017-05-25T21:33:56.303Z"), 8, 19]
        ],
        geoData: [
            ['State',   'Coolness'],
            ['FL',  276147],
            ['GA',  132411],
            ['NC',  959574],
            ['SC',  907563],
            ['CO',  655875],
            ['WI',  607906],
            ['CA',  380181],
            ['NY',  371282],
            ['TX',  0]
        ],
        areaData: [
            [new Date('2013'),  1000,   400],
            [new Date('2014'),  1170,   460],
            [new Date('2015'),  660,    1120],
            [new Date('2016'),  1030,   540],
            [new Date('2017'),  1058,   670],
            [new Date('2018'),  918,    980]
        ]
    }
  }

  fakeData(){
    console.log("Faking data...")
    let pieData = this.state.pieData
    let geoData = this.state.geoData
    let areaData = this.state.areaData
    let lineData = this.state.lineData
    let columnData = this.state.columnData

    for(let i = 1; i < pieData.length; i++){
      pieData[i][1] = Math.floor((Math.random() * 100) + 1)
    }

    for(let i = 1; i < geoData.length; i++){
      geoData[i][1] = Math.floor((Math.random() * 1000000) + 1)
    }

    for(let i = 0; i < areaData.length; i++){
      areaData[i][1] = Math.floor((Math.random() * 1000) + 1)
      areaData[i][2] = Math.floor((Math.random() * 1000) + 1)
    }

    for(let i = 1; i < columnData.length; i++){
      columnData[i][1] = Math.floor((Math.random() * 20) + 1)
      columnData[i][2] = Math.floor((Math.random() * 20) + 1)
    }

    for(let i = 0; i < lineData.length; i++){
      lineData[i][1] = Math.floor((Math.random() * 10) + 1)
      lineData[i][2] = Math.floor((Math.random() * 20) + 1)
    }

    this.setState({
      pieData,
      lineData,
      geoData,
      areaData,
      columnData
    })

  }

  render() {
    return (
        <Container>
            <Button onClick={() => this.fakeData()} content="Create Fake Data"/>
            <Divider />
            <Chart
                chartType="Line"
                rows={this.state.lineData}
                columns={[
                    "Date",
                    "Coolness",
                    "Uber-Coolness"
                ]}
                width="100%"
                height="400px"
                legendToggle
                options={{
                    curveType: "function",
                    hAxis:{
                        title: "Date"
                    }
                }}
            />
            <Divider />
            <Chart
                chartType="AreaChart"
                rows={this.state.areaData}
                columns={[
                    'Year', 
                    'Sales', 
                    'Expenses'
                ]}
                options={{
                    title: 'Company Performance',
                    hAxis: {
                        title: 'Year',  
                        titleTextStyle: {
                            color: '#333'
                        }
                    }
                }}
                width="100%"
                height="400px"
                legendToggle
            />
            <Divider />
            <Chart
                chartType="ColumnChart"
                data={this.state.columnData}
                width="100%"
                height="400px"
                legendToggle
            />
            <Divider />
            <Chart
                chartType="PieChart"
                data={this.state.pieData}
                width="100%"
                height="400px"
                legendToggle
                options={{
                    pieHole: 0.5,
                    pieSliceText: "value",
                    title:"Dope Pie Chart"
                }}
            />
            <Divider />
            <Chart
                chartType="GeoChart"
                data={this.state.geoData}
                options={{
                    region: "US",
                    displayMode: "auto",
                    resolution: "provinces"
                }}
                width="100%"
                height="400px"
                legendToggle
            />
            <Divider />
            
        </Container>
    )
  }
}

export default GoogleChartsTest;

Brutalbeard avatar Aug 18 '18 14:08 Brutalbeard

Perhaps after a night of sleep, things have gotten better? ColumnChart seems to be the only one still having issues. The other charts are re-loading as expected.

Brutalbeard avatar Aug 18 '18 14:08 Brutalbeard

I tested your code on CodeSandbox and it works as expected, Sandbox link.

Is there something I'm missing ?

rakannimer avatar Aug 19 '18 08:08 rakannimer

Could you try in a browser? I'm still seeing the same issue this morning.

https://youtu.be/ezPYJht41wk

Brutalbeard avatar Aug 19 '18 14:08 Brutalbeard

Fresh cup of coffee and sleep again! Simple mistake on my end. For the column chart, when faking data, I was accidentally adding another element to the data array.

Closing, and thanks for letting me bug you :-)

Brutalbeard avatar Aug 20 '18 13:08 Brutalbeard