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

0
0

Dear all,

I am trying to load my chart with a default zoom factor value. For that I have follow and modify the exemple shown by using key stroke.

My modifiier class is defined as below :

 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<UserControl>(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
        }
    }

From the code above, the zoom in by using Up and Down keys is working perfectly well but when I try to force the zoom in inside the chart_loaded event, nothing happen.

Any idea what I am doing wrong ?

regards

Version
4
  • You must to post comments
0
0

Hi there,

I think this happens because the SciChartSurface has already been loaded, so the loaded event is not fired any more. In fact, the OnAttached() method is called on a modifier in a Loaded handler of SciChartSurface. So actually you can put all the code from SciChart_Loaded into the OnAttached() and it should work just fine.

Another viable option is overriding the OnParentSurfaceRendered(..) method and executing your initialization code there only for the first time the method is called.

Also make sure that you have AutoRange set to “Never” on axes, otherwise your axes configuration will be overridden later by AutoRange:
https://www.scichart.com/documentation/v4.x/webframe.html#Axis%20Ranging%20-%20AutoRange%20and%20VisibleRange.html

Best regards,
Yuriy

  • sc sc
    thnanks will give a try. What is the ratio unit of the zoomBy factor ?For exemple if I have A visible rqnge from 0 to 1000 which correspond to the Min and Max in my series, I need to set the zoom in order that range is from 200 to 800. How to get the corresponding zoom factor for that range ?
  • Yuriy
    It depends on axis type. For most cases, it is (factor * range_diff), so if you want to make the range smaller, your factors have to be negative. For instance, in your example factors should be (-0.2) to result in the range of (200, 800) from (0, 1000).
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.