@edmond_brakus
To get JSON data and display it on a Chart.js chart, you can follow these general steps:
1 2 3 4 5 |
fetch('data.json')
.then(response => response.json())
.then(data => {
// Call a function to initialize and display the chart
});
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: data.labels,
datasets: [{
label: 'My Dataset',
data: data.values,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
|
1
|
<canvas id="myChart" width="400" height="400"></canvas> |
By following these steps, you should be able to fetch JSON data and display it on a Chart.js chart in your web application.