Skip to content

Commit 5ba59d9

Browse files
Filmbostock
andauthored
make it easier to create a text halo (#703)
* make it easier to create a text halo by defaulting paintOrder to stroke and strokeWidth to 3 if the stroke option is specified. supersedes #667 closes #666 * default paintOrder * currentColor, not black Co-authored-by: Mike Bostock <[email protected]>
1 parent 396d968 commit 5ba59d9

File tree

7 files changed

+16
-7
lines changed

7 files changed

+16
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,8 @@ The **fontSize** and **rotate** options can be specified as either channels or c
11281128

11291129
If the **frameAnchor** option is not specified, then **textAnchor** and **lineAnchor** default to middle. Otherwise, **textAnchor** defaults to start if **frameAnchor** is on the left, end if **frameAnchor** is on the right, and otherwise middle. Similarly, **lineAnchor** defaults to top if **frameAnchor** is on the top, bottom if **frameAnchor** is on the bottom, and otherwise middle.
11301130

1131+
The **paintOrder** option defaults to “stroke” and the **strokeWidth** option defaults to 3. By setting **fill** to the foreground color and **stroke** to the background color (such as black and white, respectively), you can surround text with a “halo” which may improve legibility against a busy background.
1132+
11311133
#### Plot.text(*data*, *options*)
11321134

11331135
Returns a new text mark with the given *data* and *options*. If neither the **x** nor **y** nor **frameAnchor** options are specified, *data* is assumed to be an array of pairs [[*x₀*, *y₀*], [*x₁*, *y₁*], [*x₂*, *y₂*], …] such that **x** = [*x₀*, *x₁*, *x₂*, …] and **y** = [*y₀*, *y₁*, *y₂*, …].

src/marks/text.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {Mark} from "../plot.js";
66
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform, offset, impliedString, applyFrameAnchor} from "../style.js";
77

88
const defaults = {
9-
strokeLinejoin: "round"
9+
strokeLinejoin: "round",
10+
strokeWidth: 3,
11+
paintOrder: "stroke"
1012
};
1113

1214
export class Text extends Mark {

src/style.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export function styles(
3232
strokeWidth: defaultStrokeWidth,
3333
strokeLinecap: defaultStrokeLinecap,
3434
strokeLinejoin: defaultStrokeLinejoin,
35-
strokeMiterlimit: defaultStrokeMiterlimit
35+
strokeMiterlimit: defaultStrokeMiterlimit,
36+
paintOrder: defaultPaintOrder
3637
}
3738
) {
3839

@@ -75,6 +76,10 @@ export function styles(
7576
if (strokeLinecap === undefined) strokeLinecap = defaultStrokeLinecap;
7677
if (strokeLinejoin === undefined) strokeLinejoin = defaultStrokeLinejoin;
7778
if (strokeMiterlimit === undefined) strokeMiterlimit = defaultStrokeMiterlimit;
79+
80+
// The paint order only takes effect if there is both a fill and a stroke
81+
// (at least if we ignore markers, which no built-in marks currently use).
82+
if (cfill !== "none" && paintOrder === undefined) paintOrder = defaultPaintOrder;
7883
}
7984

8085
const [vstrokeWidth, cstrokeWidth] = maybeNumberChannel(strokeWidth);

test/output/carsParcoords.svg

Lines changed: 1 addition & 2 deletions
Loading

test/output/metroInequalityChange.svg

Lines changed: 1 addition & 1 deletion
Loading

test/plots/cars-parcoords.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export default async function() {
3737
marks: [
3838
Plot.ruleY(dimensions),
3939
Plot.line(data, {...xy, z: "name", stroke: "#444", strokeWidth: 0.5, strokeOpacity: 0.5}),
40-
Plot.text(ticks, {...xy, fill: null, stroke: "white", strokeWidth: 3, strokeLinejoin: "round", text: "value"}),
41-
Plot.text(ticks, {...xy, text: "value"})
40+
Plot.text(ticks, {...xy, text: "value", fill: "black", stroke: "white"})
4241
]
4342
});
4443
}

test/plots/metro-inequality-change.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export default async function() {
3232
y: "R90_10_2015",
3333
filter: "highlight",
3434
text: "nyt_display",
35+
fill: "currentColor",
36+
stroke: "white",
3537
dy: -8
3638
})
3739
]

0 commit comments

Comments
 (0)