Skip to content

make it easier to create a text halo #703

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

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.

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.

#### 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
5 changes: 4 additions & 1 deletion src/marks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export class Text extends Mark {
{name: "rotate", value: numberChannel(vrotate), optional: true},
{name: "text", value: text, filter: nonempty}
],
options,
{
...options.stroke && {paintOrder: "stroke", strokeWidth: 3},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strokeWidth only has an effect if there is a stroke, so we can simply move this to up the defaults declaration. Likewise, we could move the paintOrder up to defaults too and add some logic to style.js so only apply the default paintOrder if there’s both a fill and a stroke.

Alternatively, I was thinking that the strokeWidth would be 3 only if both a fill and stroke are specified, and that it would be 1.5 (or 1?) if only the stroke were specified.

...options
},
defaults
);
this.rotate = crotate;
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"})
]
});
}
3 changes: 3 additions & 0 deletions test/plots/metro-inequality-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export default async function() {
y: "R90_10_2015",
filter: "highlight",
text: "nyt_display",
fill: "black",
stroke: "white",
strokeWidth: 4,
dy: -8
})
]
Expand Down