Note that there are some explanatory texts on larger screens.

plurals
  1. PORestrict brush to discrete ranges
    primarykey
    data
    text
    <p>I'd like to get a user to select a range of values using a <code>brush</code>. However, only discrete values make sense in my application, so I'd like to restrict the brush to these discrete values (integers, say).</p> <p>One way of doing this is to use the approach described at <a href="http://bl.ocks.org/mbostock/4349509" rel="nofollow noreferrer">Ordinal Brushing</a>. In that example, the brush itself can take continuous values, which are then mapped to the discrete values of the ordinal scale.</p> <p>I would like to get a brush that snaps to discrete values while the user is brushing or dragging the brush. I came up with something that basically works: Rounding the <code>target.extent</code> and then reselecting the rounded range (<a href="http://jsfiddle.net/8X25N/1" rel="nofollow noreferrer">Fiddle</a>):</p> <pre><code>function brush() { var s = d3.event.target.extent(); if (d3.event.mode === "move") { var extentlength = Math.round(s[1] - s[0]) d3.event.target.extent([Math.round(s[0] + 0.5) - 0.5, Math.round(s[0] + 0.5) - 0.5 + extentlength]) } else { d3.event.target.extent([Math.round(s[0] + 0.5) - 0.5, Math.round(s[1] + 0.5) - 0.5]) } d3.event.target(d3.select(this)) } </code></pre> <p><a href="http://jsfiddle.net/8X25N/1" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qLoop.png" alt=""></a></p> <p>However, this feels a bit clunky, and it leads to the undesirable behaviour that while the user is dragging the brush, the cursor can hover on the brush's edge, turning the cursor symbol from the "drag"-hand to the "resize"-arrow.</p> <p>Is there a more elegant and robust way of getting a brush that only allows for the selection of discrete ranges?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload