Skip to content

Commit

Permalink
tweak textLayout default
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 15, 2023
1 parent c601427 commit 8cb8b43
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
6 changes: 4 additions & 2 deletions docs/marks/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ Any additional *options* are passed through to the constituent link, dot, and te

The **textLayout** option controls how text labels are anchored to the node. Two layouts are supported:

* *mirrored* - leaf-node labels are left-anchored, and non-leaf nodes right-anchored (with a -dx offset); default unless a **treeLayout** is specified
* *normal* - all labels are left-anchored; default if a **treeLayout** is specified
* *mirrored* - leaf-node labels are left-anchored, and non-leaf nodes right-anchored
* *normal* - all labels are left-anchored

If the **treeLayout** is d3.tree or d3.cluster, the **textLayout** defaults to *mirrored*; otherwise it defaults to *normal*.

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

Expand Down
20 changes: 11 additions & 9 deletions src/marks/tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {cluster as Cluster} from "d3";
import {cluster as Cluster, tree as Tree} from "d3";
import {marks} from "../mark.js";
import {isNoneish} from "../options.js";
import {maybeTreeAnchor, treeLink, treeNode} from "../transforms/tree.js";
Expand Down Expand Up @@ -29,21 +29,20 @@ export function tree(
dx,
dy,
textAnchor,
textLayout,
treeLayout = Tree,
textLayout = treeLayout === Tree || treeLayout === Cluster ? "mirrored" : "normal",
...options
} = {}
) {
if (dx === undefined) dx = maybeTreeAnchor(options.treeAnchor).dx;
if (textAnchor !== undefined) throw new Error("textAnchor is not a configurable tree option");
textLayout = keyword(
textLayout === undefined ? (options.treeLayout === undefined ? "mirrored" : "normal") : textLayout,
"textLayout",
["mirrored", "normal"]
);
textLayout = keyword(textLayout, "textLayout", ["mirrored", "normal"]);

function treeText(textOptions) {
return text(
data,
treeNode({
treeLayout,
text: textText,
fill: fill === undefined ? "currentColor" : fill,
stroke: textStroke,
Expand All @@ -60,6 +59,7 @@ export function tree(
link(
data,
treeLink({
treeLayout,
markerStart,
markerEnd,
stroke: stroke !== undefined ? stroke : fill === undefined ? "node:internal" : fill,
Expand All @@ -73,7 +73,9 @@ export function tree(
...options
})
),
dotDot ? dot(data, treeNode({fill: fill === undefined ? "node:internal" : fill, title, ...options})) : null,
dotDot
? dot(data, treeNode({treeLayout, fill: fill === undefined ? "node:internal" : fill, title, ...options}))
: null,
textText != null
? textLayout === "mirrored"
? [
Expand All @@ -86,5 +88,5 @@ export function tree(
}

export function cluster(data, options) {
return tree(data, {textLayout: "mirrored", ...options, treeLayout: Cluster});
return tree(data, {...options, treeLayout: Cluster});
}

0 comments on commit 8cb8b43

Please sign in to comment.