Recount-Server/web/static/chart_functions.js

39 lines
1.1 KiB
JavaScript

function fill_charts() {
document.querySelectorAll(".chartjs-chart").forEach(function (el) {
var url = el.dataset.chartEndpoint;
var type = el.dataset.chartType;
fetch(url)
.then(response => {
if (!response.ok) {
console.log(response);
throw new Error("Fetch response was not ok!")
}
return response.json();
}).then(jsonData => {
const config = {
type: type,
data: {
labels: jsonData.labels,
datasets: [{
data: jsonData.data
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
};
new Chart(el, config)();
}).catch(error => {
console.error("Unable to set up chart: ", error)
});
});
}
export {fill_charts}