Trendline 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.

Trendline Example for WPF Description

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points. In this example, the trendline series is used to display different fittings like linear, exponential, logarithmic, etc. of a source data points set.


Trendline Example

C# Trendline 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.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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 = "Trendline Series";
            chartView.Surface.Legends[0].Visibility = ENVisibility.Collapsed;

            // configure chart
            m_Chart = (NCartesianChart)chartView.Surface.Charts[0];
            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.Standard);
            m_Chart.Interactor = new NInteractor(new NTrackballTool());

            // switch X axis in linear scale
            m_Chart.Axes[ENCartesianAxis.PrimaryX].Scale = new NLinearScale();

            // add interlace stripe
            NLinearScale linearScale = m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale as NLinearScale;
            NScaleStrip strip = new NScaleStrip(new NColorFill(ENNamedColor.Beige), null, true, 0, 0, 1, 1);
            strip.Interlaced = true;
            linearScale.Strips.Add(strip);

            // setup line series to show trenline
            m_Trendline = new NTrendlineSeries();
            m_Trendline.Name = "Trendline Series";
            m_Trendline.Stroke = new NStroke(3, LineColor);
            m_Chart.Series.Add(m_Trendline);

            m_Trendline.LegendView.Mode = ENSeriesLegendMode.SeriesLogic;

            // setup point series to show XY scatter
            m_PointSeries = new NPointSeries();
            m_PointSeries.Name = "Point Series";
            m_PointSeries.UseXValues = true;
            m_PointSeries.Fill = new NColorFill(BarColor);
            m_Chart.Series.Add(m_PointSeries);

            return chartViewWithCommandBars;
        }
        public NWidget CreateExampleControls()
        {
            NStackPanel stack = new NStackPanel();
            NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);

            NNumericUpDown movingAveragePeriodNumericUpDown = new NNumericUpDown();
            NNumericUpDown polynomialOrderNumericUpDown = new NNumericUpDown();
            m_PolynomialOrderNumericUpDown = polynomialOrderNumericUpDown;
            m_MovingAveragePeriodNumericUpDown = movingAveragePeriodNumericUpDown;

            NComboBox trendlineTypeComboBox = new NComboBox();
            trendlineTypeComboBox.Items.FillFromEnum();
            trendlineTypeComboBox.SelectedIndexChanged += OnTrendlineTypeComboBoxSelectedIndexChanged;
            trendlineTypeComboBox.SelectedIndex = (int)m_Trendline.TrendlineType;
            stack.Add(NPairBox.Create("Trendline Type:", trendlineTypeComboBox));
            
            movingAveragePeriodNumericUpDown.Minimum = 1;
            movingAveragePeriodNumericUpDown.Maximum = 30;
            movingAveragePeriodNumericUpDown.Value = m_Trendline.MovingAveragePeriod;
            movingAveragePeriodNumericUpDown.ValueChanged += OnMovingAveragePeriodNumericUpDownValueChanged;
            stack.Add(NPairBox.Create("Moving Average Period:", movingAveragePeriodNumericUpDown));
            
            polynomialOrderNumericUpDown.Minimum = 1;
            polynomialOrderNumericUpDown.Maximum = 100;
            polynomialOrderNumericUpDown.Value = m_Trendline.PolynomialOrder;
            polynomialOrderNumericUpDown.ValueChanged += OnPolynomialOrderNumericUpDownValueChanged;
            stack.Add(NPairBox.Create("Polynomial Order:", polynomialOrderNumericUpDown));

            // new positive data button
            NButton newDataButton = new NButton("New Data");
            newDataButton.Click += (e) =>
            {
                // fill with random data
                RandomData();
            };
            stack.Add(newDataButton);

            // fill with random data
            RandomData();

            return boxGroup;
        }

        public string GetExampleDescription()
        {
            return @"

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points. In this example, the trendline series is used to display different fittings like linear, exponential, logarithmic, etc. of a source data points set.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Trendline Series", "https://www.nevron.com/docs/openvision/trendline-chart-series.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), }; } #endregion #region Implementation private void RandomData() { Random rand = new Random(); int count = 100; double[] randomDataX = new double[count]; double[] randomDataY = new double[count]; double x; for (int i = 0; i < count; i++) { x = i + rand.NextDouble(); randomDataX[i] = x * 10; randomDataY[i] = x * 5.0 * rand.NextDouble() + rand.NextDouble(); } m_PointSeries.DataPoints.Clear(); for (int i = 0; i < randomDataX.Length; i++) { m_PointSeries.DataPoints.Add(new NPointDataPoint(randomDataX[i], randomDataY[i])); } CalculateStatistics(); } void CalculateStatistics() { // show trendline m_Trendline.SourceSeries = m_PointSeries; } private NColor BarColor { get { return NChartPalette.FreshPalette[0]; } } private NColor LineColor { get { return NChartPalette.FreshPalette[1]; } } #endregion #region Event Handlers #endregion #region Fields NPointSeries m_PointSeries; NCartesianChart m_Chart; NTrendlineSeries m_Trendline; NNumericUpDown m_PolynomialOrderNumericUpDown; NNumericUpDown m_MovingAveragePeriodNumericUpDown; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Trendline Example for WPF Solution

VB.NET Trendline 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 - NTrendlineExample"

		' 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 = "Trendline Series"
            chartView.Surface.Legends(0).Visibility = ENVisibility.Collapsed

            ' configure chart
            m_Chart = CType(chartView.Surface.Charts(0), NCartesianChart)
            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.Standard)
            m_Chart.Interactor = New NInteractor(New NTrackballTool())

            ' switch X axis in linear scale
            m_Chart.Axes(ENCartesianAxis.PrimaryX).Scale = New NLinearScale()

            ' add interlace stripe
            Dim linearScale As NLinearScale = TryCast(m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale, NLinearScale)
            Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(ENNamedColor.Beige), Nothing, True, 0, 0, 1, 1)
            strip.Interlaced = True
            linearScale.Strips.Add(strip)

            ' setup line series to show trenline
            m_Trendline = New NTrendlineSeries()
            m_Trendline.Name = "Trendline Series"
            m_Trendline.Stroke = New NStroke(3, LineColor)
            m_Chart.Series.Add(m_Trendline)

            m_Trendline.LegendView.Mode = ENSeriesLegendMode.SeriesLogic

            ' setup point series to show XY scatter
            m_PointSeries = New NPointSeries()
            m_PointSeries.Name = "Point Series"
            m_PointSeries.UseXValues = True
            m_PointSeries.Fill = New NColorFill(BarColor)
            m_Chart.Series.Add(m_PointSeries)

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

            Dim movingAveragePeriodNumericUpDown As NNumericUpDown = New NNumericUpDown()
            Dim polynomialOrderNumericUpDown As NNumericUpDown = New NNumericUpDown()
            m_PolynomialOrderNumericUpDown = polynomialOrderNumericUpDown
            m_MovingAveragePeriodNumericUpDown = movingAveragePeriodNumericUpDown

            Dim trendlineTypeComboBox As NComboBox = New NComboBox()
            trendlineTypeComboBox.Items.FillFromEnum(Of ENTrendlineType)()
            trendlineTypeComboBox.SelectedIndexChanged += AddressOf OnTrendlineTypeComboBoxSelectedIndexChanged
            trendlineTypeComboBox.SelectedIndex = m_Trendline.TrendlineType
            stack.Add(NPairBox.Create("Trendline Type:", trendlineTypeComboBox))

            movingAveragePeriodNumericUpDown.Minimum = 1
            movingAveragePeriodNumericUpDown.Maximum = 30
            movingAveragePeriodNumericUpDown.Value = m_Trendline.MovingAveragePeriod
            movingAveragePeriodNumericUpDown.ValueChanged += AddressOf OnMovingAveragePeriodNumericUpDownValueChanged
            stack.Add(NPairBox.Create("Moving Average Period:", movingAveragePeriodNumericUpDown))

            polynomialOrderNumericUpDown.Minimum = 1
            polynomialOrderNumericUpDown.Maximum = 100
            polynomialOrderNumericUpDown.Value = m_Trendline.PolynomialOrder
            polynomialOrderNumericUpDown.ValueChanged += AddressOf OnPolynomialOrderNumericUpDownValueChanged
            stack.Add(NPairBox.Create("Polynomial Order:", polynomialOrderNumericUpDown))

            ' new positive data button
            Dim newDataButton As NButton = New NButton("New Data")
            ' fill with random data
            newDataButton.Click += Sub(e) RandomData()
            stack.Add(newDataButton)

            ' fill with random data
            RandomData()

            Return boxGroup
        End Function

        Public Function GetExampleDescription() As String
            Return "

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points. In this example, the trendline series is used to display different fittings like linear, exponential, logarithmic, etc. of a source data points set.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Trendline Series", "https://www.nevron.com/docs/openvision/trendline-chart-series.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html")} End Function #End Region #Region "Implementation" Private Sub RandomData() Dim rand As Random = New Random() Dim count = 100 Dim randomDataX = New Double(count - 1) {} Dim randomDataY = New Double(count - 1) {} Dim x As Double For i = 0 To count - 1 x = i + rand.NextDouble() randomDataX(i) = x * 10 randomDataY(i) = x * 5.0 * rand.NextDouble() + rand.NextDouble() Next m_PointSeries.DataPoints.Clear() For i = 0 To randomDataX.Length - 1 m_PointSeries.DataPoints.Add(New NPointDataPoint(randomDataX(i), randomDataY(i))) Next CalculateStatistics() End Sub Private Sub CalculateStatistics() ' show trendline m_Trendline.SourceSeries = m_PointSeries End Sub Private ReadOnly Property BarColor As NColor Get Return NChartPalette.FreshPalette(0) End Get End Property Private ReadOnly Property LineColor As NColor Get Return NChartPalette.FreshPalette(1) End Get End Property #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_PointSeries As NPointSeries Private m_Chart As NCartesianChart Private m_Trendline As NTrendlineSeries Private m_PolynomialOrderNumericUpDown As NNumericUpDown Private m_MovingAveragePeriodNumericUpDown As NNumericUpDown #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) Trendline Example for WPF Solution