Alright, after experimenting with this approach, it seems to be effective.
The concept is straightforward - you gather the array of keys and values and supply them to labels and data.
Below is a simple example based on the provided data.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="./script.js"></script>
</body>
</html>
including the script file
Script.js
const toCharts = () => {
const sampleData = {
"2021-6-12": 2,
"2021-6-13": 3,
"2021-6-14": 1
}
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: "bar",
data: {
labels: Object.keys(sampleData),
datasets: [{
label: "Demo years",
data: Object.values(sampleData),
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
],
borderWidth: 0.5,
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
})
}
toCharts();