fl_chart icon indicating copy to clipboard operation
fl_chart copied to clipboard

How to translate localPosition of TouchEvent to coordinates of the LineChart? (projection)

Open hmumixaM opened this issue 4 years ago • 6 comments

I am working on the LineChart, and I wish to allow users to drag points freely in the chart so that they can perform analysis on it. Analysis includes drawing a line based on 2 points of selection and calculate the length of the line.

The LineTouchResponse of touchCallback only returns coordinates of curves with given x-coordinate, but I wish the point can be dragged freely even outside the curve.

My current approach is to get the coordinate that user pointing at, and setState to draw a new point there. I am able to get localPosition (the pixel coordinate of the phone) from FlTouchEvent.localPosition, but I cannot draw a new point with the pixel coordinate. So is there a method to translate the localPosition to chart coordinates?

hmumixaM avatar Oct 26 '21 05:10 hmumixaM

A flexible solution, It's works for me

BoxConstraints provided by a LayoutBuilder wrapped outside the LineChart

Offset pixelToCoordinate(Offset offset, BoxConstraints constraints) {
    return Offset(
        minX + (maxX - minX) * (offset.dx - reservedSizeY) / (constraints.maxWidth - reservedSizeY),
        minY +
            (maxY - minY) *
                (constraints.maxHeight - offset.dy - reservedSizeX) /
                (constraints.maxHeight - reservedSizeX));
}

icefee avatar Oct 19 '23 08:10 icefee

We can provide a method to convert screen coordinates to chart coordinates and vice-versa. We have the function internally, but it is not exposed yet.

imaNNeo avatar Nov 11 '23 16:11 imaNNeo

Having an exposed interface that would allow us to convert a chart x, y coordinate to a local or global offset as well as the ability to convert a mouse x, y to a chart axis x,y would be super helpful. One other item that would be super handy is if the BarTouchData touchCallback for a stacked bar had the index of the rodStackItem chosen. That way we could provide tooltips and/or feedback based on the item below the mouse in the stacked bar rather than on just the bar itself. Found no way of doing that short of figuring out the y of the mose position and comparing that in the yFrom and yTo of the items. Thats how I landed on this thread. That also goes for FlSpot. KNowing if the mouse is directly over the spot (on a line chart) would allow us to better manage tooltips. Right now the tooltip get involked when the mouse is over the x coordinate (y is ignored)

JeffOlajos avatar Dec 01 '23 18:12 JeffOlajos

We can provide a method to convert screen coordinates to chart coordinates and vice-versa. We have the function internally, but it is not exposed yet.

This would be very helpful if you can expose that method for the next release.

TheOlajos avatar Dec 01 '23 18:12 TheOlajos

We can provide a method to convert screen coordinates to chart coordinates and vice-versa. We have the function internally, but it is not exposed yet.

When will this be released?

Ingenious-App-Studios avatar Feb 20 '24 19:02 Ingenious-App-Studios