Data Zoom And Pan Tools Example for WPF

Nevron NOV Chart for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF Chart examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Data Zoom And Pan Tools Example for WPF Description

This example demonstrates how to implement zooming and scrolling. Press the left mouse button over the chart and select an area.

You can also use the mouse wheel to zoom in and out. When "Wheel Zoom at Mouse" is checked the control will zoom in out relative to the current mouse position in the chart plot.


Data Zoom and Pan Tools Example

C# Data Zoom And Pan Tools Example Code for Nevron Chart for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Chart.Tools;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Net.Http.Headers;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NDataZoomAndPanToolsExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NChartViewWithCommandBars chartViewWithCommandBars = new NChartViewWithCommandBars();
			NChartView chartView = chartViewWithCommandBars.View;
			chartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);

			// configure title
			chartView.Surface.Titles[0].Text = "Rectangle Zoom Tool";

			// configure chart
			m_Chart = (NCartesianChart)chartView.Surface.Charts[0];

			m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear);

			// setup X axis
			NLinearScale scaleX = new NLinearScale();
			scaleX.MajorGridLines = new NScaleGridLines();
			scaleX.MajorGridLines.Stroke.DashStyle = ENDashStyle.Dot;
			m_Chart.Axes[ENCartesianAxis.PrimaryX].Scale = scaleX;

			// setup Y axis
			NLinearScale scaleY = new NLinearScale();
			scaleY.MajorGridLines = new NScaleGridLines();
			scaleY.MajorGridLines.Stroke.DashStyle = ENDashStyle.Dot;

            // add interlaced stripe for Y axis
            NScaleStrip strip = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
            strip.Interlaced = true;
			scaleY.Strips.Add(strip);

			m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale = scaleY;

			// add some random data
			for (int i = 0; i < 4; i++)
			{
				AddPointCloud();
			}

            m_Chart.Enabled = true;
			NInteractor interactor = new NInteractor();

			m_RectangleZoomTool = new NRectangleZoomTool();
			interactor.Add(m_RectangleZoomTool);

			NDataPanTool dataPanTool = new NDataPanTool();
			dataPanTool.StartMouseButtonEvent = ENMouseButtonEvent.RightButtonDown;
			dataPanTool.EndMouseButtonEvent = ENMouseButtonEvent.RightButtonUp;
			interactor.Add(dataPanTool);

			m_Chart.Interactor = interactor;

			chartView.Document.StyleSheets.ApplyTheme(new NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series));

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			NUniSizeBoxGroup group = new NUniSizeBoxGroup(stack);
			
			NComboBox orientationComboBox = new NComboBox();
			orientationComboBox.Items.FillFromEnum();
			orientationComboBox.SelectedIndex = (int)m_Chart.Orientation;
			orientationComboBox.SelectedIndexChanged += OnOrientationComboBoxSelectedIndexChanged;
			stack.Add(NPairBox.Create("Orientation:", orientationComboBox));

			NCheckBox snapToMajorTicksCheckBox = new NCheckBox("Snap To Major Ticks");
			snapToMajorTicksCheckBox.CheckedChanged += OnSnapToMajorTicksCheckBoxCheckedChanged;
			stack.Add(snapToMajorTicksCheckBox);

			NCheckBox invertScaleCheckBox = new NCheckBox("Invert Scale");
			invertScaleCheckBox.CheckedChanged += OnInvertScaleCheckBoxCheckedChanged;
			stack.Add(invertScaleCheckBox);

			NCheckBox showScrollbarsWhenZoomedCheckBox = new NCheckBox("Show Scrollbars When Zoomed");
			showScrollbarsWhenZoomedCheckBox.Checked = true;
			showScrollbarsWhenZoomedCheckBox.CheckedChanged += OnShowScrollbarsWhenZoomedCheckBoxCheckedChanged;
			stack.Add(showScrollbarsWhenZoomedCheckBox);

            NCheckBox wheelZoomAtMouseCheckBox = new NCheckBox("Wheel Zoom at Mouse Position");
            wheelZoomAtMouseCheckBox.Checked = true;
            wheelZoomAtMouseCheckBox.CheckedChanged += OnWheelZoomAtMouseCheckBoxCheckedChanged;
            stack.Add(wheelZoomAtMouseCheckBox);

            return group;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to implement zooming and scrolling. Press the left mouse button over the chart and select an area.

