Search code examples
horizontal-scrollingscichart

How to support sideways scrolling with trackpad / horizontal scroll wheel?


I've got a SciChartSurface where I support zooming as follows:

  • Zoom x-axis on regular scrolling.
  • Zoom y-axis on scrolling + CTRL
  • Pan x-axis on scrolling + Shift

I'd like to also enable panning in the X-direction when the user either scrolls horizontally on a trackpad or uses a horizontal scroll wheel (thumb-wheel). However, I'm not sure how to go about doing this.

Here's the extended MouseWheelZoomModifier I've been using. Can I somehow send it information about my scrolling behavior? Can I somehow just treat the sideways/horizontal scrolling as Shift + scroll? Thanks!

/// <summary>
/// Extended <see cref="MouseWheelZoomModifier"/> which modifies zoom
/// behavior based on modifier keys so that scrolling while holding CTRL 
/// zooms vertically and doing so while holding SHIFT pans horizontally.
/// </summary>
class MouseWheelZoomModifierEx : MouseWheelZoomModifier {
    public override void OnModifierMouseWheel(ModifierMouseArgs e) {
        switch (e.Modifier) {
            case MouseModifier.Ctrl:
                ActionType = ActionType.Zoom;
                XyDirection = XyDirection.YDirection;
                break;
            case MouseModifier.Shift:
                ActionType = ActionType.Pan;
                XyDirection = XyDirection.XDirection;
                break;
            default:
                ActionType = ActionType.Zoom;
                XyDirection = XyDirection.XDirection;
                break;
        }

        // e.Modifier is set to None so that the base implementation of 
        // OnModifierMouseWheel doesn't change ActionType and XyDirection again.
        e.Modifier = MouseModifier.None;
        base.OnModifierMouseWheel(e);
    }
}

Solution

  • In SciChart you can add any custom zoom and pan behaviors using the ChartModifierBase API.

    As well as the standard methods which you can override (like OnModifierMouseWheel, OnModifierMouseDown, OnModifierMouseUp) you can also subscribe to events directly on the ParentSurface.

    Have a look at this KB article: Custom ChartModifiers - Part 2 - Custom ZoomPanModifier and Zooming on KeyPress.

    Up to date accompanying source code is here.

    So my suggestion is take the SimpleZoomInOutModifier and modify it to respond to mouse wheel events instead of key events.

    Does that help?