I'm trying to load JSON data externally and pass it on to datasets and labels.
When I start the server I see the data being called in developer options, however, the same does not reflect in the bar graph.
I tried using fetch function to call my API but I don't know how to call it into the labels (which represent X-axis in chart.js) and to datasets (which represent the Y-axis in chart.js).
Moreover, I am unaware how to call an array data. I am calling the chartData into another chart file in which I have created a chart with all properties of bar and trying to call this chartData into my bar chart data using props.
Please correct me where I've been doing it wrong. Below is my code.
getChartData(){
//Ajax calls here
return fetch('https://api.myjson.com/bins/l5pw3')
.then((response)=> response.json())
.then((responseJson) => {
this.setState({
//chartData:responseJson.keywordData
chartData: {
labels: [responseJson.keywordData.category],
datasets:[
{
label: 'spectra',
data [responseJson.keywordData.noOfSpectra],
backgroundColor:[
'rgba(255, 99, 132, 0.6)'
]
}
]
}
});
console.log(this.state.chartData)
});
}
render() {
return (
<div className="App">
<Chart chartData={this.state.chartData} />
</div>
);
}
export default App;