score:0
You need to make two adjustments in your code.
1. Load data in componentDidMount
Change this line
componentWillMount = () => this.getChartData();
to
componentDidMount() {
this.getChartData();
}
You need to load the data once the component is mounted on the DOM.
2. Render only when you have chart data
Conditionally render Chart
component.
render() {
return (
<div className="App">
{Object.keys(this.state.chartData).length &&
<Chart
chartData={this.state.chartData}
location="Massachusetts"
legendPosition="bottom"
/>
}
</div>
);
}
You should render your Chart
only after chartData
is populated from API response.
score:0
it's work
my mistakes in json enter code here
getChartData() {
axios.get("http://www.json-generator.com/api/json/get/cghIWRNGEO?indent=2").then(res => {
const coin = res.data;
let labels = coin.chartData.labels;
let data = coin.chartData.datasets.data;
// console.log(data.Object.values(data)[0])
// data.forEach(element => {
// data.push(element.data);
// });
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)"
],
}
]
}
});
});
}
Source: stackoverflow.com
Related Query
- Create Chart using Reactjs Chartjs and axios
- create Chart using Chartjs and PHP
- Create Chart using $getJSON and ChartJS
- How to create a gantt chart using Chart.js and populate dates?
- Meteor and ChartJS dynamically create a chart
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- How to update a chart using VueJS and ChartJS
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- ReactJS - Loading data using Axios + chartjs
- Using ChartJS to create a multiple grouped bar chart - see picture below
- How to remove old chart and append new chart to div using chartjs
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- Can't draw pie chart using chartjs - TypeError: Cannot create property '_meta' on number '16'
- Angular and ChartJS to create bar chart
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- using react js to create a website that could read .csv file and show in chartjs
- Chartjs create chart with big data and fixed labels
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- My chartJS axios.get is fired twice causing chart to render twice too and i don't know why. I'm using react-chartjs-2
- Create a chartjs pie chart using json data
- Creating a real time line chart using chartjs and firebase realtime db
- How to add vertical line in bar chart using chartJs and ng2-charts?
- I wanna make this chart using chartjs In reactjs
- how to create bar chart with group and sam color for each group using chart.js?
- create a multi line chart using Chart.js
- Display line chart with connected dots using chartJS
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Dynamically update the options of a chart in chartjs using Javascript
- chartjs datalabels change font and color of text displaying inside pie chart
More Query from same tag
- React and chart.js with json
- Chart.js multiTooltip Labels in Pie
- How call a function when my array is filled
- How to create Bar chart that gets updated from new ajax requests with ChartJS?
- Chartjs Polar Area Chart - Data labels shown don't rotate with startAngle
- Connecting Two Datasets in Chart.js Into a Single Line
- How to style specific gridlines differently in chartJs?
- What are these line-boxes called in Chart.js, and how do I turn them off?
- Angular 5 chart.js onclick returning empty array
- Change mouse cursor to pointer in charts js
- How to get the data attribute of the canvas chart created using chartjs
- how to access class variables inside chart.js custom tooltip interface
- how to change display and styles of the labels in react-chartjs-2
- Chart.js - Hover labels to display data for all data points on x-axis
- Pass data from array to function vue
- Chart.js have the y-axis step from 20 to 1 (instead of 1 to 20)
- Chart.js: Get point index from chart.getPointsAtEvent(e)
- How to set axes' step size in Chart.js 2?
- Pass function elements to render in js
- Performance issue, what causes it?
- How to Import Angular Chart in IONIC
- ChatJS 2.8 chaning x-Axis font style and color
- Chart.js Multiple charts with one common legend
- pan on chart.js also zoom on line charts
- Revert tooltip order
- Javascript object from string
- Change X and Y axis font color
- Unable to install chartjs-node with npm, due to file cairo.h missing/Failed at the canvas@1.6.13 install script
- How to change the color of y axis labels in different color in Chart.js
- react-chart2: Cannot read properties of undefined (reading 'map')