You can also use the mouse wheel to zoom in and out. When ""Wheel Zoom at Mouse"" is checked the control will zoom in out relative to the current mouse position in the chart plot.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Zooming and Scrolling", "https://www.nevron.com/docs/openvision/chart-interactivity-zooming-and-scrolling.html"), ("Common Axis Functionality", "https://www.nevron.com/docs/openvision/chart-common-axis-functionality.html"), ("Cartesian Axes", "https://www.nevron.com/docs/openvision/cartesian-chart-axes.html"), }; } #endregion #region Implementation /// /// Adds a point cloud to the chart /// private void AddPointCloud() { // add a point series NPointSeries point = new NPointSeries(); point.Name = "Point Series"; point.DataLabelStyle = new NDataLabelStyle(false); point.Size = 5; point.Shape = ENPointShape3D.Bar; point.UseXValues = true; m_Chart.Series.Add(point); // add some sample data NDataPointCollection dataPoints = point.DataPoints; Random random = new Random(); double xBase = random.Next(10); double yBase = random.Next(10); double scale = Math.Min(2.5, Math.Max(1.5, random.NextDouble() * 2)); for (int i = 0; i < 1000; i++) { double u1 = random.NextDouble(); double u2 = random.NextDouble(); if (u1 == 0) u1 += 0.0001; if (u2 == 0) u2 += 0.0001; double x = Math.Sqrt(-2 * Math.Log(u1)) * Math.Cos(2 * Math.PI * u2) / scale + xBase; double y = Math.Sqrt(-2 * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2) / scale + yBase; dataPoints.Add(new NPointDataPoint(x, y)); } } #endregion #region Event Handlers void OnSnapToMajorTicksCheckBoxCheckedChanged(NValueChangeEventArgs arg) { if (((NCheckBox)arg.TargetNode).Checked) { m_RectangleZoomTool.HorizontalValueSnapper = new NAxisMajorTickSnapper(); m_RectangleZoomTool.VerticalValueSnapper = new NAxisMajorTickSnapper(); } else { m_RectangleZoomTool.HorizontalValueSnapper = null; m_RectangleZoomTool.VerticalValueSnapper = null; } } void OnInvertScaleCheckBoxCheckedChanged(NValueChangeEventArgs arg) { for (int i = 0; i < m_Chart.Axes.Count; i++) { m_Chart.Axes[i].Scale.Invert = ((NCheckBox)arg.TargetNode).Checked; } } void OnOrientationComboBoxSelectedIndexChanged(NValueChangeEventArgs arg) { m_Chart.Orientation = (ENCartesianChartOrientation)((NComboBox)arg.TargetNode).SelectedIndex; } void OnShowScrollbarsWhenZoomedCheckBoxCheckedChanged(NValueChangeEventArgs arg) { bool showScrollbars = ((NCheckBox)arg.TargetNode).Checked; m_Chart.Axes[ENCartesianAxis.PrimaryX].ShowScrollbarWhenZoomed = showScrollbars; m_Chart.Axes[ENCartesianAxis.PrimaryY].ShowScrollbarWhenZoomed = showScrollbars; } void OnWheelZoomAtMouseCheckBoxCheckedChanged(NValueChangeEventArgs arg) { bool wheelZoomAtMouse = ((NCheckBox)arg.TargetNode).Checked; m_RectangleZoomTool.WheelZoomAtMouse = wheelZoomAtMouse; } #endregion #region Fields NCartesianChart m_Chart; NRectangleZoomTool m_RectangleZoomTool; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Data Zoom And Pan Tools Example for WPF Solution

