-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
50,573 additions
and
32,999 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { stratify, treemap } from "d3-hierarchy"; | ||
import { select } from "d3-selection"; | ||
import { group } from "d3-array"; | ||
import { addBalanceDepthInput, emptyElement, MainView, State } from "./views"; | ||
import { scaleOrdinal } from "d3-scale"; | ||
import { schemePastel1 } from "d3-scale-chromatic"; | ||
import { AccountBalanceAndTotal } from "./utils"; | ||
|
||
// based on https://observablehq.com/@d3/nested-treemap | ||
export function viewBalancesChart(options?: { | ||
negated?: boolean; // is this negatively denominated account (e.g. Income/Liability) | ||
}) { | ||
const containerSelector = MainView; | ||
const account = State.SelectedAccount; | ||
const opts = { negated: false }; // defaults | ||
Object.assign(opts, options); | ||
emptyElement(containerSelector); | ||
addBalanceDepthInput(containerSelector); | ||
let root = stratify<AccountBalanceAndTotal>() | ||
.id(({ account }) => account.fullName) | ||
.parentId(({ account }) => | ||
account.parent && account.parent != State.SelectedAccount.parent | ||
? account.parent.fullName | ||
: undefined | ||
)(account.withAllChildBalances(State.EndDate)); | ||
root = root.sum((a) => a.balance.toNumber()); | ||
|
||
const [width, height] = [1200, 800]; | ||
const tm = treemap<AccountBalanceAndTotal>() | ||
.size([width, height]) | ||
.padding(4) | ||
.paddingTop(20); | ||
const nodes = tm(root); | ||
|
||
let uidCounter = 0; | ||
|
||
const svg = select(containerSelector) | ||
.append("svg") | ||
.attr("id", "chart") | ||
.attr("width", "100%") | ||
.attr("height", height); | ||
// .attr("viewBox", [0, 0, width, height]); | ||
// .attr( | ||
// "style", | ||
// "max-width: 100%; height: auto; overflow: visible; font: 10px sans-serif;" | ||
// ); | ||
|
||
const color = scaleOrdinal([0, 10], schemePastel1); | ||
|
||
const node = svg | ||
.selectAll("g") | ||
.data(group(nodes, (d) => d.height)) | ||
.join("g") | ||
.selectAll("g") | ||
.data((d) => d[1]) | ||
.join("g") | ||
.attr("transform", (d) => `translate(${d.x0},${d.y0})`); | ||
|
||
node | ||
.append("title") | ||
.text(({ data }) => | ||
data.account.children.length > 0 | ||
? `${data.account.fullName} ${data.total} [ ${data.balance} ]` | ||
: `${data.account.fullName} ${data.balance}` | ||
); | ||
|
||
node | ||
.append("rect") | ||
.attr("id", (d: any) => (d.nodeUid = `node-${uidCounter++}`)) | ||
.attr("fill", (d) => color(d.height)) | ||
.attr("width", (d) => d.x1 - d.x0) | ||
.attr("height", (d) => d.y1 - d.y0); | ||
|
||
node | ||
.append("clipPath") | ||
.attr("id", (d: any) => (d.clipUid = `clip-${uidCounter++}`)) | ||
.append("use") | ||
.attr("xlink:href", (d: any) => `#${d.nodeUid}`); | ||
|
||
node | ||
.append("text") | ||
.attr("clip-path", (d: any) => `url(#${d.clipUid})`) | ||
.selectAll("tspan") | ||
.data(({ data }) => { | ||
const bits = [data.account.name, data.total.toString()]; | ||
if (data.account.children.length > 0) bits.push(data.balance.toString()); | ||
return bits; | ||
}) | ||
.join("tspan") | ||
.text((d) => d); | ||
|
||
const narrowBoxLimit = 150; | ||
node | ||
.filter((d: any) => d.x1 - d.x0 > narrowBoxLimit) | ||
.selectAll("tspan") | ||
.attr("dx", 10) | ||
.attr("y", 15); | ||
|
||
node | ||
.filter((d) => d.x1 - d.x0 <= narrowBoxLimit) | ||
.selectAll("tspan") | ||
.attr("x", 10) | ||
.attr("dy", 15); | ||
} |
Oops, something went wrong.