Debian Codegraph @

frontend/src/charts/LineChart.svelte

raw
1 <script lang="ts">
2 import { extent, max, min } from "d3-array";
3 import { axisBottom, axisLeft } from "d3-axis";
4 import { quadtree } from "d3-quadtree";
5 import { scaleLinear, scaleUtc } from "d3-scale";
6 import { area, curveStepAfter, line } from "d3-shape";
7
8 import { chartToggledCurrencies, lineChartMode } from "../stores/chart";
9 import { ctx, short } from "../stores/format";
10 import Axis from "./Axis.svelte";
11 import { currenciesScale, includeZero, padExtent } from "./helpers";
12 import type { LineChart, LineChartDatum } from "./line";
13 import type { TooltipFindNode } from "./tooltip";
14 import { positionedTooltip } from "./tooltip";
15
16 interface Props {
17 chart: LineChart;
18 width: number;
19 }
20
21 let { chart, width }: Props = $props();
22
23 const today = new Date();
24 const margin = { top: 10, right: 10, bottom: 30, left: 40 };
25 const height = 250;
26 let innerWidth = $derived(width - margin.left - margin.right);
27 let innerHeight = $derived(height - margin.top - margin.bottom);
28
29 let data = $derived(chart.filter($chartToggledCurrencies));
30
31 // Scales and quadtree
32 let allValues = $derived(data.map((d) => d.values).flat(1));
33
34 let xExtent = $derived([
35 min(data, (s) => s.values[0]?.date) ?? today,
36 max(data, (s) => s.values[s.values.length - 1]?.date) ?? today,
37 ] as const);
38 let x = $derived(scaleUtc([0, innerWidth]).domain(xExtent));
39 let valueExtent = $derived(extent(allValues, (v) => v.value));
40 // Include zero in area charts so the entire area is shown, not a cropped part of it
41 let yExtent = $derived(
42 $lineChartMode === "area" ? includeZero(valueExtent) : valueExtent,
43 );
44 // Span y-axis as max minus min value plus 5 percent margin
45 let y = $derived(scaleLinear([innerHeight, 0]).domain(padExtent(yExtent)));
46
47 // Quadtree for hover.
48 let quad = $derived(
49 quadtree(
50 allValues,
51 (d) => x(d.date),
52 (d) => y(d.value),
53 ),
54 );
55
56 let lineShape = $derived(
57 line<LineChartDatum>()
58 .x((d) => x(d.date))
59 .y((d) => y(d.value))
60 .curve(curveStepAfter),
61 );
62
63 let areaShape = $derived(
64 area<LineChartDatum>()
65 .x((d) => x(d.date))
66 .y1((d) => y(d.value))
67 .y0(Math.min(innerHeight, y(0)))
68 .curve(curveStepAfter),
69 );
70
71 // Axes
72 let xAxis = $derived(axisBottom(x).tickSizeOuter(0));
73 let yAxis = $derived(
74 axisLeft(y).tickPadding(6).tickSize(-innerWidth).tickFormat($short),
75 );
76
77 const tooltipFindNode: TooltipFindNode = (xPos, yPos) => {
78 const d = quad.find(xPos, yPos);
79 return d && [x(d.date), y(d.value), chart.tooltipText($ctx, d)];
80 };
81
82 let futureFilter = $derived(
83 xExtent[1] > today ? "url(#desaturateFuture)" : undefined,
84 );
85 </script>
86
87 <svg viewBox={`0 0 ${width.toString()} ${height.toString()}`}>
88 <filter id="desaturateFuture">
89 <feColorMatrix type="saturate" values="0.5" x={x(today)} />
90 <feBlend in2="SourceGraphic" />
91 </filter>
92 <g
93 use:positionedTooltip={tooltipFindNode}
94 transform={`translate(${margin.left.toString()},${margin.top.toString()})`}
95 >
96 <Axis x axis={xAxis} {innerHeight} />
97 <Axis y axis={yAxis} />
98 {#if $lineChartMode === "area"}
99 <g class="area" filter={futureFilter}>
100 {#each data as d}
101 <path d={areaShape(d.values)} fill={$currenciesScale(d.name)} />
102 {/each}
103 </g>
104 {/if}
105 <g class="lines" filter={futureFilter}>
106 {#each data as d}
107 <path d={lineShape(d.values)} stroke={$currenciesScale(d.name)} />
108 {/each}
109 </g>
110 {#if $lineChartMode === "line"}
111 <g>
112 {#each data as d}
113 <g fill={$currenciesScale(d.name)}>
114 {#each d.values as v}
115 <circle
116 r="2"
117 cx={x(v.date)}
118 cy={y(v.value)}
119 class:desaturate={v.date > today}
120 />
121 {/each}
122 </g>
123 {/each}
124 </g>
125 {/if}
126 </g>
127 </svg>
128
129 <style>
130 svg > g {
131 pointer-events: all;
132 }
133
134 .lines path {
135 fill: none;
136 stroke-width: 2px;
137 }
138
139 .area path {
140 opacity: 0.3;
141 }
142
143 .desaturate {
144 filter: saturate(50%);
145 }
146 </style>