0

I'm making a pic chart using Chart.js with React.js(react-chartjs-2 ). My data set is stored in global variable and I want to use them to make a pie chart.

Currently when I use the data in global variable, the data is not reflected in the pie chart. What could I do?

Here is my code.

// global variable 
 let piData = 0;
 let piData2 = 0;
 let piData3 = 0;

 let newpiData = parseFloat( piData / 100 * 1000);
 let newpiData2 = parseFloat( piData2 / 100  * 1000);
 let newpiData3 = parseFloat( piData3 /100 * 1000);

  const data = {
  labels: [
    'question1',
    'question2',
    'question3',
    'question4',
    'question5'
   ],
  datasets: [{
    data: ['newpiData', 'newpiData2', 'newpiData3'],
    backgroundColor: [
    'orange',
    'blue',
    'red',
    'purple',
    'green'
    ],
    hoverBackgroundColor: [
    'orange',
    'blue',
    'red',
    'purple',
    'green'
    ]
   }]
  };

class PieChart extends React.Component {

render() {
return (
  <div>
    <h2>Pie Example</h2>
    <Pie data={data} />
  </div>
 );
}
}
aaayumi
  • 1,224
  • 8
  • 27
  • 47
  • Possible duplicate of [How to update Chart with data from global variable in React.js?](https://stackoverflow.com/questions/46367090/how-to-update-chart-with-data-from-global-variable-in-react-js) – Hemerson Carlin Sep 25 '17 at 09:39
  • How is your file structure set up? Where is your globals set, in a different file than where PieChart is defined? Are you including it? – pk1m Sep 25 '17 at 21:40

0 Answers0