(XY-Plot) Getting X,Y value from mouse event coordinates
Wonder if someone can point me in the right direction.
How can one get the (x, y) value from mouse event coordinates?
Hey @anselmo, can you say more about your use case?
@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.
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 got it, I'll dive into it. really appreciate your help. Thanks
also worth following this thread https://github.com/uber/react-vis/issues/202
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.
Also I'm gonna mark this as closed, as #202 is chronicling this directly