VB.NET Data Zoom And Pan Tools Example Code for Nevron Chart for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.Chart.Tools
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NDataZoomAndPanToolsExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim chartViewWithCommandBars As NChartViewWithCommandBars = New NChartViewWithCommandBars()
            Dim chartView = chartViewWithCommandBars.View
            chartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian)

            ' configure title
            chartView.Surface.Titles(0).Text = "Rectangle Zoom Tool"

            ' configure chart
            m_Chart = CType(chartView.Surface.Charts(0), NCartesianChart)

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear)

            ' setup X axis
            Dim scaleX As NLinearScale = New NLinearScale()
            scaleX.MajorGridLines = New NScaleGridLines()
            scaleX.MajorGridLines.Stroke.DashStyle = ENDashStyle.Dot
            m_Chart.Axes(ENCartesianAxis.PrimaryX).Scale = scaleX

            ' setup Y axis
            Dim scaleY As NLinearScale = New NLinearScale()
            scaleY.MajorGridLines = New NScaleGridLines()
            scaleY.MajorGridLines.Stroke.DashStyle = ENDashStyle.Dot

            ' add interlaced stripe for Y axis
            Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(NColor.Beige), Nothing, True, 0, 0, 1, 1)
            strip.Interlaced = True
            scaleY.Strips.Add(strip)

            m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale = scaleY

            ' add some random data
            For i = 0 To 3
                AddPointCloud()
            Next

            m_Chart.Enabled = True
            Dim interactor As NInteractor = New NInteractor()

            m_RectangleZoomTool = New NRectangleZoomTool()
            interactor.Add(m_RectangleZoomTool)

            Dim dataPanTool As NDataPanTool = New NDataPanTool()
            dataPanTool.StartMouseButtonEvent = ENMouseButtonEvent.RightButtonDown
            dataPanTool.EndMouseButtonEvent = ENMouseButtonEvent.RightButtonUp
            interactor.Add(dataPanTool)

            m_Chart.Interactor = interactor

            chartView.Document.StyleSheets.ApplyTheme(New NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series))

            Return chartViewWithCommandBars
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            Dim group As NUniSizeBoxGroup = New NUniSizeBoxGroup(stack)

            Dim orientationComboBox As NComboBox = New NComboBox()
            orientationComboBox.Items.FillFromEnum(Of ENCartesianChartOrientation)()
            orientationComboBox.SelectedIndex = m_Chart.Orientation
            orientationComboBox.SelectedIndexChanged += AddressOf OnOrientationComboBoxSelectedIndexChanged
            stack.Add(NPairBox.Create("Orientation:", orientationComboBox))

            Dim snapToMajorTicksCheckBox As NCheckBox = New NCheckBox("Snap To Major Ticks")
            snapToMajorTicksCheckBox.CheckedChanged += AddressOf OnSnapToMajorTicksCheckBoxCheckedChanged
            stack.Add(snapToMajorTicksCheckBox)

            Dim invertScaleCheckBox As NCheckBox = New NCheckBox("Invert Scale")
            invertScaleCheckBox.CheckedChanged += AddressOf OnInvertScaleCheckBoxCheckedChanged
            stack.Add(invertScaleCheckBox)

            Dim showScrollbarsWhenZoomedCheckBox As NCheckBox = New NCheckBox("Show Scrollbars When Zoomed")
            showScrollbarsWhenZoomedCheckBox.Checked = True
            showScrollbarsWhenZoomedCheckBox.CheckedChanged += AddressOf OnShowScrollbarsWhenZoomedCheckBoxCheckedChanged
            stack.Add(showScrollbarsWhenZoomedCheckBox)

            Dim wheelZoomAtMouseCheckBox As NCheckBox = New NCheckBox("Wheel Zoom at Mouse Position")
            wheelZoomAtMouseCheckBox.Checked = True
            wheelZoomAtMouseCheckBox.CheckedChanged += AddressOf OnWheelZoomAtMouseCheckBoxCheckedChanged
            stack.Add(wheelZoomAtMouseCheckBox)

            Return group
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to implement zooming and scrolling. Press the left mouse button over the chart and select an area.

