Skip to content

Commit 39a47a5

Browse files
author
Daniel Precioso
committed
Add zoom and pan functionality to time series charts; initialize Chart.js plugin and update canvas handling for improved user interaction
1 parent 14dbc7d commit 39a47a5

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

static/app.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ const METRIC_CFG = {
8484
}
8585
};
8686

87+
/* ======================= INIT PARAMS ====================== */
88+
Chart.register(window.ChartZoom); // make Chart.js aware of the plugin
89+
8790
/* ======================= MAP INIT ======================= */
8891
// take the first metric as default
8992
let currentMetric = Object.keys(METRIC_CFG)[0];
@@ -248,13 +251,21 @@ function drawTimeSeries(nutsId, regionName) {
248251
const labels = res.data.map(d => `${d.year}-W${String(d.week).padStart(2, '0')}`);
249252
const values = res.data.map(d => d.value);
250253

254+
/* auto-centre: ±10 years around the current slider year */
255+
const curYear = +yearSlider.value;
256+
const minYear = Math.max(curYear - 10, +labels[0].slice(0,4));
257+
const maxYear = Math.min(curYear + 10, +labels.at(-1).slice(0,4));
258+
const minLabel = labels.findIndex(s => +s.slice(0,4) >= minYear);
259+
const maxLabel = labels.findLastIndex(s => +s.slice(0,4) <= maxYear);
260+
251261
/* prepare a fresh canvas each click */
252262
const holder = document.getElementById('regionGraph');
253-
holder.innerHTML = ''; // remove any previous canvas
254-
const canvas = document.createElement('canvas');
255-
holder.appendChild(canvas);
263+
holder.innerHTML = '<canvas></canvas>';
264+
const ctx = holder.firstChild.getContext('2d');
265+
266+
if (currentChart) currentChart.destroy();
256267

257-
new Chart(canvas.getContext('2d'), {
268+
currentChart = new Chart(ctx, {
258269
type: 'line',
259270
data: {
260271
labels,
@@ -273,8 +284,26 @@ function drawTimeSeries(nutsId, regionName) {
273284
responsive: true,
274285
maintainAspectRatio: false,
275286
scales: {
276-
x: { ticks: { autoSkip: true, maxTicksLimit: 12 } },
277-
y: { beginAtZero: true }
287+
x: {
288+
ticks : { autoSkip: true, maxTicksLimit:12 },
289+
min : labels[minLabel],
290+
max : labels[maxLabel]
291+
},
292+
y: { beginAtZero:true }
293+
},
294+
/* ------------ Zoom / Pan only on X ------------- */
295+
plugins: {
296+
zoom: {
297+
limits: { x: {min: labels[0], max: labels.at(-1)} },
298+
zoom: {
299+
wheel : { enabled:true },
300+
mode : 'x'
301+
},
302+
pan: {
303+
enabled: true, // allow panning
304+
mode : 'x' // x-axis only
305+
}
306+
}
278307
}
279308
}
280309
});

templates/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
<!-- Chart.js -->
2020
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
21+
22+
<!-- Zoom/Pan plug-in -->
23+
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@2"></script>
2124
</head>
2225
<body>
2326

0 commit comments

Comments
 (0)