| 1 |
<script lang="ts"> |
| 2 |
import type { HierarchyRectangularNode } from "d3-hierarchy"; |
| 3 |
import { partition } from "d3-hierarchy"; |
| 4 |
import { scaleLinear, scaleSqrt } from "d3-scale"; |
| 5 |
import { arc } from "d3-shape"; |
| 6 |
import { untrack } from "svelte"; |
| 7 |
|
| 8 |
import { formatPercentage } from "../format"; |
| 9 |
import { urlForAccount } from "../helpers"; |
| 10 |
import { ctx } from "../stores/format"; |
| 11 |
import { sunburstScale } from "./helpers"; |
| 12 |
import type { |
| 13 |
AccountHierarchyDatum, |
| 14 |
AccountHierarchyNode, |
| 15 |
} from "./hierarchy"; |
| 16 |
|
| 17 |
interface Props { |
| 18 |
data: AccountHierarchyNode; |
| 19 |
currency: string; |
| 20 |
width: number; |
| 21 |
height: number; |
| 22 |
} |
| 23 |
|
| 24 |
let { data, currency, width, height }: Props = $props(); |
| 25 |
|
| 26 |
let radius = $derived(Math.min(width, height) / 2); |
| 27 |
|
| 28 |
let root = $derived(partition<AccountHierarchyDatum>()(data)); |
| 29 |
let nodes = $derived( |
| 30 |
root.descendants().filter((d) => !d.data.dummy && d.depth > 0), |
| 31 |
); |
| 32 |
|
| 33 |
let current: AccountHierarchyNode | null = $state(null); |
| 34 |
|
| 35 |
$effect.pre(() => { |
| 36 |
// if-expression to run on each change of chart |
| 37 |
// eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 38 |
data; |
| 39 |
untrack(() => { |
| 40 |
current = null; |
| 41 |
}); |
| 42 |
}); |
| 43 |
|
| 44 |
function balanceText(d: AccountHierarchyNode): string { |
| 45 |
const val = d.value ?? 0; |
| 46 |
const total = root.value ?? 0; |
| 47 |
return total |
| 48 |
? `${$ctx.amount(val, currency)} (${formatPercentage(val / total)})` |
| 49 |
: $ctx.amount(val, currency); |
| 50 |
} |
| 51 |
|
| 52 |
const x = scaleLinear([0, 2 * Math.PI]); |
| 53 |
let y = $derived(scaleSqrt([0, radius])); |
| 54 |
let arcShape = $derived( |
| 55 |
arc<HierarchyRectangularNode<AccountHierarchyDatum>>() |
| 56 |
.startAngle((d) => x(d.x0)) |
| 57 |
.endAngle((d) => x(d.x1)) |
| 58 |
.innerRadius((d) => y(d.y0)) |
| 59 |
.outerRadius((d) => y(d.y1)), |
| 60 |
); |
| 61 |
</script> |
| 62 |
|
| 63 |
<g |
| 64 |
{width} |
| 65 |
{height} |
| 66 |
transform={`translate(${(width / 2).toString()},${(height / 2).toString()})`} |
| 67 |
onmouseleave={() => { |
| 68 |
current = null; |
| 69 |
}} |
| 70 |
role="img" |
| 71 |
> |
| 72 |
<circle style="opacity:0" r={radius} /> |
| 73 |
<text class="account" text-anchor="middle"> |
| 74 |
{(current ?? root).data.account} |
| 75 |
</text> |
| 76 |
<text class="balance" dy="1.2em" text-anchor="middle"> |
| 77 |
{balanceText(current ?? root)} |
| 78 |
</text> |
| 79 |
{#each nodes as d} |
| 80 |
<a href={$urlForAccount(d.data.account)} aria-label={d.data.account}> |
| 81 |
<path |
| 82 |
onmouseover={() => { |
| 83 |
current = d; |
| 84 |
}} |
| 85 |
onfocus={() => { |
| 86 |
current = d; |
| 87 |
}} |
| 88 |
class:half={current && !current.data.account.startsWith(d.data.account)} |
| 89 |
fill-rule="evenodd" |
| 90 |
fill={$sunburstScale(d.data.account)} |
| 91 |
d={arcShape(d)} |
| 92 |
role="img" |
| 93 |
/> |
| 94 |
</a> |
| 95 |
{/each} |
| 96 |
</g> |
| 97 |
|
| 98 |
<style> |
| 99 |
.half { |
| 100 |
opacity: 0.5; |
| 101 |
} |
| 102 |
|
| 103 |
.account { |
| 104 |
fill: var(--text-color); |
| 105 |
} |
| 106 |
|
| 107 |
.balance { |
| 108 |
font-family: var(--font-family-monospaced); |
| 109 |
} |
| 110 |
|
| 111 |
path { |
| 112 |
cursor: pointer; |
| 113 |
} |
| 114 |
</style> |