You can also use the mouse wheel to zoom in and out. When ""Wheel Zoom at Mouse"" is checked the control will zoom in out relative to the current mouse position in the chart plot.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Zooming and Scrolling", "https://www.nevron.com/docs/openvision/chart-interactivity-zooming-and-scrolling.html"), ("Common Axis Functionality", "https://www.nevron.com/docs/openvision/chart-common-axis-functionality.html"), ("Cartesian Axes", "https://www.nevron.com/docs/openvision/cartesian-chart-axes.html")} End Function #End Region #Region "Implementation" ''' ''' Adds a point cloud to the chart ''' Private Sub AddPointCloud() ' add a point series Dim point As NPointSeries = New NPointSeries() point.Name = "Point Series" point.DataLabelStyle = New NDataLabelStyle(False) point.Size = 5 point.Shape = ENPointShape3D.Bar point.UseXValues = True m_Chart.Series.Add(point) ' add some sample data Dim dataPoints = point.DataPoints Dim random As Random = New Random() Dim xBase As Double = random.Next(10) Dim yBase As Double = random.Next(10) Dim scale As Double = Math.Min(2.5, Math.Max(1.5, random.NextDouble() * 2)) For i = 0 To 999 Dim u1 As Double = random.NextDouble() Dim u2 As Double = random.NextDouble() If u1 = 0 Then u1 += 0.0001 If u2 = 0 Then u2 += 0.0001 Dim x = Math.Sqrt(-2 * Math.Log(u1)) * Math.Cos(2 * Math.PI * u2) / scale + xBase Dim y = Math.Sqrt(-2 * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2) / scale + yBase dataPoints.Add(New NPointDataPoint(x, y)) Next End Sub #End Region #Region "Event Handlers" Private Sub OnSnapToMajorTicksCheckBoxCheckedChanged(arg As NValueChangeEventArgs) If CType(arg.TargetNode, NCheckBox).Checked Then m_RectangleZoomTool.HorizontalValueSnapper = New NAxisMajorTickSnapper() m_RectangleZoomTool.VerticalValueSnapper = New NAxisMajorTickSnapper() Else m_RectangleZoomTool.HorizontalValueSnapper = Nothing m_RectangleZoomTool.VerticalValueSnapper = Nothing End If End Sub Private Sub OnInvertScaleCheckBoxCheckedChanged(arg As NValueChangeEventArgs) For i = 0 To m_Chart.Axes.Count - 1 m_Chart.Axes(i).Scale.Invert = CType(arg.TargetNode, NCheckBox).Checked Next End Sub Private Sub OnOrientationComboBoxSelectedIndexChanged(arg As NValueChangeEventArgs) m_Chart.Orientation = CType(CType(arg.TargetNode, NComboBox).SelectedIndex, ENCartesianChartOrientation) End Sub Private Sub OnShowScrollbarsWhenZoomedCheckBoxCheckedChanged(arg As NValueChangeEventArgs) Dim showScrollbars = CType(arg.TargetNode, NCheckBox).Checked m_Chart.Axes(ENCartesianAxis.PrimaryX).ShowScrollbarWhenZoomed = showScrollbars m_Chart.Axes(ENCartesianAxis.PrimaryY).ShowScrollbarWhenZoomed = showScrollbars End Sub Private Sub OnWheelZoomAtMouseCheckBoxCheckedChanged(arg As NValueChangeEventArgs) Dim wheelZoomAtMouse = CType(arg.TargetNode, NCheckBox).Checked m_RectangleZoomTool.WheelZoomAtMouse = wheelZoomAtMouse End Sub #End Region #Region "Fields" Private m_Chart As NCartesianChart Private m_RectangleZoomTool As NRectangleZoomTool #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Data Zoom And Pan Tools Example for WPF Solution