react-vis icon indicating copy to clipboard operation
react-vis copied to clipboard

(XY-Plot) Getting X,Y value from mouse event coordinates

Open anselmo opened this issue 9 years ago • 7 comments

Wonder if someone can point me in the right direction.

How can one get the (x, y) value from mouse event coordinates?

anselmo avatar Mar 19 '17 23:03 anselmo

Hey @anselmo, can you say more about your use case?

mcnuttandrew avatar Mar 20 '17 05:03 mcnuttandrew

@mcnuttandrew, thanks for your quick reply. I'm putting my head around a brush component.

I've come up with a straightforward approach to calculate the correspondent X axis value from an event coordinate.

I'm not sure if there's a canonical way to get it from within abstract-series or scale-utils.

My ad hoc take on it:

  _getXCoordinateByDomain(domain, range, value) {
    const dDiff = domain[1] - domain[0];
    const rDiff = range[1] - range[0];
    const dPos =  Math.round(dDiff/rDiff) * value;

    return domain[0] + dPos;
  }

used as:

    const xValue = this._getXCoordinateByDomain(
      this.props.xDomain,
      [0, this.props.innerWidth],
      this._getXCoordinateInContainer(e)
    );

The Initial approach Is mainly for time-series chart but I think it should work on any Series plot.

anselmo avatar Mar 20 '17 14:03 anselmo

Well assuming this is happening from within the context of the XYPlot, you probably want to generate a d3 scale using the scale/series utils, and then do an inverse on it, as that will work for all kinds of scales (and not just linear)

mcnuttandrew avatar Mar 20 '17 17:03 mcnuttandrew

@mcnuttandrew got it, I'll dive into it. really appreciate your help. Thanks

anselmo avatar Mar 20 '17 19:03 anselmo

also worth following this thread https://github.com/uber/react-vis/issues/202

mcnuttandrew avatar Mar 20 '17 19:03 mcnuttandrew

Hey @anselmo

Have you seen this? https://github.com/uber/react-vis/blob/master/examples/zoomable-chart/highlight.js It sounds like it should deal with your use case pretty directly. While the brush hasn't been completely productionalized (which is why it's not yet in the main library) the zoomable-chart example is a good place to start if you want brushing.

mcnuttandrew avatar Jun 16 '17 00:06 mcnuttandrew

Also I'm gonna mark this as closed, as #202 is chronicling this directly

mcnuttandrew avatar Jun 16 '17 00:06 mcnuttandrew