0

Here I want to fetch data from a REST API URL which will be fed into the pie chart rather than manually entering data as given in the piechart below.

The API URL will have a JSON response.

ex:
[{"Development stage:1,"no.of.project:60},{"Development stage:2,"no.of.project:50}]

I want to display the data on the basis of JSON .

                     <Chart
                                  width={'450px'}
                                  height={'310px'}
                                  chartType="PieChart"
                                  loader={<div>Loading Chart</div>}
                                  data={[
                                    ['Development Stages', 'Number of Project'],
                                    ['Stage 1', 60 ],
                                    ['Stage 2',40],
                                    ['Stage 3',20 ],
                                    ['Stage 4',10],
                                    ['Stage 5', 30],
                                  ]}
                                  options={{
                                    title: 'Projects',
                                  }}
                                  rootProps={{ 'data-testid': '1' }}
                    />

How can I use async axios for the same???? and pass it into Chart.

                const [stage,setStage]=useState([]);
                const getStageData = async() => {
              try{

            const data=await axios.get(APIurl
           );

        setStage(data.data)
        }
        catch(e){


        }
    };

    useEffect(()=> {
        getStageData();
    },[]);

enter image description here

enter image description here

14
  • 1
    Are you using chart.js library? Commented Mar 30, 2021 at 10:37
  • 1
    please console log "stage" and let me know what do you, see? share that output here. Commented Mar 31, 2021 at 9:00
  • 1
    are you sure it is empty? if yes, then make sure the stage has the JSON response that you have mentioned above. what do u see when you console log "data.data"? is it empty? if it is not empty then do setStage(...stage , data.data) and then in your chart component do this data = {[stage]}. Not sure if this helps you but give it a try. Commented Mar 31, 2021 at 9:56
  • 1
    setStage(.data.data), no need for the spread operator here. Commented Mar 31, 2021 at 10:08
  • 1
    Please correct me if I am wrong. From your code, I can see that you have set the axios response first (setStage(data1.data)), and then you are converting to arrays using stage. map(Object. values). Try to map first and then set state and see. something like this const res = data1.data.map(obejct.values) and then setStage(res). Commented Mar 31, 2021 at 10:52

4 Answers 4

1

So, the data which you should pass to the pie chart should be array of arrays(depending on the library you are using). Your response data looks like an array of objects.

  1. Convert your data to array of arrays.
    setStage(reponse.data.map(Object.values));
  1. Pass that data to the pie chart.
    <Chart
       ...otherProps
       data={[
         ['Development Stages', 'Number of Project'], 
         ...data
       ]}
    />

Sign up to request clarification or add additional context in comments.

1 Comment

Here the stage is getting converted into array of array but using spread operator it is not display in the chart
1

From your code, I can see that you have set the axios response first (setStage(data1.data)), and then you are converting to arrays using stage.map(Object. values). Try to map first and then set state and see. something like this const res = data1.data.map(obejct.values) and then setStage(res).

Comments

0

<Chart width={'450px'} height={'310px'} chartType="PieChart" loader={Loading Chart} data={[['Development Stages', 'Number of Project'],...stage]}options={{title: 'Projects',}}rootProps={{ 'data-testid': '1' }} />

1 Comment

After this also the piechart is coming blank
0

The Complete working code to use react-google Piechart along with rest api data call

         const [stage,setStage]=useState([]);
         const getStageData = async() => 
        {
             try{

                 const data1=await axios.get("REST_API");

                //converting array of objects into array
                 const res=data1.data.map(Object.values);

                //setting it to stage variable
                setStage(res);  
        }
               catch(e){}  

         }; 
          useEffect(()=> {
                         getStageData();
                            },[]);

    <Chart
         width={'500px'}
         height={'310px'}
         chartType="PieChart"
        loader={<div>Loading Chart</div>}
        data={[
               ["COLUMN_NAME_1","COLUMN_NAME_2"],
               ...stage
            ]}
       options={{ 
                 title: 'Ongoing Projects Development Stage',                                                                               
               }}
        rootProps={{ 'data-testid': '1'}} 
     />
            
                                   
                            

                                        

                                                       

                                            
                            
                                          
                                          



                          

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.