Skip to content

Commit

Permalink
default paintOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 25, 2022
1 parent 8518555 commit e6e3bd2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ The **fontSize** and **rotate** options can be specified as either channels or c

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.

If the **stroke** option is specified, the **paintOrder** option defaults to stroke, and the **strokeWidth** option defaults to 3, making it easy to create a “halo” around text labels.
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.

#### Plot.text(*data*, *options*)

Expand Down
9 changes: 4 additions & 5 deletions src/marks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {Mark} from "../plot.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform, offset, impliedString, applyFrameAnchor} from "../style.js";

const defaults = {
strokeLinejoin: "round"
strokeLinejoin: "round",
strokeWidth: 3,
paintOrder: "stroke"
};

export class Text extends Mark {
Expand Down Expand Up @@ -39,10 +41,7 @@ export class Text extends Mark {
{name: "rotate", value: numberChannel(vrotate), optional: true},
{name: "text", value: text, filter: nonempty}
],
{
...options.stroke && {paintOrder: "stroke", strokeWidth: 3},
...options
},
options,
defaults
);
this.rotate = crotate;
Expand Down
7 changes: 6 additions & 1 deletion src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function styles(
strokeWidth: defaultStrokeWidth,
strokeLinecap: defaultStrokeLinecap,
strokeLinejoin: defaultStrokeLinejoin,
strokeMiterlimit: defaultStrokeMiterlimit
strokeMiterlimit: defaultStrokeMiterlimit,
paintOrder: defaultPaintOrder
}
) {

Expand Down Expand Up @@ -73,6 +74,10 @@ export function styles(
if (strokeLinecap === undefined) strokeLinecap = defaultStrokeLinecap;
if (strokeLinejoin === undefined) strokeLinejoin = defaultStrokeLinejoin;
if (strokeMiterlimit === undefined) strokeMiterlimit = defaultStrokeMiterlimit;

// The paint order only takes effect if there is both a fill and a stroke
// (at least if we ignore markers, which no built-in marks currently use).
if (cfill !== "none" && paintOrder === undefined) paintOrder = defaultPaintOrder;
}

const [vstrokeWidth, cstrokeWidth] = maybeNumberChannel(strokeWidth);
Expand Down
2 changes: 1 addition & 1 deletion test/output/metroInequalityChange.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion test/plots/metro-inequality-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default async function() {
text: "nyt_display",
fill: "black",
stroke: "white",
strokeWidth: 4,
dy: -8
})
]
Expand Down

0 comments on commit e6e3bd2

Please sign in to comment.