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

Support Drawing Rectangle in XY Plot

Open akindr opened this issue 10 years ago • 6 comments

I've got a feature request that I think several charts may want - adding an overlay that allows drawing a transparent rectangle on the chart, in order to accomplish enhanced chart features like zooming or selection.

I'm thinking a new component (e.g., DrawingArea) that only handles drawing a rectangle on the svg, triggering a callback when the user lifts the mouse with the rectangle dimensions (in the appropriate x / y units). There would have to be some standard behavior such as the rectangle disappearing after a few seconds, etc...

I think zooming is a larger can of worms, so I think this overlay would be purely for drawing. That would allow the user to define the follow-on behavior after drawing.

akindr avatar Nov 29 '16 19:11 akindr

react-vis' composable architecture allows you to draw arbitrary components, the API interface needs to be clarified (e.g. usage of requiresSVG class indicates that it's an element to be rendered within the SVG element), but it you can add any custom components, even custom series types.

chrisirhc avatar Nov 30 '16 06:11 chrisirhc

Ah interesting, I hadn't seen that in the docs. Can you give me a quick example of drawing an arbitrary component?

Sounds like it would be possible to do it without a supported react-vis component. However looking @ the docs and source, the XY Plot in particular doesn't expose enough mouse handlers to do the drawing. In particular, onmousedown and onmouseup would have to be exposed as props on the plot.

A few other questions - is the component rendered inside of the chart area (e.g., offset by the margins)? Can the component access the domain and range to determine where the rectangle falls on the scale?

akindr avatar Nov 30 '16 14:11 akindr

PRs welcome for any additional event handlers you need for XYPlot.

The custom component has access to props that the MarkSeries/LineSeries use to render the points. The API is designed in such a way so we can add new types of Series on the XYPlot.

We have plans to add such a use case into the documentation as a cookbook-style example.

Meanwhile, the GridLines component is an example of a non-series component that gets properties like marginLeft from XYPlot and renders using a transform. It also demonstrates how to get the scale attributes (getAttributeScale).

chrisirhc avatar Dec 01 '16 06:12 chrisirhc

Awesome, thanks. I wrote a custom component to handle it, which I hope to do a PR for soon. I'm hoping other people find it useful as well to add to their plots.

akindr avatar Dec 05 '16 18:12 akindr

I've opened a PR for adding mousedown handling + exporting AbstractSeries to make this easier.

akindr avatar Dec 08 '16 14:12 akindr

#197 handles the original ask here.

chrisirhc avatar Dec 10 '16 07:12 chrisirhc