I have a unique JSON file structure that I am trying to display in ChartJS. The data looks like this:
{
"AAPL": [
{
"Q1": -26986,
"Q2": -168099,
"Q3": -137101,
"Q4": -561990
}
]
}
In my code, I am using the keys as labels and the values as data:
const xmlhttp4 = new XMLHttpRequest();
const url4 = 'https://api.npoint.io/ee3b3d406810c46c44e0';
xmlhttp4.open('GET', url4, true);
xmlhttp4.send();
xmlhttp4.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const datapoints = JSON.parse(this.responseText);
const data1 = datapoints[0];
const barchart = document.getElementById('insider_positions_barchart').getContext('2d');
const myBarChartAUD = new Chart(barchart, {
type: 'bar',
data: {
labels: Object.keys(data1),
datasets: [{
label: 'Net Activity',
data: Object.values(data1),
backgroundColor: ['rgba(0, 255, 255, 0.2)'],
borderColor: ['rgba(0, 255, 255, 1)'],
borderWidth: 3
}]
},
options: {
plugins: {
legend: {
display: false
}
},
maintainAspectRatio: false,
scales: {
y: {
ticks: {
color: "white"
},
grid: {
display: false
}
},
x: {
ticks: {
color: "white"
},
grid: {
display: false
}
}
}
}
});
}
}
I'm facing some issues with getting the data to display correctly in the chart, possibly due to how I am handling the keys and values. Should I consider changing the JSON structure or is there something else I should be looking at?