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

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

0
0

Dear all,

I have use the sample code below in order to be able to scroll with key stroke instead of mouse

public class SimpleZoomInOutModifier : ChartModifierBase

{
    public static readonly DependencyProperty ZoomFractionProperty
        = DependencyProperty.Register(“ZoomFraction”, typeof(double), typeof(SimpleZoomInOutModifier), new PropertyMetadata(0.1));
 
    public double ZoomFraction
    {
        get { return (double)GetValue(ZoomFractionProperty); }
        set { SetValue(ZoomFractionProperty, value); }
    }
 
    void SciChart_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        double factor = 0;
 
        if (e.Key == Key.Up)
        {
            // On CTRL+, Zoom In
            factor = -ZoomFraction;
        }
        if (e.Key == Key.Down)
        {
            // On CTRL-, Zoom Out
            factor = ZoomFraction;
        }
 
        using (ParentSurface.SuspendUpdates())
        {
            // Zoom the XAxis by the required factor
            XAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Zoom the YAxis by the required factor
            YAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Note.. can be extended for multiple YAxis XAxis, just iterate over all axes on the parent surface
        }
    }
 
    public override void OnAttached()
    {
        base.OnAttached();
        var scichart = ((SciChartSurface)ParentSurface);
 
        var mainWindow = FindLogicalParent(scichart);
 
        mainWindow.PreviewKeyDown -= SciChart_PreviewKeyDown;
        mainWindow.PreviewKeyDown += SciChart_PreviewKeyDown;
        mainWindow.Loaded += SciChart_Loaded;
    }
 
    private void SciChart_Loaded(object sender, RoutedEventArgs e)
    {
        double factor = 0;
        // On CTRL+, Zoom In
        factor = -ZoomFraction;
 
        using (ParentSurface.SuspendUpdates())
        {
            // Zoom the XAxis by the required factor
            XAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Zoom the YAxis by the required factor
            YAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Note.. can be extended for multiple YAxis XAxis, just iterate over all axes on the parent surface
        }
    }

The problem I have is that when my chart load and I press the key stroke for zooming in, it does not work.
TO make it work I need to zoom once with mouse and then I can use zoom in/out key after without problem.

Any idea how to solve that behavior ?

regards

Version
4
  • You must to post comments
0
0

My guess is that the ScIChartSurface is not focussed so the keyboard events are not being passed to the SciChartSurface.

You have to click on the chart to focus it.

Or, you can programmatically focus by calling ((MainGrid)SciChartSurface.RootGrid).Focus().

Finally, if you want to handle key events globally then you have to do this at a higher level in your app. WPF handles key events hierachically, so only the focused element and its children will receive key events.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.