From 1ff7a33e45d5585f4975d628f6f37b00f40cc153 Mon Sep 17 00:00:00 2001 From: Jeremia Kimelman Date: Thu, 30 May 2024 23:52:53 +0100 Subject: [PATCH] Add tooltip info on hover --- src/lib/GrantChart.svelte | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/lib/GrantChart.svelte b/src/lib/GrantChart.svelte index f68740a..4256e7a 100644 --- a/src/lib/GrantChart.svelte +++ b/src/lib/GrantChart.svelte @@ -2,6 +2,8 @@ import dayjs from "dayjs"; import * as Pancake from "@sveltejs/pancake"; export let data = []; + + let closest let title = "Parole is granted in fewer than 1 in 5 hearings"; function formatDate(date) { @@ -23,6 +25,10 @@ $: lastGranted = granted[granted.length - 1]; $: lastGrantedPercent = lastGranted?.percent; + $: chartData = granted.map((d) => ({ + x: parseDate(d.month), + y: d.percent * 100, + })) $: { if (lastGrantedPercent > 0.2) { @@ -49,15 +55,28 @@ ({ - x: parseDate(d.month), - y: d.percent * 100, - }))} + data={chartData} let:d > + + {#if closest} + {@const value = closest.y} + {@const month = (new Date(closest.x)).getMonth() + 1} + {@const year = (new Date(closest.x).getFullYear())} +
+
+ Month: {`${month}`.padStart(2, '0')}-{year} +
+
+ Grant rate: {Math.round(value)}% +
+
+ {/if} + + @@ -101,6 +120,7 @@ } path.data { + cursor: pointer; stroke: gold; stroke-linejoin: round; stroke-linecap: round; @@ -112,4 +132,10 @@ text-align: center; font-weight: 700; } + + .tooltip { + font-size: .8em; + padding-top: .5rem; + text-align: center; + }