Files
nrml_dashboard/dashboard/public/js/dashboard/charts.js

108 lines
2.9 KiB
JavaScript

const charts = {};
function buildChart(id, type, labels, data, label = 'Cases') {
const ctx = document.getElementById(id);
if (!ctx) return;
if (charts[id]) charts[id].destroy();
charts[id] = new Chart(ctx, {
type: type,
data: {
labels: labels,
datasets: [{
label: label,
data: data,
borderWidth: 2,
tension: 0.3
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
}
function buildMixedTrendChart(canvasId, labels, samples, positivity) {
const ctx = document.getElementById(canvasId);
if (!ctx) return;
if (charts[canvasId]) charts[canvasId].destroy();
charts[canvasId] = new Chart(ctx, {
data: {
labels: labels,
datasets: [
{
type: 'line',
label: '% Positive',
data: positivity,
borderColor: '#1e6ef2',
borderWidth: 2,
tension: 0.4,
fill: false,
pointRadius: 4,
pointStyle: 'line',
yAxisID: 'y1'
},
{
type: 'bar',
label: 'Total sample',
data: samples,
backgroundColor: '#2ecc71',
borderRadius: 6,
barPercentage: 0.6,
pointStyle: 'rect',
categoryPercentage: 0.7,
yAxisID: 'y',
},
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom',
align: 'center',
labels: {
usePointStyle: true,
padding: 20,
boxWidth: 30,
font: {
size: 12
}
}
}
},
layout: {
padding: {
bottom: 50
}
},
scales: {
y: {
position: 'left',
title: {
display: true,
text: 'Total sample'
}
},
y1: {
position: 'right',
grid: {
drawOnChartArea: false
},
title: {
display: true,
text: '% Positive'
},
ticks: {
callback: value => value + '%'
}
}
}
}
});
}