Skip to content

Commit

Permalink
fix tree.js and add various tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Aug 5, 2024
1 parent d0d0b70 commit 1f00372
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/transforms/tree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {stratify, tree} from "d3";
import {ascendingDefined} from "../defined.js";
import {column, identity, isObject, one, valueof} from "../options.js";
import {column, identity, isArray, isObject, one, valueof} from "../options.js";
import {basic} from "./basic.js";

export function treeNode({
Expand Down Expand Up @@ -40,7 +40,9 @@ export function treeNode({
for (const o of outputs) o[output_values] = o[output_setValues]([]);
for (const facet of facets) {
const treeFacet = [];
const root = rootof(facet.filter((i) => P[i] != null)).each((node) => (node.data = data[node.data]));
const root = rootof(facet.filter((i) => P[i] != null)).each(
isArray(data) ? (node) => (node.data = data[node.data]) : (node) => (node.data = data.get(node.data))
);
if (treeSort != null) root.sort(treeSort);
layout(root);
for (const node of root.descendants()) {
Expand Down
134 changes: 134 additions & 0 deletions test/output/arrowTestCustomOrder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions test/output/arrowTestDifferenceY.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions test/output/arrowTestSort.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions test/output/arrowTestTree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions test/plots/arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,100 @@ export async function arrowTestGroup() {
const data = Arrow.tableFromArrays({category, vector});
return Plot.barY(data, Plot.groupX({y: "count"}, {x: "vector", fill: "category"})).plot({marginLeft: 60});
}

/**
* An arrow table dataset supports sorting with a comparator.
*/
export async function arrowTestSort() {
const data = Arrow.tableFromArrays({
id: [1, 2, 3],
name: ["Alice", "Bob", "Charlie"],
age: [35, 25, 45]
});
return Plot.barX(data, {x: "age", fill: "name", sort: (a: {age: number}, b: {age: number}) => b.age - a.age}).plot();
}

/**
* An arrow table dataset supports accessing the node's datum.
*/
export async function arrowTestTree() {
const gods = Arrow.tableFromArrays({
branch: `Chaos Gaia Mountains
Chaos Gaia Pontus
Chaos Gaia Uranus
Chaos Eros
Chaos Erebus
Chaos Tartarus`
.split("\n")
.map((d) => d.replace(/\s+/g, "/"))
});
return Plot.plot({
axis: null,
insetLeft: 35,
insetTop: 20,
insetBottom: 20,
insetRight: 120,
marks: [Plot.tree(gods, {path: "branch", fill: (d) => d?.branch})]
});
}

/**
* An arrow table dataset supports Plot.find.
*/
export async function arrowTestDifferenceY() {
const stocks = Arrow.tableFromJSON(await readStocks());
return Plot.plot({
marks: [
Plot.differenceY(
stocks,
Plot.normalizeY(
Plot.groupX(
{y1: Plot.find((d) => d.Symbol === "GOOG"), y2: Plot.find((d) => d.Symbol === "AAPL")},
{x: "Date", y: "Close", tip: true}
)
)
)
]
});
}

async function readStocks(start = 0, end = Infinity) {
return (
await Promise.all(
["AAPL", "GOOG"].map((symbol) =>
d3.csv<any>(`data/${symbol.toLowerCase()}.csv`, (d, i) =>
start <= i && i < end ? ((d.Symbol = symbol), d3.autoType(d)) : null
)
)
)
).flat();
}

/**
* An arrow table dataset supports stack custom order.
*/
export async function arrowTestCustomOrder() {
const riaa = Arrow.tableFromJSON(await d3.csv<any>("data/riaa-us-revenue.csv", d3.autoType));
return Plot.plot({
y: {
grid: true,
label: "Annual revenue (billions, adj.)",
transform: (d) => d / 1000
},
marks: [
Plot.areaY(
riaa,
Plot.stackY({
x: "year",
y: "revenue",
z: "format",
order: (a, b) => d3.ascending(a.group, b.group) || d3.descending(a.revenue, b.revenue),
fill: "group",
stroke: "white",
title: (d) => `${d.format}\n${d.group}`
})
),
Plot.ruleY([0])
]
});
}

0 comments on commit 1f00372

Please sign in to comment.