Debian Codegraph @

frontend/src/charts/BarChart.svelte

raw
1 <script lang="ts">
2 import { extent } from "d3-array";
3 import { axisBottom, axisLeft } from "d3-axis";
4 import { scaleBand, scaleLinear, scaleOrdinal } from "d3-scale";
5
6 import { urlForAccount } from "../helpers";
7 import { barChartMode, chartToggledCurrencies } from "../stores/chart";
8 import { ctx, currentTimeFilterDateFormat, short } from "../stores/format";
9 import Axis from "./Axis.svelte";
10 import type { BarChart } from "./bar";
11 import {
12 currenciesScale,
13 filterTicks,
14 hclColorRange,
15 includeZero,
16 padExtent,
17 urlForTimeFilter,
18 } from "./helpers";
19 import { followingTooltip } from "./tooltip";
20
21 interface Props {
22 chart: BarChart;
23 width: number;
24 }
25
26 let { chart, width }: Props = $props();
27
28 const today = new Date();
29 const maxColumnWidth = 100;
30 const margin = { top: 10, right: 10, bottom: 30, left: 40 };
31 const height = 250;
32
33 let accounts = $derived(chart.accounts);
34
35 let filtered = $derived(chart.filter($chartToggledCurrencies));
36 let currencies = $derived(filtered.currencies);
37 let bar_groups = $derived(filtered.bar_groups);
38 let stacks = $derived(filtered.stacks);
39
40 let innerHeight = $derived(height - margin.top - margin.bottom);
41 let maxWidth = $derived(bar_groups.length * maxColumnWidth);
42 let offset = $derived(margin.left + Math.max(0, width - maxWidth) / 2);
43 let innerWidth = $derived(
44 Math.min(width - margin.left - margin.right, maxWidth),
45 );
46
47 /** Whether to display stacked bars. */
48 let showStackedBars = $derived(
49 $barChartMode === "stacked" && chart.hasStackedData,
50 );
51 /** The currently hovered account. */
52 let highlighted: string | null = $state(null);
53
54 // Scales
55 let x0 = $derived(
56 scaleBand([0, innerWidth])
57 .domain(bar_groups.map((d) => d.label))
58 .padding(0.1),
59 );
60 let x1 = $derived(scaleBand([0, x0.bandwidth()]).domain(currencies));
61
62 let yExtent = $derived(
63 showStackedBars
64 ? extent(stacks.flatMap(([, s]) => s.flat(2)))
65 : extent(bar_groups.map((d) => d.values).flat(), (d) => d.value),
66 );
67 let y = $derived(
68 scaleLinear([innerHeight, 0]).domain(padExtent(includeZero(yExtent))),
69 );
70
71 let colorScale = $derived(
72 scaleOrdinal(hclColorRange(accounts.length)).domain(accounts),
73 );
74
75 // Axes
76 let xAxis = $derived(
77 axisBottom(x0)
78 .tickSizeOuter(0)
79 .tickValues(filterTicks(x0.domain(), innerWidth / 70)),
80 );
81 let yAxis = $derived(
82 axisLeft(y).tickPadding(6).tickSize(-innerWidth).tickFormat($short),
83 );
84 </script>
85
86 <svg viewBox={`0 0 ${width.toString()} ${height.toString()}`}>
87 <g transform={`translate(${offset.toString()},${margin.top.toString()})`}>
88 <Axis x axis={xAxis} {innerHeight} />
89 <Axis y axis={yAxis} lineAtZero={y(0)} />
90 {#each bar_groups as group}
91 <g
92 class="group"
93 class:desaturate={group.date > today}
94 use:followingTooltip={() => chart.tooltipText($ctx, group)}
95 transform={`translate(${(x0(group.label) ?? 0).toString()},0)`}
96 >
97 <rect
98 class="group-box"
99 x={(x0.bandwidth() - x0.step()) / 2}
100 width={x0.step()}
101 height={innerHeight}
102 />
103 <a
104 href={urlForTimeFilter(group.date)}
105 aria-label={$currentTimeFilterDateFormat(group.date)}
106 >
107 <rect
108 class="axis-group-box"
109 transform={`translate(0,${innerHeight.toString()})`}
110 width={x0.bandwidth()}
111 height={margin.bottom}
112 />
113 </a>
114 {#if !showStackedBars}
115 {#each group.values as { currency, value, budget }}
116 <rect
117 fill={$currenciesScale(currency)}
118 width={x1.bandwidth()}
119 x={x1(currency)}
120 y={y(Math.max(0, value))}
121 height={Math.abs(y(value) - y(0))}
122 />
123 <rect
124 class="budget"
125 width={x1.bandwidth()}
126 x={x1(currency)}
127 y={y(Math.max(0, budget))}
128 height={Math.abs(y(budget) - y(0))}
129 />
130 {/each}
131 {/if}
132 </g>
133 {/each}
134 {#if showStackedBars}
135 {#each stacks as [currency, account_stacks]}
136 {#each account_stacks as stack}
137 {@const account = stack.key}
138 <a href={$urlForAccount(account)}>
139 <g
140 class="category"
141 class:faded={highlighted != null && account !== highlighted}
142 onmouseover={() => {
143 highlighted = account;
144 }}
145 onfocus={() => {
146 highlighted = account;
147 }}
148 onmouseout={() => {
149 highlighted = null;
150 }}
151 onblur={() => {
152 highlighted = null;
153 }}
154 role="img"
155 >
156 {#each stack as bar}
157 <rect
158 class:desaturate={bar.data.date > today}
159 width={x1.bandwidth()}
160 x={(x0(bar.data.label) ?? 0) + (x1(currency) ?? 0)}
161 y={y(Math.max(bar[0], bar[1]))}
162 height={Math.abs(y(bar[1]) - y(bar[0]))}
163 fill={colorScale(account)}
164 use:followingTooltip={() =>
165 chart.tooltipTextAccount(
166 $ctx,
167 bar.data,
168 account,
169 $chartToggledCurrencies,
170 )}
171 />
172 {/each}
173 </g>
174 </a>
175 {/each}
176 {/each}
177 {/if}
178 </g>
179 </svg>
180
181 <style>
182 .category.faded {
183 opacity: 0.5;
184 }
185
186 .axis-group-box {
187 cursor: pointer;
188 opacity: 0;
189 }
190
191 .group-box {
192 opacity: 0;
193 }
194
195 .group:hover .group-box {
196 opacity: 0.1;
197 }
198
199 .budget {
200 opacity: 0.3;
201 }
202
203 .desaturate {
204 filter: saturate(50%);
205 }
206 </style>