New to Telerik UI for WinFormsStart a free 30-day trial

Restricting Zoom Interval in RadChartView

Updated over 6 months ago

Environment

Product Version2019.1.117
ProductRadChartView for WinForms

Description

An example demonstrating how zooming in the RadChartView control can be restricted to a predefined DateTime interval.

Solution

The example features a DateTimeContinuousAxis and suggests a solution how zooming can be restricted to an interval no less than ten minutes. The key in the suggested approach is to handle the ZoomChanging event and to cancel it when the time span between the first and last visible point in the view port is about to become less than the predefined interval, in this case - 10 minutes.

Figure 1: Restricting Zoom

radchartview-restrict-zoom-interval

A similar approach can be also used with other types of axes.

Implementation

C#
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();

        LineSeries lineSeries = new LineSeries();
        lineSeries.PointSize = new SizeF(10, 10);

        DateTime current = new DateTime(2018, 12, 21, 0, 0, 0);

        Random rand = new Random();
        for (int i = 0; i < 24; i++)
        {
            lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(1, 10), current.AddMinutes(i * 5)));
        }

        DateTimeContinuousAxis continuousAxis = new DateTimeContinuousAxis();
        continuousAxis.LabelInterval = 1;
        continuousAxis.MajorStepUnit = TimeInterval.Hour;
        continuousAxis.PlotMode = AxisPlotMode.OnTicksPadded;
        continuousAxis.LabelFormat = "{0:HH:mm}";

        lineSeries.HorizontalAxis = continuousAxis;
        this.radChartView1.Series.Add(lineSeries);

        this.radChartView1.ShowPanZoom = true;

        this.radChartView1.View.ZoomChanging += View_ZoomChanging;
    }

    private void View_ZoomChanging(object sender, ZoomChangingEventArgs e)
    {
        if (e.NewHorizontalScaleFactor < e.OldHorizontalScaleFactor)
        {
            return;
        }

        IChartView view = ((IChartView)this.radChartView1.View);
        foreach (var series in this.radChartView1.Series)
        {
            List<CategoricalDataPoint> visiblePoints = new List<CategoricalDataPoint>();
            foreach (var dp in series.DataPoints)
            {
                CategoricalDataPoint cdp = (CategoricalDataPoint)dp;
                if (this.CheckIfDataPointIsVisible(view, cdp))
                {
                    visiblePoints.Add(cdp);
                }
            }

            if (visiblePoints.Count >= 2)
            {
                TimeSpan span = (DateTime)visiblePoints.Last().Category - (DateTime)visiblePoints[0].Category;
                if (span.TotalMinutes <= 10)
                {
                    e.Cancel = true;
                }
            }
        }
    }

    private bool CheckIfDataPointIsVisible(IChartView view, CategoricalDataPoint cdp)
    {
        bool inXRange = cdp.LayoutSlot.X >= -view.PlotOriginX && cdp.LayoutSlot.X <= -view.PlotOriginX + view.ViewportWidth;
        bool inYRange = cdp.LayoutSlot.Y >= -view.PlotOriginY && cdp.LayoutSlot.Y <= -view.PlotOriginY + view.ViewportHeight;

        return inXRange && inYRange;
    }
}


See Also