Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it easier to create a text halo #703

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,8 @@ 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.

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*)

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₂*, …].
Expand Down
4 changes: 3 additions & 1 deletion 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
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
3 changes: 1 addition & 2 deletions test/output/carsParcoords.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
3 changes: 1 addition & 2 deletions test/plots/cars-parcoords.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export default async function() {
marks: [
Plot.ruleY(dimensions),
Plot.line(data, {...xy, z: "name", stroke: "#444", strokeWidth: 0.5, strokeOpacity: 0.5}),
Plot.text(ticks, {...xy, fill: null, stroke: "white", strokeWidth: 3, strokeLinejoin: "round", text: "value"}),
Plot.text(ticks, {...xy, text: "value"})
Plot.text(ticks, {...xy, text: "value", fill: "black", stroke: "white"})
]
});
}
2 changes: 2 additions & 0 deletions test/plots/metro-inequality-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default async function() {
y: "R90_10_2015",
filter: "highlight",
text: "nyt_display",
fill: "currentColor",
stroke: "white",
dy: -8
})
]
Expand Down