| 1 |
<script lang="ts"> |
| 2 |
import type { HierarchyRectangularNode } from "d3-hierarchy"; |
| 3 |
import { treemap } from "d3-hierarchy"; |
| 4 |
import type { Action } from "svelte/action"; |
| 5 |
|
| 6 |
import { formatPercentage } from "../format"; |
| 7 |
import { urlForAccount } from "../helpers"; |
| 8 |
import { ctx } from "../stores/format"; |
| 9 |
import { treemapScale } from "./helpers"; |
| 10 |
import type { |
| 11 |
AccountHierarchyDatum, |
| 12 |
AccountHierarchyNode, |
| 13 |
} from "./hierarchy"; |
| 14 |
import { domHelpers, followingTooltip } from "./tooltip"; |
| 15 |
|
| 16 |
interface Props { |
| 17 |
data: AccountHierarchyNode; |
| 18 |
width: number; |
| 19 |
currency: string; |
| 20 |
} |
| 21 |
|
| 22 |
let { data, width, currency }: Props = $props(); |
| 23 |
|
| 24 |
let height = $derived(Math.min(width / 2.5, 400)); |
| 25 |
|
| 26 |
const tree = treemap<AccountHierarchyDatum>().paddingInner(2).round(true); |
| 27 |
let root = $derived(tree.size([width, height])(data)); |
| 28 |
let leaves = $derived( |
| 29 |
root.leaves().filter((d) => d.value != null && d.value !== 0), |
| 30 |
); |
| 31 |
|
| 32 |
function fill(d: AccountHierarchyNode) { |
| 33 |
const node = d.data.dummy && d.parent ? d.parent : d; |
| 34 |
if (node.depth === 1 || !node.parent) { |
| 35 |
return $treemapScale(node.data.account); |
| 36 |
} |
| 37 |
return $treemapScale(node.parent.data.account); |
| 38 |
} |
| 39 |
|
| 40 |
function tooltipText(d: AccountHierarchyNode) { |
| 41 |
const val = d.value ?? 0; |
| 42 |
const rootValue = root.value ?? 1; |
| 43 |
|
| 44 |
return [ |
| 45 |
domHelpers.t( |
| 46 |
`${$ctx.amount(val, currency)} (${formatPercentage(val / rootValue)})`, |
| 47 |
), |
| 48 |
domHelpers.em(d.data.account), |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** Hide account names that are too long. */ |
| 53 |
const setVisibility: Action< |
| 54 |
SVGTextElement, |
| 55 |
HierarchyRectangularNode<AccountHierarchyDatum> |
| 56 |
> = (node, param) => { |
| 57 |
function update(d: HierarchyRectangularNode<AccountHierarchyDatum>) { |
| 58 |
const length = node.getComputedTextLength(); |
| 59 |
node.style.visibility = |
| 60 |
d.x1 - d.x0 > length + 4 && d.y1 - d.y0 > 14 ? "visible" : "hidden"; |
| 61 |
} |
| 62 |
update(param); |
| 63 |
return { update }; |
| 64 |
}; |
| 65 |
</script> |
| 66 |
|
| 67 |
<svg viewBox={`0 0 ${width.toString()} ${height.toString()}`}> |
| 68 |
{#each leaves as d} |
| 69 |
<g |
| 70 |
transform={`translate(${d.x0.toString()},${d.y0.toString()})`} |
| 71 |
use:followingTooltip={() => tooltipText(d)} |
| 72 |
> |
| 73 |
<rect fill={fill(d)} width={d.x1 - d.x0} height={d.y1 - d.y0} /> |
| 74 |
<a href={$urlForAccount(d.data.account)}> |
| 75 |
<text |
| 76 |
use:setVisibility={d} |
| 77 |
dy=".5em" |
| 78 |
x={(d.x1 - d.x0) / 2} |
| 79 |
y={(d.y1 - d.y0) / 2} |
| 80 |
text-anchor="middle" |
| 81 |
> |
| 82 |
{d.data.account.split(":").pop() ?? ""} |
| 83 |
</text> |
| 84 |
</a> |
| 85 |
</g> |
| 86 |
{/each} |
| 87 |
</svg> |
| 88 |
|
| 89 |
<style> |
| 90 |
svg { |
| 91 |
shape-rendering: crispedges; |
| 92 |
} |
| 93 |
</style> |