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