Chartjs-Unable to pass on data from json file to labels and datasets in chart.js

1👍

It was a little bit hard to understand what you meant, but as I understood, you wanted:

y – datasets
x – categories

and all datasets show the spectra property.

If I understood it correctly, the problem was that you didn’t map the properties to the corresponding fields: for example: labels: [responseJson.keywordData.category], should be more likelabels: responseJson.keywordData.map(k => k.category), because you cannot explicitly state that all the fields are mapped and looped automatically.

The same was for the spectra property, you have to then map each object from the response and extract the spectra property ( you can now take only one value, cause there is only one spectra, but in the future, if there are more, you will have to map)

I am adding a codesandboxexample
(a little notes in forward:
for fetch I used axios (my own preferance, there is no difference)
(you were using react, so I imported react-chartjs-2)

Code Sanbox Link: https://codesandbox.io/s/qk0r930p89

Hope it helps!

Leave a comment