SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Answered
2
0

Hi

Has anyone implemented or has any thoughts on how to implement keyboard shortcuts to zoom and pan? Something like key up/down/right/left to pan and shift+up/down/right/left to zoom?

  • You must to post comments
Best Answer
3
0

Hi there,

Keyboard input for ChartModifiers is a hotly requested feature. We haven’t implemented this yet, but you can achieve the same effect by a workaround.

Here is an extract from a related post titled keybindings for rollover functionality

Start by inheriting RolloverModifier and subscribing to the KeyDown event of the parent Window. E.g.

public class KeydownRolloverModifier : RolloverModifier
{
    private double positionFraction = 0.0;

    void KeydownRolloverModifier_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Right)
        {
            positionFraction += 0.01;
        }
        if (e.Key == Key.Left)
        {
            positionFraction -= 0.01;
        }

        // Constrain to 0.0, 1.0
        positionFraction = positionFraction > 1.0 ? 1.0 : positionFraction < 0.0 ? 0.0 : positionFraction;

        UpdateRollover(positionFraction);
    }

    public override void OnAttached()
    {
        base.OnAttached();
        var scichart = (ParentSurface as SciChartSurface);
        scichart.FindLogicalParent<Window>().PreviewKeyDown +=
            new KeyEventHandler(KeydownRolloverModifier_PreviewKeyDown);
    }

    public override void OnModifierMouseMove(ModifierMouseArgs e)
    {
        // Don't call base. This way the mouse does not change the cursor position
        // base.OnModifierMouseMove(e);
    }

    private void UpdateRollover(double fraction)
    {
        var point = new Point(ModifierSurface.Width * fraction, 0);
        point = ModifierSurface.TranslatePoint(point, RootGrid);
        base.OnModifierMouseMove(new ModifierMouseArgs(point, MouseButtons.None, MouseModifier.None, true));
    }
}

The code for FindLogicalParent is as follows:

public static class FrameworkElementExtensions
{
    public static T FindLogicalParent<T>(this FrameworkElement element) where T : FrameworkElement
    {
        var parent = (FrameworkElement)element.Parent;

        if (parent is T)
            return (T)parent;

        return parent.FindLogicalParent<T>();
    }
}

Using the above you should get a key right/left response by moving the rollover. It’ll move by a percentage each time, not to the next data-point. If you wish to achieve data-point by point moves, then maybe take a look at the HitTest API (BaseRenderableSeries.HitTest). This is used in the base RolloverModifier class to get the X,Y values of data-points.

Best regards,
Andrew

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.