| 1 |
import { sum } from "d3-array"; |
| 2 |
import type { HierarchyNode } from "d3-hierarchy"; |
| 3 |
import { hierarchy as d3Hierarchy } from "d3-hierarchy"; |
| 4 |
import type { Writable } from "svelte/store"; |
| 5 |
import { writable } from "svelte/store"; |
| 6 |
|
| 7 |
import type { Result } from "../lib/result"; |
| 8 |
import type { TreeNode } from "../lib/tree"; |
| 9 |
import type { ValidationError, Validator } from "../lib/validation"; |
| 10 |
import { |
| 11 |
array, |
| 12 |
boolean, |
| 13 |
defaultValue, |
| 14 |
lazy, |
| 15 |
number, |
| 16 |
object, |
| 17 |
optional, |
| 18 |
record, |
| 19 |
string, |
| 20 |
unknown, |
| 21 |
} from "../lib/validation"; |
| 22 |
import { notify_warn } from "../notifications"; |
| 23 |
import { sort_by_strings } from "../sort"; |
| 24 |
import type { ChartContext } from "./context"; |
| 25 |
|
| 26 |
/** The data provided with a fava.core.tree.SerialisedTreeNode. */ |
| 27 |
export type AccountTreeNode = TreeNode<{ |
| 28 |
readonly account: string; |
| 29 |
readonly balance: Record<string, number>; |
| 30 |
readonly balance_children: Record<string, number>; |
| 31 |
readonly cost: Record<string, number> | null; |
| 32 |
readonly cost_children: Record<string, number> | null; |
| 33 |
readonly has_txns: boolean; |
| 34 |
}>; |
| 35 |
|
| 36 |
/** The data for a single account in a d3-hierarchy. */ |
| 37 |
export type AccountHierarchyDatum = TreeNode<{ |
| 38 |
readonly account: string; |
| 39 |
readonly balance: Record<string, number>; |
| 40 |
readonly dummy: boolean; |
| 41 |
}>; |
| 42 |
|
| 43 |
export type AccountHierarchyInputDatum = TreeNode<{ |
| 44 |
readonly account: string; |
| 45 |
readonly balance: Record<string, number>; |
| 46 |
}>; |
| 47 |
|
| 48 |
/** A d3-hierarchy node for an account. */ |
| 49 |
export type AccountHierarchyNode = HierarchyNode<AccountHierarchyDatum>; |
| 50 |
|
| 51 |
/** |
| 52 |
* Add internal nodes as dummy leaf nodes to their own children. |
| 53 |
* |
| 54 |
* In the treemap, we only render leaf nodes, so for accounts that have both |
| 55 |
* children and a balance, we want to duplicate them as leaf nodes. |
| 56 |
*/ |
| 57 |
export function addInternalNodesAsLeaves({ |
| 58 |
account, |
| 59 |
balance, |
| 60 |
children, |
| 61 |
}: AccountHierarchyInputDatum): AccountHierarchyDatum { |
| 62 |
if (children.length) { |
| 63 |
const c = children.map(addInternalNodesAsLeaves); |
| 64 |
c.push({ account, balance, children: [], dummy: true }); |
| 65 |
return { account, balance: {}, children: c, dummy: false }; |
| 66 |
} |
| 67 |
return { account, balance, children: [], dummy: false }; |
| 68 |
} |
| 69 |
|
| 70 |
export class HierarchyChart { |
| 71 |
readonly type = "hierarchy"; |
| 72 |
|
| 73 |
/** All currencies for which we have an hierarchy. */ |
| 74 |
readonly currencies: readonly string[]; |
| 75 |
|
| 76 |
/** The currency to show the treemap of. */ |
| 77 |
readonly treemap_currency: Writable<string> | null; |
| 78 |
|
| 79 |
constructor( |
| 80 |
readonly name: string | null, |
| 81 |
readonly data: ReadonlyMap<string, AccountHierarchyNode>, |
| 82 |
) { |
| 83 |
this.currencies = [...this.data.keys()]; |
| 84 |
const first_currency = this.currencies[0]; |
| 85 |
this.treemap_currency = |
| 86 |
first_currency != null ? writable<string>(first_currency) : null; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
const sort_children = (values: AccountTreeNode[]) => |
| 91 |
sort_by_strings(values, (v) => v.account); |
| 92 |
|
| 93 |
const inventory = record(number); |
| 94 |
|
| 95 |
export const account_hierarchy_validator: Validator<AccountTreeNode> = object({ |
| 96 |
account: string, |
| 97 |
balance: inventory, |
| 98 |
balance_children: inventory, |
| 99 |
children: lazy( |
| 100 |
() => (json) => array(account_hierarchy_validator)(json).map(sort_children), |
| 101 |
), |
| 102 |
cost: optional(inventory), |
| 103 |
cost_children: optional(inventory), |
| 104 |
has_txns: defaultValue(boolean, () => false), |
| 105 |
}); |
| 106 |
|
| 107 |
export function hierarchy_from_parsed_data( |
| 108 |
label: string | null, |
| 109 |
data: AccountHierarchyInputDatum, |
| 110 |
{ currencies }: ChartContext, |
| 111 |
): HierarchyChart { |
| 112 |
const root = addInternalNodesAsLeaves(data); |
| 113 |
return new HierarchyChart( |
| 114 |
label, |
| 115 |
new Map( |
| 116 |
currencies |
| 117 |
.map((currency) => { |
| 118 |
const r = d3Hierarchy<AccountHierarchyDatum>(root); |
| 119 |
const root_balance = sum( |
| 120 |
r.descendants(), |
| 121 |
(n) => n.data.balance[currency] ?? 0, |
| 122 |
); |
| 123 |
// depending on the balance for this currency in the root, |
| 124 |
// build the tree either for all positive values or all negative values |
| 125 |
const sign = root_balance ? Math.sign(root_balance) : 1; |
| 126 |
r.sum( |
| 127 |
(d) => sign * Math.max(sign * (d.balance[currency] ?? 0), 0), |
| 128 |
).sort((a, b) => sign * ((b.value ?? 0) - (a.value ?? 0))); |
| 129 |
return [currency, r] as const; |
| 130 |
}) |
| 131 |
.filter(([, h]) => h.value != null && h.value !== 0), |
| 132 |
), |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
const hierarchy_data_with_modifier = object({ |
| 137 |
modifier: number, |
| 138 |
root: unknown, |
| 139 |
}); |
| 140 |
|
| 141 |
export function hierarchy( |
| 142 |
label: string | null, |
| 143 |
json: unknown, |
| 144 |
$chartContext: ChartContext, |
| 145 |
): Result<HierarchyChart, ValidationError> { |
| 146 |
const with_modifier = hierarchy_data_with_modifier(json); |
| 147 |
if (with_modifier.is_ok) { |
| 148 |
notify_warn( |
| 149 |
"Tree for the hierarchy chart should now be specified at the top-level directly.\n" + |
| 150 |
"{ modifier: 1, root: { ...children } } -> { ...children }", |
| 151 |
); |
| 152 |
} |
| 153 |
const root = with_modifier.is_ok ? with_modifier.value.root : json; |
| 154 |
return account_hierarchy_validator(root).map((r) => |
| 155 |
hierarchy_from_parsed_data(label, r, $chartContext), |
| 156 |
); |
| 157 |
} |