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

0
0

How to move chart scroll by arrow key? For example, When I pressed the Right Arrow Key, the bars in the chart will move Right one Bar position, vice versa. Thanks.

  • You must to post comments
0
0

Hello and thanks for your enquiry!

We had a similar request a while back asking for key bindings to move the RolloverModifier line one step to the left or right. Please take a look here.

If you were to create a similar CustomModifier except you updated the XAxis.VisibleRange you could achieve scrolling, for instance:

public class ScrollWithArrowKeysModifier : ChartModifierBase
{
    void KeydownRolloverModifier_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        int pointsToScroll;
        if (e.Key == Key.Right)
        {
            pointsToScroll = 1;
        }
        if (e.Key == Key.Left)
        {
            pointsToScroll = -1;
        }
 
        // This code assumes you are using a CategoryDateTimeAxis. If not then you will need to adjust it
        var inputRange = ParentSurface.XAxis.VisibleRange as IndexRange;
        var outputRange = new IndexRange(inputRange.Min + pointsToScroll, inputRange.Max + pointsToScroll);
        ParentSurface.XAxis.VisibleRange = outputRange;
    }
 
    public override void OnAttached()
    {
        base.OnAttached();
        var scichart = (ParentSurface as SciChartSurface);
        scichart.FindLogicalParent<Window>().PreviewKeyDown +=
            new KeyEventHandler(KeydownRolloverModifier_PreviewKeyDown);
    }

    public override void OnDetached()
    {
        // Todo: You might want to unsubscribe to PreviewKeyDown here
    }
}

The above should work – let me know if it helps!

Also note in SciChart 2 we have introduced an additional API, which allows you to get a helper class off an axis to perform VisibleRange manipulations.

Try this code out:

IAxisInteractivityHelper axisHelper = ParentSurface.XAxis.GetCurrentInteractivityHelper();
IRange outputRange = axisHelper.ScrollBy(inputRange, pointsToScroll);
ParentSurface.XAxis.VisibleRange = outputRange;

I hope this helps!

Andrew

  • rcannon
    Questions: So I'm attempting to do something similar to the original post, but I want to scroll a percentage of the screen size based on a DateTime x axis in V2. The math for that is logically easy enough, but if I'm to use the first method then scichart.FindLogicalParent().PreviewKeyDown no longer exists as an available function, apparently. As for the second method, I'm not entirely sure how to use it. Suggestions? Thanks so much
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.