@@ -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
8992let 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 } ) ;
0 commit comments