Candle Stock Example for WinForms

Nevron NOV Chart for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms 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.

Candle Stock Example for WinForms Description

This example demonstrates how to create a candle stock chart.


Candle Stock Example

C# Candle Stock Example Code for Nevron Chart for WinForms
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 - NCandleStockExample";

			// 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 = "Candle Stock";

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

			m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear);

			// setup X axis
			NCartesianAxis axis = m_Chart.Axes[ENCartesianAxis.PrimaryX];

			axis.Scale = new NValueTimelineScale(); 

			// setup primary Y axis
			axis = m_Chart.Axes[ENCartesianAxis.PrimaryY];
			NLinearScale linearScale = (NLinearScale)axis.Scale;

			// configure ticks and grid lines
			linearScale.MajorGridLines.Stroke = new NStroke(NColor.LightGray);
			linearScale.InnerMajorTicks.Visible = false;

			// add interlaced stripe 
			NScaleStrip strip = new NScaleStrip();
			strip.Fill = new NColorFill(NColor.Beige);
			strip.Interlaced = true;
			linearScale.Strips.Add(strip);

			// Setup the stock series
			m_Stock = new NStockSeries();
			m_Chart.Series.Add(m_Stock);

			m_Stock.DataLabelStyle = new NDataLabelStyle(false);
			m_Stock.CandleShape = ENCandleShape.Rectangle;
			m_Stock.CandleWidth = 4;
			m_Stock.UseXValues = true;

			GenerateData();

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			NUniSizeBoxGroup group = new NUniSizeBoxGroup(stack);
			
			NCheckBox showHighLowLine = new NCheckBox("Show High Low Line");
			showHighLowLine.CheckedChanged += OnShowHighLowLineCheckedChanged;
			stack.Add(showHighLowLine);
			showHighLowLine.Checked = true;

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

This example demonstrates how to create a candle stock chart.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Stock Series", "https://www.nevron.com/docs/openvision/stock-chart-series.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), }; } #endregion #region Implementation void GenerateData() { // generate data for 30 weeks DateTime dtNow = DateTime.Now; DateTime dtEnd = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 7, 0, 0, 0); DateTime dtStart = NDateTimeUnit.Week.Add(dtEnd, -30); NDateTimeSpan span = new NDateTimeSpan(1, NDateTimeUnit.Day); long count = span.GetSpanCountInRange(new NDateTimeRange(dtStart, dtEnd)); double open, high, low, close; m_Stock.DataPoints.Clear(); Random random = new Random(); double prevClose = 100; for (int nIndex = 0; nIndex < count; nIndex++) { open = prevClose; if (prevClose < 25 || random.NextDouble() > 0.5) { // 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; } prevClose = close; m_Stock.DataPoints.Add(new NStockDataPoint(NDateTimeHelpers.ToOADate(dtNow), open, close, high, low)); dtNow = span.Add(dtNow); } } #endregion #region Event Handlers private void OnShowHighLowLineCheckedChanged(NValueChangeEventArgs arg) { m_Stock.ShowHighLow = ((NCheckBox)arg.TargetNode).Checked; } #endregion #region Fields NStockSeries m_Stock; NCartesianChart m_Chart; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Candle Stock Example for WinForms Solution

VB.NET Candle Stock Example Code for Nevron Chart for WinForms
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.Forms
Imports System

Public Class Form1

#Region "Constructors"

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

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NCandleStockExample"

        ' 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)
        host.Dock = DockStyle.Fill
        Controls.Add(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 = "Candle Stock"

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

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear)

            ' setup X axis
            Dim axis = m_Chart.Axes(ENCartesianAxis.PrimaryX)

            axis.Scale = New NValueTimelineScale()

            ' setup primary Y axis
            axis = m_Chart.Axes(ENCartesianAxis.PrimaryY)
            Dim linearScale = CType(axis.Scale, NLinearScale)

            ' configure ticks and grid lines
            linearScale.MajorGridLines.Stroke = New NStroke(NColor.LightGray)
            linearScale.InnerMajorTicks.Visible = False

            ' add interlaced stripe 
            Dim strip As NScaleStrip = New NScaleStrip()
            strip.Fill = New NColorFill(NColor.Beige)
            strip.Interlaced = True
            linearScale.Strips.Add(strip)

            ' Setup the stock series
            m_Stock = New NStockSeries()
            m_Chart.Series.Add(m_Stock)

            m_Stock.DataLabelStyle = New NDataLabelStyle(False)
            m_Stock.CandleShape = ENCandleShape.Rectangle
            m_Stock.CandleWidth = 4
            m_Stock.UseXValues = True

            GenerateData()

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

            Dim showHighLowLine As NCheckBox = New NCheckBox("Show High Low Line")
            showHighLowLine.CheckedChanged += AddressOf OnShowHighLowLineCheckedChanged
            stack.Add(showHighLowLine)
            showHighLowLine.Checked = True

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

This example demonstrates how to create a candle stock chart.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Stock Series", "https://www.nevron.com/docs/openvision/stock-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 GenerateData() ' generate data for 30 weeks Dim dtNow = Date.Now Dim dtEnd As Date = New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 7, 0, 0, 0) Dim dtStart As Date = NDateTimeUnit.Week.Add(dtEnd, -30) Dim span As NDateTimeSpan = New NDateTimeSpan(1, NDateTimeUnit.Day) Dim count As Long = span.GetSpanCountInRange(New NDateTimeRange(dtStart, dtEnd)) Dim open, high, low, close As Double m_Stock.DataPoints.Clear() Dim random As Random = New Random() Dim prevClose As Double = 100 For nIndex As Integer = 0 To count - 1 open = prevClose If prevClose < 25 OrElse random.NextDouble() > 0.5 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 prevClose = close m_Stock.DataPoints.Add(New NStockDataPoint(NDateTimeHelpers.ToOADate(dtNow), open, close, high, low)) dtNow = span.Add(dtNow) Next End Sub #End Region #Region "Event Handlers" Private Sub OnShowHighLowLineCheckedChanged(arg As NValueChangeEventArgs) m_Stock.ShowHighLow = CType(arg.TargetNode, NCheckBox).Checked End Sub #End Region #Region "Fields" Private m_Stock As NStockSeries Private m_Chart As NCartesianChart #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) Candle Stock Example for WinForms Solution

Related Documentation Links