Skip to content

Commit

Permalink
Add tooltip info on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiak committed May 30, 2024
1 parent 2934fbb commit 1ff7a33
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/lib/GrantChart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -49,15 +55,28 @@

<Pancake.Svg>
<Pancake.SvgLine
data={granted.map((d) => ({
x: parseDate(d.month),
y: d.percent * 100,
}))}
data={chartData}
let:d
>
<path class="data" {d} />
</Pancake.SvgLine>
</Pancake.Svg>

{#if closest}
{@const value = closest.y}
{@const month = (new Date(closest.x)).getMonth() + 1}
{@const year = (new Date(closest.x).getFullYear())}
<div class="tooltip">
<div>
Month: {`${month}`.padStart(2, '0')}-{year}
</div>
<div>
Grant rate: {Math.round(value)}%
</div>
</div>
{/if}

<Pancake.Quadtree data={chartData} bind:closest/>
</Pancake.Chart>
</div>
</div>
Expand Down Expand Up @@ -101,6 +120,7 @@
}
path.data {
cursor: pointer;
stroke: gold;
stroke-linejoin: round;
stroke-linecap: round;
Expand All @@ -112,4 +132,10 @@
text-align: center;
font-weight: 700;
}
.tooltip {
font-size: .8em;
padding-top: .5rem;
text-align: center;
}
</style>

0 comments on commit 1ff7a33

Please sign in to comment.