Files
HowBadIsMyBatch/docs/BatchcodeByCountryBarChart.js
2023-08-27 17:39:03 +02:00

80 lines
2.1 KiB
JavaScript

class BatchcodeByCountryBarChart {
#canvas;
#chart;
constructor(canvas) {
this.#canvas = canvas;
}
displayChart(barChartDescription) {
if (this.#chart != null) {
this.#chart.destroy();
}
this.#chart = new Chart(
this.#canvas,
{
type: 'bar',
data: this.#getData(barChartDescription),
options: this.#getOptions()
});
}
setData(barChartDescription) {
const data = this.#getData(barChartDescription);
this.#chart.config.data = data;
this.#chart.update();
}
#getData(barChartDescription) {
return {
labels: barChartDescription.countries,
datasets: [
{
label: `Known (${this.#dateRange2str(barChartDescription['date range known'])})`,
data: barChartDescription["Adverse Reaction Reports known"]
},
{
label: `Guessed (${this.#dateRange2str(barChartDescription['date range guessed'])})`,
data: barChartDescription["Adverse Reaction Reports guessed"]
}
]
};
}
#dateRange2str(dateRange) {
return `${dateRange.start} - ${dateRange.end}`;
}
#getOptions() {
return {
plugins: {
datalabels: {
anchor: 'end',
align: 'top'
}
},
title: {
display: true,
position: 'top'
},
scales: {
y: {
ticks: {
precision: 0
},
title: {
display: true,
text: 'Adverse Reaction Reports'
}
},
x: {
title: {
display: true,
text: 'Country'
}
}
}
};
}
}