graphic icon indicating copy to clipboard operation
graphic copied to clipboard

Keep the crosshair on range update

Open denysb-hb opened this issue 5 months ago • 6 comments

I have this code on my chart, tapping on mark works but as soon as I pan scroll chart, the "selection" is removed.

crosshair: CrosshairGuide(
  followPointer: [false, true],
  mark: 0,
  layer: -1,
  styles: [PaintStyle(strokeColor: Colors.black, strokeWidth: 2), null],
  selections: {'tap'},
),
selections: {
  'pan': IntervalSelection(
    devices: {PointerDeviceKind.touch, PointerDeviceKind.mouse},
    clear: {},
  ),
  'tap': PointSelection(
    dim: Dim.x,
    on: {GestureType.tap},
    clear: {GestureType.doubleTap},
    variable: 'day'
  ),
},

denysb-hb avatar Jul 30 '25 08:07 denysb-hb

Thanks for reporting this issue. I understand that the tap selection is being cleared when you pan the chart, even though you haven't explicitly configured the pan gesture to clear the tap selection.

This behavior occurs because when you have multiple selections defined, they can interfere with each other. In your case, the pan IntervalSelection is being activated during pan gestures, which may be triggering the selection system to clear other selections.

Here's a solution that should preserve the tap selection during pan operations:

selections: {
  'pan': IntervalSelection(
    devices: {PointerDeviceKind.touch, PointerDeviceKind.mouse},
    clear: {},
    // Add this to prevent clearing other selections
    merge: SelectionUpdateType.merge,
  ),
  'tap': PointSelection(
    dim: Dim.x,
    on: {GestureType.tap},
    clear: {GestureType.doubleTap},
    variable: 'day',
    // Ensure tap selection persists
    merge: SelectionUpdateType.merge,
  ),
},

Alternatively, you could modify the pan selection to be a different type that doesn't interfere with the point selection:

selections: {
  'tap': PointSelection(
    dim: Dim.x,
    on: {GestureType.tap},
    clear: {GestureType.doubleTap},
    variable: 'day',
  ),
},
// Handle pan gestures separately without selection
chartController: ChartController(
  gestureType: GestureType.pan,
  // Custom pan handling logic here
),

The key is to ensure that the pan gesture doesn't trigger selection clearing logic that affects the tap selection. The merge property in SelectionUpdateType can help maintain existing selections when new ones are created.

Note: This solution was generated with AI assistance. Please let me know if this approach works for your use case or if you need further clarification.

entronad avatar Aug 05 '25 07:08 entronad

Neither of those solution is present in 2.6.0 (Latest available version)

denysb-hb avatar Aug 05 '25 10:08 denysb-hb

@entronad Any ideas?

denysb-hb avatar Aug 06 '25 12:08 denysb-hb

Thank you for reporting this issue. After reviewing the code, I can see why the tap selection is being cleared when you pan the chart.

The issue occurs because IntervalSelection automatically responds to pan gestures (GestureType.scaleUpdate), and when a new selection is triggered, it replaces the existing selectors. In your configuration, when you pan the chart, the 'pan' IntervalSelection is activated, which causes the 'tap' PointSelection to be cleared.

Here's a solution that should maintain the crosshair on the tapped point even when panning:

crosshair: CrosshairGuide(
  followPointer: [false, true],
  mark: 0,
  layer: -1,
  styles: [PaintStyle(strokeColor: Colors.black, strokeWidth: 2), null],
  selections: {'tap'},  // Only react to tap selection
),
selections: {
  'tap': PointSelection(
    dim: Dim.x,
    on: {GestureType.tap},
    clear: {GestureType.doubleTap},
    variable: 'day'
  ),
  // Remove the pan IntervalSelection if you don't need the interval selection functionality
  // Or keep it but understand that it will trigger during pan
},

If you need both the tap selection for the crosshair AND interval selection for range selection, you might need to handle them differently. The current architecture makes the IntervalSelection respond to pan gestures by design, which can interfere with maintaining the tap selection.

Alternatively, if you want to keep the chart pannable while maintaining the crosshair position, you could consider:

  1. Using only the PointSelection and removing the IntervalSelection
  2. Implementing a custom gesture handler that preserves the tap selection state during panning

This solution was generated with AI assistance. Please let me know if this resolves your issue or if you need a different approach to handle both selections simultaneously.

entronad avatar Aug 11 '25 07:08 entronad

@denysb-hb Try this new answer, the former has illusions, I have changed the prompt.

entronad avatar Aug 11 '25 07:08 entronad

@entronad Regarding this Implementing a custom gesture handler that preserves the tap selection state during panning is it even possible to pass your own gesture handlers and add them to chart? If yes I would like some pointers

denysb-hb avatar Aug 11 '25 10:08 denysb-hb