Skip to content

text dx and dy cannot accept function #1528

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

Closed
winner106 opened this issue May 8, 2023 · 3 comments
Closed

text dx and dy cannot accept function #1528

winner106 opened this issue May 8, 2023 · 3 comments

Comments

@winner106
Copy link

 Plot.text(driving, {x: "miles", y: "gas", text: (d) => `${d.year}`, dy: d.year % 5 === 0 ? -6 : 10})

the following code not working

dy: d.year % 5 === 0 ? -6 : 10
@yurivish
Copy link
Contributor

yurivish commented May 8, 2023

You would need to pass d as an argument to an anonymous function:

dy: (d) => d.year % 5 === 0 ? -6 : 10

This still wouldn't work, though, because dx and dy are (currently) constant options rather than channels: https://observablehq.com/plot/features/marks#mark-options

Option values that must be the same for all of a mark’s generated shapes are known as constants, whereas option values that may vary across a mark’s generated shapes are known as channels.

There's some discussion in this PR about converting more options to channels: #909

@mbostock
Copy link
Member

mbostock commented May 8, 2023

The dx and dy options are constant options, not channels. If you want to specify varying values, you need to declare multiple marks. For example, you could filter:

[
  Plot.text(driving, {filter: (d) => d.year % 5 === 0, x: "miles", y: "gas", text: (d) => `${d.year}`, dy: -6}),
  Plot.text(driving, {filter: (d) => d.year % 5 !== 0, x: "miles", y: "gas", text: (d) => `${d.year}`, dy: 10}),
]

@mbostock mbostock closed this as completed May 8, 2023
@winner106
Copy link
Author

thank you very much, I got it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants