Line Study 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.

Line Study Example for WPF Description

This example demonstrates how to create a stock chart and overlay a line study on it.


Line Study Example

C# Line Study Example Code for Nevron Chart for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
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 - NLineStudyExample";

			// 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;
            m_View = chartView;
            chartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);

            // configure title
            chartView.Surface.Titles[0].Text = "Line Study";

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

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XValueTimelineYLinear);

            // add interlaced stripe to the Y axis
            NScaleStrip strip = new NScaleStrip(new NColorFill(ENNamedColor.Beige), null, true, 0, 0, 1, 1);
            strip.Interlaced = true;
            m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale.Strips.Add(strip);

            // setup the stock series
            m_StockSeries = new NStockSeries();
            m_Chart.Series.Add(m_StockSeries);
            m_StockSeries.DataLabelStyle = new NDataLabelStyle(false);
            m_StockSeries.CandleShape = ENCandleShape.Rectangle;
            m_StockSeries.CandleWidth = 3;
            m_StockSeries.LegendView.Mode = ENSeriesLegendMode.SeriesLogic;
            m_StockSeries.UseXValues = true;
            m_StockSeries.InflateMargins = true;

            m_LineStudySeries = new NLineStudySeries();
            m_LineStudySeries.DataLabelStyle = new NDataLabelStyle(true);
            m_Chart.Series.Add(m_LineStudySeries);

            const int dataCount = 240;
            GenerateStockData(m_StockSeries, m_LineStudySeries, dataCount, 1000);

            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 lineStudyTypeComboBox = new NComboBox();
            lineStudyTypeComboBox.Items.FillFromEnum();
            lineStudyTypeComboBox.SelectedIndexChanged += OnLineStudyTypeSelectedIndexChanged;
            stack.Add(NPairBox.Create("Line Study Type:", lineStudyTypeComboBox));

            NComboBox trendLineModeComboBox = new NComboBox();
            trendLineModeComboBox.Items.FillFromEnum();
            trendLineModeComboBox.SelectedIndexChanged += OnTrendLineModeComboBoxSelectedIndexChanged;
            stack.Add(NPairBox.Create("Trend Line Mode:", trendLineModeComboBox));

            NCheckBox showTextsCheckBox = new NCheckBox();
            showTextsCheckBox.Checked = m_LineStudySeries.DataLabelStyle != null;
            showTextsCheckBox.CheckedChanged += OnShowTextsCheckBoxCheckedChanged;
            stack.Add(NPairBox.Create("Show Texts", showTextsCheckBox));

            NCheckBox infiniteCheckBox = new NCheckBox();

            infiniteCheckBox.CheckedChanged += OnInfiniteCheckBoxCheckedChanged;
            stack.Add(NPairBox.Create("Infinite", infiniteCheckBox));

            NComboBox horzTextAlign = new NComboBox();
            horzTextAlign.Items.FillFromEnum();
            horzTextAlign.SelectedIndexChanged += OnHorzTextAlignSelectedIndexChanged;
            stack.Add(NPairBox.Create("Horizontal Text Align:", horzTextAlign));

            NComboBox vertTextAlign = new NComboBox();
            vertTextAlign.Items.FillFromEnum();
            vertTextAlign.SelectedIndexChanged += OnVertTextAlignSelectedIndexChanged;
            stack.Add(NPairBox.Create("Vertical Text Align:", vertTextAlign));

            lineStudyTypeComboBox.SelectedIndex = (int)ENLineStudyType.Line;
            infiniteCheckBox.Checked = true;
            showTextsCheckBox.Checked = false;
            trendLineModeComboBox.SelectedIndex = (int)ENTrendLineMode.Beam;

            return group;
        }

        /// 
        /// 
        /// 
        /// 
        public string GetExampleDescription()
        {
            return @"

This example demonstrates how to create a stock chart and overlay a line study on it.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Line Study Series", "https://www.nevron.com/docs/openvision/linestudy-chart-series.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), }; } #endregion #region Implementation /// /// /// /// private void OnShowTextsCheckBoxCheckedChanged(NValueChangeEventArgs arg) { m_LineStudySeries.DataLabelStyle = new NDataLabelStyle((bool)arg.NewValue); } /// /// /// /// private void OnTrendLineModeComboBoxSelectedIndexChanged(NValueChangeEventArgs arg) { m_LineStudySeries.TrendLineMode = (ENTrendLineMode)arg.NewValue; } /// /// /// /// private void OnLineStudyTypeSelectedIndexChanged(NValueChangeEventArgs arg) { m_LineStudySeries.LineStudyType = (ENLineStudyType)arg.NewValue; if (m_LineStudySeries.LineStudyType == ENLineStudyType.Line) { m_View.Surface.Titles[0].Text = "Line Study"; } else { m_View.Surface.Titles[0].Text = NStringHelpers.InsertSpacesBeforeUppersAndDigits(m_LineStudySeries.LineStudyType.ToString()); } } /// /// /// /// /// /// /// private void GenerateStockData(NStockSeries stock, NLineStudySeries lineStudy, int count, double dPrevClose) { DateTime dt = DateTime.Now; double dGrowProbability = 0.0; double open, high, low, close; stock.DataPoints.Clear(); Random random = new Random(); for (int nIndex = 0; nIndex < count; nIndex++) { open = dPrevClose; if ((nIndex % 100) <= 20) { // downtrend dGrowProbability = 0.20; } else { // uptrend dGrowProbability = 0.75; } if (random.NextDouble() < dGrowProbability) { // upward price change close = open + 2 + (random.NextDouble() * 20); high = close + (random.NextDouble() * 10); low = open - (random.NextDouble() * 10); } else { // downward price change close = open - (2 + (random.NextDouble() * 20)); high = open + (random.NextDouble() * 10); low = close - (random.NextDouble() * 10); } if (low < 1) { low = 1; } dPrevClose = close; stock.DataPoints.Add(new NStockDataPoint(dt.ToOADate(), open, close, high, low)); dt += new TimeSpan(1, 0, 0, 0); } // find the points where line studies should be attached int beginIndex = stock.FindMinValue(new NRangeI(20, stock.DataPoints.Count - 1)); int endIndex = stock.FindMinValue(new NRangeI(200, stock.DataPoints.Count - 1)); lineStudy.BeginPoint = GetLowPointFromStock(stock, beginIndex); lineStudy.EndPoint = GetHighPointFromStock(stock, endIndex); } /// /// /// /// /// /// private NPoint GetLowPointFromStock(NStockSeries stock, int dataPointIndex) { NPoint result; result.X = (double)stock.DataPoints[dataPointIndex].X; result.Y = (double)stock.DataPoints[dataPointIndex].Low; return result; } /// /// /// /// /// /// private NPoint GetHighPointFromStock(NStockSeries stock, int dataPointIndex) { NPoint result; result.X = (double)stock.DataPoints[dataPointIndex].X; result.Y = (double)stock.DataPoints[dataPointIndex].High; return result; } #endregion #region Event Handlers /// /// /// /// private void OnInfiniteCheckBoxCheckedChanged(NValueChangeEventArgs arg) { m_LineStudySeries.Infinite = (bool)arg.NewValue; } /// /// Vertical text alignment /// /// private void OnVertTextAlignSelectedIndexChanged(NValueChangeEventArgs arg) { m_LineStudySeries.DataLabelStyle.TextStyle.VertAlign = (ENTextVertAlign)arg.NewValue; } /// /// Horizontal text alignment /// /// private void OnHorzTextAlignSelectedIndexChanged(NValueChangeEventArgs arg) { m_LineStudySeries.DataLabelStyle.TextStyle.HorzAlign = (ENTextHorzAlign)arg.NewValue; } #endregion #region Fields NChartView m_View; NCartesianChart m_Chart; NStockSeries m_StockSeries; NLineStudySeries m_LineStudySeries; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

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

VB.NET Line Study Example Code for Nevron Chart for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
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 - NLineStudyExample"

		' 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
            m_View = chartView
            chartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian)

            ' configure title
            chartView.Surface.Titles(0).Text = "Line Study"

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

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XValueTimelineYLinear)

            ' add interlaced stripe to the Y axis
            Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(ENNamedColor.Beige), Nothing, True, 0, 0, 1, 1)
            strip.Interlaced = True
            m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale.Strips.Add(strip)

            ' setup the stock series
            m_StockSeries = New NStockSeries()
            m_Chart.Series.Add(m_StockSeries)
            m_StockSeries.DataLabelStyle = New NDataLabelStyle(False)
            m_StockSeries.CandleShape = ENCandleShape.Rectangle
            m_StockSeries.CandleWidth = 3
            m_StockSeries.LegendView.Mode = ENSeriesLegendMode.SeriesLogic
            m_StockSeries.UseXValues = True
            m_StockSeries.InflateMargins = True

            m_LineStudySeries = New NLineStudySeries()
            m_LineStudySeries.DataLabelStyle = New NDataLabelStyle(True)
            m_Chart.Series.Add(m_LineStudySeries)

            Const dataCount = 240
            GenerateStockData(m_StockSeries, m_LineStudySeries, dataCount, 1000)

            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 lineStudyTypeComboBox As NComboBox = New NComboBox()
            lineStudyTypeComboBox.Items.FillFromEnum(Of ENLineStudyType)()
            lineStudyTypeComboBox.SelectedIndexChanged += AddressOf OnLineStudyTypeSelectedIndexChanged
            stack.Add(NPairBox.Create("Line Study Type:", lineStudyTypeComboBox))

            Dim trendLineModeComboBox As NComboBox = New NComboBox()
            trendLineModeComboBox.Items.FillFromEnum(Of ENTrendLineMode)()
            trendLineModeComboBox.SelectedIndexChanged += AddressOf OnTrendLineModeComboBoxSelectedIndexChanged
            stack.Add(NPairBox.Create("Trend Line Mode:", trendLineModeComboBox))

            Dim showTextsCheckBox As NCheckBox = New NCheckBox()
            showTextsCheckBox.Checked = m_LineStudySeries.DataLabelStyle IsNot Nothing
            showTextsCheckBox.CheckedChanged += AddressOf OnShowTextsCheckBoxCheckedChanged
            stack.Add(NPairBox.Create("Show Texts", showTextsCheckBox))

            Dim infiniteCheckBox As NCheckBox = New NCheckBox()

            infiniteCheckBox.CheckedChanged += AddressOf OnInfiniteCheckBoxCheckedChanged
            stack.Add(NPairBox.Create("Infinite", infiniteCheckBox))

            Dim horzTextAlign As NComboBox = New NComboBox()
            horzTextAlign.Items.FillFromEnum(Of ENTextHorzAlign)()
            horzTextAlign.SelectedIndexChanged += AddressOf OnHorzTextAlignSelectedIndexChanged
            stack.Add(NPairBox.Create("Horizontal Text Align:", horzTextAlign))

            Dim vertTextAlign As NComboBox = New NComboBox()
            vertTextAlign.Items.FillFromEnum(Of ENTextVertAlign)()
            vertTextAlign.SelectedIndexChanged += AddressOf OnVertTextAlignSelectedIndexChanged
            stack.Add(NPairBox.Create("Vertical Text Align:", vertTextAlign))

            lineStudyTypeComboBox.SelectedIndex = CInt(ENLineStudyType.Line)
            infiniteCheckBox.Checked = True
            showTextsCheckBox.Checked = False
            trendLineModeComboBox.SelectedIndex = CInt(ENTrendLineMode.Beam)

            Return group
        End Function

        ''' 
        ''' 
        ''' 
        ''' 
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to create a stock chart and overlay a line study on it.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Line Study Series", "https://www.nevron.com/docs/openvision/linestudy-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 OnShowTextsCheckBoxCheckedChanged(arg As NValueChangeEventArgs) m_LineStudySeries.DataLabelStyle = New NDataLabelStyle(arg.NewValue) End Sub ''' ''' ''' ''' Private Sub OnTrendLineModeComboBoxSelectedIndexChanged(arg As NValueChangeEventArgs) m_LineStudySeries.TrendLineMode = CType(arg.NewValue, ENTrendLineMode) End Sub ''' ''' ''' ''' Private Sub OnLineStudyTypeSelectedIndexChanged(arg As NValueChangeEventArgs) m_LineStudySeries.LineStudyType = CType(arg.NewValue, ENLineStudyType) If m_LineStudySeries.LineStudyType Is ENLineStudyType.Line Then m_View.Surface.Titles(0).Text = "Line Study" Else m_View.Surface.Titles(0).Text = NStringHelpers.InsertSpacesBeforeUppersAndDigits(m_LineStudySeries.LineStudyType.ToString()) End If End Sub ''' ''' ''' ''' ''' ''' ''' Private Sub GenerateStockData(stock As NStockSeries, lineStudy As NLineStudySeries, count As Integer, dPrevClose As Double) Dim dt = Date.Now Dim dGrowProbability = 0.0 Dim open, high, low, close As Double stock.DataPoints.Clear() Dim random As Random = New Random() For nIndex = 0 To count - 1 open = dPrevClose If nIndex Mod 100 <= 20 Then ' downtrend dGrowProbability = 0.20 Else ' uptrend dGrowProbability = 0.75 End If If random.NextDouble() < dGrowProbability Then ' upward price change close = open + 2 + (random.NextDouble() * 20) high = close + (random.NextDouble() * 10) low = open - (random.NextDouble() * 10) Else ' downward price change close = open - (2 + (random.NextDouble() * 20)) high = open + (random.NextDouble() * 10) low = close - (random.NextDouble() * 10) End If If low < 1 Then low = 1 End If dPrevClose = close stock.DataPoints.Add(New NStockDataPoint(dt.ToOADate(), open, close, high, low)) dt += New TimeSpan(1, 0, 0, 0) Next ' find the points where line studies should be attached Dim beginIndex As Integer = stock.FindMinValue(New NRangeI(20, stock.DataPoints.Count - 1)) Dim endIndex As Integer = stock.FindMinValue(New NRangeI(200, stock.DataPoints.Count - 1)) lineStudy.BeginPoint = GetLowPointFromStock(stock, beginIndex) lineStudy.EndPoint = GetHighPointFromStock(stock, endIndex) End Sub ''' ''' ''' ''' ''' ''' Private Function GetLowPointFromStock(stock As NStockSeries, dataPointIndex As Integer) As NPoint Dim result As NPoint result.X = stock.DataPoints(dataPointIndex).X result.Y = stock.DataPoints(dataPointIndex).Low Return result End Function ''' ''' ''' ''' ''' ''' Private Function GetHighPointFromStock(stock As NStockSeries, dataPointIndex As Integer) As NPoint Dim result As NPoint result.X = stock.DataPoints(dataPointIndex).X result.Y = stock.DataPoints(dataPointIndex).High Return result End Function #End Region #Region "Event Handlers" ''' ''' ''' ''' Private Sub OnInfiniteCheckBoxCheckedChanged(arg As NValueChangeEventArgs) m_LineStudySeries.Infinite = CBool(arg.NewValue) End Sub ''' ''' Vertical text alignment ''' ''' Private Sub OnVertTextAlignSelectedIndexChanged(arg As NValueChangeEventArgs) m_LineStudySeries.DataLabelStyle.TextStyle.VertAlign = CType(arg.NewValue, ENTextVertAlign) End Sub ''' ''' Horizontal text alignment ''' ''' Private Sub OnHorzTextAlignSelectedIndexChanged(arg As NValueChangeEventArgs) m_LineStudySeries.DataLabelStyle.TextStyle.HorzAlign = CType(arg.NewValue, ENTextHorzAlign) End Sub #End Region #Region "Fields" Private m_View As NChartView Private m_Chart As NCartesianChart Private m_StockSeries As NStockSeries Private m_LineStudySeries As NLineStudySeries #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) Line Study Example for WPF Solution