DEV Community

Darkside
Darkside

Posted on

How to obtain the relative position of brush in the coordinate system in the vchart chart library?

Question title

How to obtain the relative position of brush in the coordinate system in the vchart chart library?

Problem description

When I was using the vchart library, I encountered a problem. I listened to the onBrushEnd event and wanted to obtain the relative position of the area selected by the brush in the coordinate system. That is, after I drew a frame on the chart, I wanted to obtain the position range of this area on the x/y axis. I tried all the methods I could think of, but did not find the corresponding data.

Image description

Solution

According to the design and usage of vchart, currently the vchart API does not directly provide this information, but it is still possible to obtain this necessary information through some other means. The specific steps are as follows:

  1. Use vchart.getAllComponents () to find the brush component

Image description

  1. In the brush component, there is a property called _brushComponent. AABBBounds , which is the relative position of the checkbox in the region.

Image description

  1. Similarly, with the vchart.getAllComponents () method, we can find the components for the x and y axes.

Image description

  1. In the found axis component, we can use the _scale .invert (position) method to convert the position of AABBBounds obtained in the previous step to specific values on the x and y axes.

In order to prevent a very small position shift when the user clicks and accidentally triggers the brush drawing, we can also set a sizeThreshold property for the brush. This property means the checkbox size threshold, which literally means the minimum size of the box you need to draw. The brush event will only be triggered when the size of the box is greater than this threshold.

Related Documents

Top comments (0)