@@ -243,8 +243,67 @@ const currentUrl = (() => {
243243 <div class = " bg-white rounded-lg shadow-sm border border-gray-200 p-4 sm:p-8 mb-6 sm:mb-8" >
244244 <h2 class = " text-lg sm:text-xl font-semibold text-gray-900 mb-4" >Timeline sostenitori</h2 >
245245 <div id = " chart" class = " w-full overflow-x-auto" ></div >
246- <script type="module" define:vars={{ timelineData: initiativeTimeLine }}> import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6/+esm"; const fmtIt = new Intl.NumberFormat("it-IT").format; const fmtDateTick = d => d.toLocaleDateString("it-IT", { day: "2-digit", month: "short", year: "2-digit" }); const fmtDateTooltip = d => d.toLocaleDateString("it-IT", { day: "numeric", month: "long", year: "numeric" }); function draw() { const data = timelineData .map(d => ({ ...d, data: new Date(d.data) })) .sort((a, b) => a.data - b.data); if (data.length === 0) return; const container = document.getElementById("chart"); if (!container) return; const { width } = container.getBoundingClientRect(); const chart = Plot.plot({ width, marginLeft: 60, marginBottom: 40, x: { type: "utc", label: "Data", tickFormat: fmtDateTick }, y: { label: "Sostenitori", tickFormat: fmtIt, ticks: 5, zero: true }, marks: [ Plot.line(data, { x: "data", y: "sostenitori", stroke: "#1f77b4" }), Plot.dot(data, { x: "data", y: "sostenitori", r: 4, fill: "#1f77b4", title: d => `sostenitori: ${fmtIt(d.sostenitori)}
247- ${ fmtDateTooltip (d .data )} ` }) ] }); container.innerHTML = ""; container.append(chart); } const chartContainer = document.getElementById("chart"); if (chartContainer) { draw (); new ResizeObserver (draw ).observe (chartContainer ); } </script >
246+ <script type = " module" define :vars = { { timelineData: initiativeTimeLine }} > import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6/+esm"; const fmtIt = new Intl.NumberFormat("it-IT").format; const fmtDateTooltip = d => d.toLocaleDateString("it-IT", {
247+ day : " numeric" ,
248+ month : " long" ,
249+ year : " numeric"
250+ } ); let lastTick = null;
251+ // Definiamo la funzione di formattazione separatamente per evitare errori di parsing
252+ const customTickFormat = (d) => {
253+ if (! (d instanceof Date ) || isNaN (d )) {
254+ return " " ;
255+ }
256+ const currentTick = d.toLocaleDateString(" it-IT" , { day : " 2-digit" , month : " short" , year : " 2-digit" });
257+ if (currentTick === lastTick ) {
258+ return " " ;
259+ }
260+ lastTick = currentTick ;
261+ return currentTick;
262+ };
263+ function draw() {
264+ const data = timelineData .map (d => ({ ... d , data: new Date (d .data ) }))
265+ .sort ((a , b ) => a .data - b .data );
266+ if (data .length === 0 ) return ;
267+ const container = document .getElementById (" chart" );
268+ if (! container ) return ;
269+ const { width } = container .getBoundingClientRect ();
270+ lastTick = null ; // Reset for each redraw
271+ const chart = Plot .plot ({
272+ width ,
273+ marginLeft: 60 ,
274+ marginBottom: 40 ,
275+ x: {
276+ type: " utc" ,
277+ label: " Data" ,
278+ tickFormat: customTickFormat , // Usiamo la funzione nominata
279+ insetLeft: 20 // Aggiunge padding a sinistra dell'asse x
280+ },
281+ y: {
282+ label: " Sostenitori" ,
283+ tickFormat: fmtIt ,
284+ ticks: 5 ,
285+ zero: true
286+ },
287+ marks: [
288+ Plot .line (data , { x: " data" , y: " sostenitori" , stroke: " #1f77b4" }),
289+ Plot .dot (data , {
290+ x: " data" ,
291+ y: " sostenitori" ,
292+ r: 4 ,
293+ fill: " #1f77b4" ,
294+ title : d => ` sostenitori: ${fmtIt (d .sostenitori )}
295+ ${fmtDateTooltip (d .data )} `
296+ })
297+ ]
298+ });
299+ container .innerHTML = " " ;
300+ container .append (chart );
301+ }
302+ const chartContainer = document .getElementById (" chart" );
303+ if (chartContainer ) {
304+ draw();
305+ new ResizeObserver(draw ).observe(chartContainer );
306+ } < / script >
248307 < / div >
249308 )}
250309
0 commit comments