Skip to content

Custom marks that return Plot objects instead of SVG #1976

Answered by Fil
Hvass-Labs asked this question in Q&A
Discussion options

You must be logged in to vote

In this case I would create the svg element directly:

(index, scales, values, dimensions) => {
  const [y_min, y_max] = scales.scales.y.domain;
  if (y_min < 0 && y_max >= 0) {
    const y = scales.scales.y.apply(0);
    return svg`<line
      stroke="red"
      stroke-dasharray="10"
      x1="${dimensions.marginLeft}"
      x2="${dimensions.width - dimensions.marginRight}"
      y1="${y}"
      y2="${y}"
    >`;
  }
}

However I reckon that this doesn't give you all the goodies of using a mark. You could also write this:

  if (y_min < 0 && y_max >= 0) {
    return Plot.ruleY([0], { strokeDasharray: 10, opacity: 0.5 }).plot({
      y: scales.scales.y,
      axis: null,
      ...dimensions…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@Hvass-Labs
Comment options

@Fil
Comment options

Answer selected by Hvass-Labs
@Hvass-Labs
Comment options

@mbostock
Comment options

@Hvass-Labs
Comment options

@Fil
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants