Skip to content

Commit 56729e3

Browse files
committed
textLayout option
1 parent a09910a commit 56729e3

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

docs/marks/tree.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Plot.plot({
7575
```
7676
:::
7777

78-
The **treeLayout** option specifies the layout algorithm. The tree mark uses the Reingold–Tilford “tidy” tree algorithm, [d3.tree](https://github.com/d3/d3-hierarchy/blob/main/README.md#tree), by default. The [cluster](#cluster-data-options) convenience method sets **treeLayout** to [d3.cluster](https://github.com/d3/d3-hierarchy/blob/main/README.md#cluster), aligning the leaf nodes. Both default the **textBalanced** option to true, drawing non-leaf node labels to the left of the node.
78+
The **treeLayout** option specifies the layout algorithm. The tree mark uses the Reingold–Tilford “tidy” tree algorithm, [d3.tree](https://github.com/d3/d3-hierarchy/blob/main/README.md#tree), by default. The [cluster](#cluster-data-options) convenience method sets **treeLayout** to [d3.cluster](https://github.com/d3/d3-hierarchy/blob/main/README.md#cluster), aligning the leaf nodes.
7979

8080
:::plot https://observablehq.com/@observablehq/plot-cluster-flare
8181
```js
@@ -151,13 +151,16 @@ The following options are supported:
151151
* **title** - the text and dot title; defaults to *node:path*
152152
* **text** - the text label; defaults to *node:name*
153153
* **textStroke** - the text stroke; defaults to *white*
154-
* **textBalanced** - the orientation of non-leaf labels
155-
* **dx** - the text horizontal offset; defaults to 6 if left-anchored, or -6 if right-anchored
154+
* **textLayout** - the text anchoring layout
155+
* **dx** - the text horizontal offset; defaults to 6
156156
* **dy** - the text vertical offset; defaults to 0
157157

158158
Any additional *options* are passed through to the constituent link, dot, and text marks and their corresponding treeLink or treeNode transform.
159159

160-
Text labels for leaf nodes are drawn to the right of the node; the **textBalanced** option controls whether labels for non-leaf nodes are drawn to the left of the node. It defaults to true, unless a **treeLayout** is specified (*e.g.*, the custom indent layout above).
160+
The **textLayout** option controls how text labels are anchored to the node. Two layouts are supported:
161+
162+
* *mirrored* - leaf-node labels are left-anchored, and non-leaf nodes right-anchored (with a -dx offset); default unless a **treeLayout** is specified
163+
* *normal* - all labels are left-anchored; default if a **treeLayout** is specified
161164

162165
## tree(*data*, *options*)
163166

@@ -173,4 +176,4 @@ Returns a new tree mark with the given *data* and *options*.
173176
Plot.cluster(flare, {path: "name", delimiter: "."})
174177
```
175178

176-
Like [tree](#tree-data-options), except sets the **treeLayout** option to [d3.cluster](https://github.com/d3/d3-hierarchy/blob/main/README.md#cluster), aligning leaf nodes.
179+
Like [tree](#tree-data-options), except sets the **treeLayout** option to [d3.cluster](https://github.com/d3/d3-hierarchy/blob/main/README.md#cluster), aligning leaf nodes, and defaults the **textLayout** option to *mirrored*.

src/marks/tree.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ export interface TreeOptions extends DotOptions, LinkOptions, TextOptions, TreeT
2121
textStroke?: MarkOptions["stroke"];
2222

2323
/**
24-
* Whether labels for non-leaf nodes are drawn to the left of the node;
25-
* defaults to true unless a **treeLayout** is specified.
24+
* Layout for node labels: if *mirrored*, leaf-node labels are left-anchored,
25+
* and non-leaf nodes right-anchored (with a -dx offset). If *normal*, all
26+
* labels are left-anchored. Defaults to *mirrored* unless a **treeLayout**
27+
* has been specified.
2628
*/
27-
textBalanced?: boolean;
29+
textLayout?: "mirrored" | "normal";
2830
}
2931

3032
/**
@@ -46,7 +48,7 @@ export function tree(data?: Data, options?: TreeOptions): CompoundMark;
4648
* option, placing leaf nodes of the tree at the same depth. Equivalent to:
4749
*
4850
* ```js
49-
* Plot.tree(data, {...options, treeLayout: d3.cluster})
51+
* Plot.tree(data, {...options, treeLayout: d3.cluster, textLayout: "mirrored"})
5052
* ```
5153
*
5254
* [1]: https://github.com/d3/d3-hierarchy/blob/main/README.md#cluster

src/marks/tree.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {tree as Tree, cluster as Cluster} from "d3";
1+
import {cluster as Cluster} from "d3";
22
import {marks} from "../mark.js";
33
import {isNoneish} from "../options.js";
44
import {maybeTreeAnchor, treeLink, treeNode} from "../transforms/tree.js";
55
import {dot} from "./dot.js";
66
import {link} from "./link.js";
77
import {text} from "./text.js";
8+
import {keyword} from "../options.js";
89

910
export function tree(
1011
data,
@@ -28,14 +29,17 @@ export function tree(
2829
dx,
2930
dy,
3031
textAnchor,
31-
textBalanced,
32+
textLayout,
3233
...options
3334
} = {}
3435
) {
35-
const {treeLayout = Tree} = options;
3636
if (dx === undefined) dx = maybeTreeAnchor(options.treeAnchor).dx;
3737
if (textAnchor !== undefined) throw new Error("textAnchor is not a configurable tree option");
38-
if (textBalanced === undefined) textBalanced = treeLayout === Tree || treeLayout === Cluster;
38+
textLayout = keyword(
39+
textLayout === undefined ? (options.treeLayout === undefined ? "mirrored" : "normal") : textLayout,
40+
"textLayout",
41+
["mirrored", "normal"]
42+
);
3943
function treeText(textOptions) {
4044
return text(
4145
data,
@@ -71,7 +75,7 @@ export function tree(
7175
),
7276
dotDot ? dot(data, treeNode({fill: fill === undefined ? "node:internal" : fill, title, ...options})) : null,
7377
textText != null
74-
? textBalanced
78+
? textLayout === "mirrored"
7579
? [
7680
treeText({textAnchor: "start", treeFilter: "node:external"}),
7781
treeText({textAnchor: "end", treeFilter: "node:internal", dx: -dx})
@@ -82,5 +86,5 @@ export function tree(
8286
}
8387

8488
export function cluster(data, options) {
85-
return tree(data, {...options, treeLayout: Cluster});
89+
return tree(data, {textLayout: "mirrored", ...options, treeLayout: Cluster});
8690
}

0 commit comments

Comments
 (0)