Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

api call in react chart

import axios from "axios";
import React,{useEffect, useState} from "react";
import {Bar, BarChart, CartesianGrid, Legend, Tooltip, XAxis, YAxis} from 'recharts'

function Analytics() {

  const [data,setData] = useState();

  useEffect(() => {

  axios.get('https://iammaawesomee.herokuapp.com/data/get-data').then(function(response){
/*Change the api bcuz this is fake api and setup the response data by consoling output */
  
    console.log(response.data)
    /* Response.data in this case is an array of objects.. so keep in mind*/
  
    setData(response.data)
}).catch(function(error){
  console.log(error);
})


}, []);



  return (
      <>
      <div style={{marginTop:'50px',marginLeft:'125px',marginBottom:'60px'}}>
        <h1 style={{marginLeft:'-130px',marginBottom:'30px',color:'red'}}>Data Analytics</h1>
<BarChart width={1000} height={300} data={data}>
  <CartesianGrid strokeDasharray="1 1" />
  <XAxis dataKey="device_id" />
  <YAxis />
  <Tooltip />
  <Bar dataKey="distanceCm" fill="#8884d8" />
  <Bar dataKey="duration" fill="#82ca9d" />
  <Bar dataKey="Perc" fill="#82ea5d" />
  <Legend />

</BarChart></div>
      </>
    );
  }

export default Analytics;


/*(((then u need to import this file in app.js file )))*/
 
PREVIOUS NEXT
Tagged: #api #call #react #chart
ADD COMMENT
Topic
Name
6+2 =