Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react chart js 2 api data

import React, { Component } from 'react';
import './App.css';
import axios from 'axios'
import Chart from './components/Chart';

class App extends Component {
  constructor(){
    super();
    this.state = {
      chartData:{}
    }
  }

  componentDidMount() {
    this.getChartData();
  }

  getChartData() {
    axios.get("http://www.json-generator.com/api/json/get/coXIyroYAy?indent=2").then(res => {
        const coin = res.data;
        let labels = [];
        let data = [];
        coin.forEach(element => {
        labels.push(element.labels);
        data.push(element.data);

          });

       console.log(coin)
        this.setState({
          chartData: {
            labels:labels,
            datasets: [
              {
                label: "Population",
                data: data,
                backgroundColor: [
                  "rgba(255, 99, 132, 0.6)",
                  "rgba(54, 162, 235, 0.6)",
                  "rgba(255, 99, 132, 0.6)"
                ],
              }
            ]
          }
        });
      });
    }

  render(){

        return (
          <div className="App">
          {Object.keys(this.state.chartData).length &&
            <Chart
              chartData={this.state.chartData}
              location="Massachusetts"
              legendPosition="bottom"
            />
          }
          </div>
        );

    }     
}

export default App;
Comment

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 )))*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: iteratea on values map js 
Javascript :: react forward ref 
Javascript :: how to sum the array values in javascript 
Javascript :: byte to integer js 
Javascript :: how to access data in json format using asp.net c# 
Javascript :: js ternary else if multi 
Javascript :: ckeditor inline editor example 
Javascript :: javascript array sorting 
Javascript :: javascript refresh element 
Javascript :: how to run react native app on simulator 
Javascript :: javascript print to console 
Javascript :: mdn object assign 
Javascript :: replace char at index of string 
Javascript :: reactjs lifecycle class components 
Javascript :: javascript array destructuring last element 
Javascript :: row smaller than the container bootstrap react 
Javascript :: how to find the lowest number in an array in javascript for specific indexes 
Javascript :: javascript inheritence 
Javascript :: js map 
Javascript :: curl post request 
Javascript :: javascript get object in object 
Javascript :: count occurence in array js 
Javascript :: Destructuring of object in ES6 
Javascript :: date picker type react 
Javascript :: Iterate Through the Keys of an Object 
Javascript :: react pass object as props 
Javascript :: what is getter and setter in javascript 
Javascript :: how to slice array in angular 6 
Javascript :: process nexttick 
Javascript :: render html in js.erb 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =