Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
How to use react hook in Bubble chart using chart.js

I'm trying to add new bubble in the chart whenever we click on add button I am able to update the state of data i.e. Chartdata using usestate hook but the chart does not re-render and does not show the chart according to the new updated data.

const data = 
{
  datasets: [{
      label: 'Grapefruit',
      data: [{
        x: 10,
        y: 40,
        r: 40
      }
    ],
      backgroundColor: 'rgb(255, 99, 132)'
    },
  {
    label: 'Lime',
    
    data: [{
      x: 23,
      y: 37,
      r: 40
    }    
  ],
    backgroundColor: 'rgb(0, 152, 102)'
  }
 ]
};

const  App = () => {

 
  const [chartData, setchartData] = useState(data);

   
   function callingChart(inputText,value)
   {
        
        const newl = [{x: data.datasets[1].data[0].x + l,y:data.datasets[1].data[0].y+ l,r:data.datasets[1].data[0].r + l}]
        data.datasets.push({ label: inputText,
        data: newl,
        backgroundColor: 'rgb(255, 128, 0)'})
               
     setchartData(data);          
      
    }     
   return (
    
      <div className ="bubble">  
           <BarChart data={chartData} options={options}/>
      </div>
  
  );
}

export default App;

I don't know what I'm doing wrong .

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

From what I can tell you are mutating the data object, which is a reference to the same object you initialized your chartData state to, and then save the mutated data object back into the chartData state.

In React you necessarily need to shallow copy all state that is being update. Create a newChartData object from the current chartData state and shallow copy the root properties, then declare a new datasets array by shallow copying the previous state's datasets array. This is a new array reference that you can push new values into.

function callingChart(inputText, value) {
  const newChartData = {
    ...chartData, // shallow copy chartData object
    datasets: chartData.datasets.slice(), // shallow copy datasets array
  }

  if (value === "lime") {
    const newl = [
      {
        x: chartData.datasets[1].data[0].x + l,
        y: chartData.datasets[1].data[0].y + l,
        r: chartData.datasets[1].data[0].r + l
      }
    ];
    newChartData.datasets.push({
      label: inputText,
      data: newl,
      backgroundColor: "rgb(255, 128, 0)"
    });

    l--;
  } else if (value === "coconut") {
    const newc = [
      {
        x: chartData.datasets[2].data[0].x + c,
        y: chartData.datasets[2].data[0].y + c,
        r: chartData.datasets[2].data[0].r + c
      }
    ];

    newChartData.datasets.push({
      label: inputText,
      data: newc,
      backgroundColor: "rgb(255, 128, 0)"
    });

    c--;
  } else if (value === "grapefruit") {
    const newg = [
      {
        x: chartData.datasets[0].data[0].x + g,
        y: chartData.datasets[0].data[0].y + g,
        r: chartData.datasets[0].data[0].r + g
      }
    ];

    newChartData.datasets.push({
      label: inputText,
      data: newg,
      backgroundColor: "rgb(255, 128, 0)"
    });

    g--;
  } else {
    const newm = [
      {
        x: chartData.datasets[3].data[0].x + m,
        y: chartData.datasets[3].data[0].y + m,
        r: chartData.datasets[3].data[0].r + m
      }
    ];
    newChartData.datasets.push({
      label: inputText,
      data: newm,
      backgroundColor: "rgb(255, 128, 0)"
    });

    m--;
  }

  setchartData(newChartData);

  console.log(newChartData);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!