chart js react code example
Example 1: react chart js 2
npm install --save react-chartjs-2 chart.js
Example 2: react chartjs 2
npm install --save react-chartjs-2 chart.js
Example 3: react js charts
import React, { Component } from "react";
import Chart from "react-apexcharts";
class App extends Component {
constructor(props) {
super(props);
this.state = {
options: {
chart: {
id: "basic-bar"
},
xaxis: {
categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
}
},
series: [
{
name: "series-1",
data: [30, 40, 45, 50, 49, 60, 70, 91]
}
]
};
}
render() {
return (
<div className="app">
<div className="row">
<div className="mixed-chart">
<Chart
options={this.state.options}
series={this.state.series}
type="bar"
width="500"
/>
</div>
</div>
</div>
);
}
}
export default App;
Example 4: 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;