I'm new to programming so bear with me XD I've spent 2 days trying to work this out to no avail.
I have a chart in javascript that gets data from test.json that is saved in the same directory as the chart's file :
data2020 = '[0,0,0,0,0,202,50857,116823,113280,109400,99846,83975]';
data2021 = '[58578,15864]';
As I learned how to update the data, I realized it's not the standard json format as the updated file from this script :
file_put_contents('test.json', $newJsonString);
is
[{"data2020":[0,0,0,0,0,202,50857,116823,113280,109400,99846,83975],"data2021":[58578,15864]}]
and now my chart cant read that json file. I figured what's wrong must be in my chart since I only use this :
data: {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [{
label: "Produksi 2020 : ",
lineTension: 0.3,
backgroundColor: "rgba(78, 115, 223, 0.05)",
borderColor: "rgba(235, 180, 39, 1)",
pointRadius: 3,
pointBackgroundColor: "rgba(235, 180, 39, 1)",
pointBorderColor: "rgba(235, 180, 39, 1)",
pointHoverRadius: 3,
pointHoverBackgroundColor: "rgba(255, 184, 0, 1)",
pointHoverBorderColor: "rgba(255, 184, 0, 1)",
pointHitRadius: 10,
pointBorderWidth: 2,
data: JSON.parse(data2020),
},
then I add both script to my php with this (as someone suggested in the other post below) :
<script src="../js/test.json"></script>
<script src="../js/chart.js"></script>
Notice how the only thing I did was use JSON.parse and nothing else. I figured I had to find a way to put my json file to an array, and use that array in the data. But I cant figure out how to do that. So the question is really simple, how do I convert json file to an array in javascript?
I've read this link, but I still dont understand it XD : How to read an external local JSON file in JavaScript?