Date Time Scale 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.

Date Time Scale Example for WinForms Description

This example demonstrates how to create a standard date/time scale.


Date Time Scale Example

C# Date Time Scale Example Code for Nevron Chart for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.DataStructures;
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.Globalization;
using System.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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 = "Date Time Scale";

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

			m_Chart.Axes[ENCartesianAxis.PrimaryX].Scale = new NDateTimeScale();

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

			// Add a line series (data will be generated with example controls)
			NLineSeries line = new NLineSeries();
			m_Chart.Series.Add(line);

			line.UseXValues = true;

			line.DataLabelStyle = new NDataLabelStyle(false);
			line.InflateMargins = true;

			NMarkerStyle markerStyle = new NMarkerStyle();
			markerStyle.Visible = true;
			line.MarkerStyle = markerStyle;

			// create a date time scale
			m_DateTimeScale = (NDateTimeScale)m_Chart.Axes[ENCartesianAxis.PrimaryX].Scale;
			m_DateTimeScale.Labels.Style.Angle = new NScaleLabelAngle(ENScaleLabelAngleMode.Scale, 90);
			m_DateTimeScale.Labels.Style.ContentAlignment = ENContentAlignment.TopCenter;

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

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

			stack.Add(new NLabel("Allowed Date Time Units"));

			NDateTimeUnit[] dateTimeUnits = new NDateTimeUnit[] {
						NDateTimeUnit.Century,
						NDateTimeUnit.Decade,
						NDateTimeUnit.Year,
						NDateTimeUnit.HalfYear,
						NDateTimeUnit.Quarter,
						NDateTimeUnit.Month,
						NDateTimeUnit.Week,
						NDateTimeUnit.Day,
						NDateTimeUnit.HalfDay,
						NDateTimeUnit.Hour,
						NDateTimeUnit.Minute,
						NDateTimeUnit.Second,
						NDateTimeUnit.Millisecond,
						NDateTimeUnit.Tick
				};

			m_DateTimeUnitListBox = new NListBox();
			for (int i = 0; i < dateTimeUnits.Length; i++)
			{
				NDateTimeUnit dateTimeUnit = dateTimeUnits[i];
                NCheckBox dateTimeUnitCheckBox = new NCheckBox(NStringHelpers.InsertSpacesBeforeUppersAndDigits(dateTimeUnit.DateTimeUnit.ToString()));

				dateTimeUnitCheckBox.Checked = true;
				dateTimeUnitCheckBox.CheckedChanged += OnDateTimeUnitCheckBoxCheckedChanged;
				dateTimeUnitCheckBox.Tag = dateTimeUnit;				

				m_DateTimeUnitListBox.Items.Add(new NListBoxItem(dateTimeUnitCheckBox));
			}

			stack.Add(m_DateTimeUnitListBox);
			OnDateTimeUnitCheckBoxCheckedChanged(null);

			NCheckBox enableUnitSensitiveFormattingCheckBox = new NCheckBox("Enable Unit Sensitive Formatting");
			enableUnitSensitiveFormattingCheckBox.CheckedChanged += OnEnableUnitSensitiveFormattingCheckBoxCheckedChanged;
			stack.Add(enableUnitSensitiveFormattingCheckBox);

			enableUnitSensitiveFormattingCheckBox.Checked = true;

			stack.Add(new NLabel("Start Date:"));
			m_StartDateTimeBox = new NDateTimeBox();
			m_StartDateTimeBox.SelectedDateChanged += OnStartDateTimeBoxSelectedDateChanged;
			stack.Add(m_StartDateTimeBox);

			stack.Add(new NLabel("End Date:"));
			m_EndDateTimeBox = new NDateTimeBox();
			m_EndDateTimeBox.SelectedDateChanged += OnEndDateTimeBoxSelectedDateChanged;
			stack.Add(m_EndDateTimeBox);

			NButton generateRandomDataButton = new NButton("Generate Random Data");
			generateRandomDataButton.Click += OnGenerateRandomDataButtonClick;
			stack.Add(generateRandomDataButton);

			m_StartDateTimeBox.SelectedDate = DateTime.Now;
			m_EndDateTimeBox.SelectedDate = CultureInfo.CurrentCulture.Calendar.AddYears(m_StartDateTimeBox.SelectedDate, 2);

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

This example demonstrates how to create a standard date/time scale.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("DateTime Scale", "https://www.nevron.com/docs/openvision/datetime-scale.html"), ("Scales Overview", "https://www.nevron.com/docs/openvision/scales-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers void OnEnableUnitSensitiveFormattingCheckBoxCheckedChanged(NValueChangeEventArgs arg) { if (((NCheckBox)arg.TargetNode).Checked) { m_DateTimeScale.Labels.TextProvider = new NDateTimeUnitSensitiveLabelTextProvider(); } else { m_DateTimeScale.Labels.TextProvider = new NFormattedScaleLabelTextProvider(new NDateTimeValueFormatter(ENDateTimeValueFormat.Date)); } } void OnEndDateTimeBoxSelectedDateChanged(NValueChangeEventArgs arg) { OnGenerateRandomDataButtonClick(null); } void OnStartDateTimeBoxSelectedDateChanged(NValueChangeEventArgs arg) { OnGenerateRandomDataButtonClick(null); } void OnGenerateRandomDataButtonClick(NEventArgs arg) { DateTime startDate = m_StartDateTimeBox.SelectedDate; DateTime endDate = m_EndDateTimeBox.SelectedDate; if (startDate > endDate) { DateTime temp = startDate; startDate = endDate; endDate = temp; } // Get the line series from the chart NLineSeries line = (NLineSeries)m_Chart.Series[0]; TimeSpan span = endDate - startDate; span = new TimeSpan(span.Ticks / 30); line.DataPoints.Clear(); if (span.Ticks > 0) { Random random = new Random(); while (startDate < endDate) { line.DataPoints.Add(new NLineDataPoint(NDateTimeHelpers.ToOADate(startDate), random.Next(100))); startDate += span; } } } void OnDateTimeUnitCheckBoxCheckedChanged(NValueChangeEventArgs arg) { NList dateTimeUnits = new NList(); // collect the checked date time units for (int i = 0; i < m_DateTimeUnitListBox.Items.Count; i++) { NCheckBox dateTimeUnitCheckBox = m_DateTimeUnitListBox.Items[i].Content as NCheckBox; if (dateTimeUnitCheckBox.Checked) { dateTimeUnits.Add((NDateTimeUnit)dateTimeUnitCheckBox.Tag); } } m_DateTimeScale.AutoDateTimeUnits = new NDomArray(dateTimeUnits.ToArray()); } #endregion #region Fields NCartesianChart m_Chart; NDateTimeScale m_DateTimeScale; NListBox m_DateTimeUnitListBox; NDateTimeBox m_StartDateTimeBox; NDateTimeBox m_EndDateTimeBox; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Date Time Scale Example for WinForms Solution

VB.NET Date Time Scale Example Code for Nevron Chart for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms
Imports System
Imports System.Globalization

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 - NDateTimeScaleExample"

        ' 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 = "Date Time Scale"

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

            m_Chart.Axes(ENCartesianAxis.PrimaryX).Scale = New NDateTimeScale()

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

            ' Add a line series (data will be generated with example controls)
            Dim line As NLineSeries = New NLineSeries()
            m_Chart.Series.Add(line)

            line.UseXValues = True

            line.DataLabelStyle = New NDataLabelStyle(False)
            line.InflateMargins = True

            Dim markerStyle As NMarkerStyle = New NMarkerStyle()
            markerStyle.Visible = True
            line.MarkerStyle = markerStyle

            ' create a date time scale
            m_DateTimeScale = m_Chart.Axes(ENCartesianAxis.PrimaryX).Scale
            m_DateTimeScale.Labels.Style.Angle = New NScaleLabelAngle(ENScaleLabelAngleMode.Scale, 90)
            m_DateTimeScale.Labels.Style.ContentAlignment = ENContentAlignment.TopCenter

            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 boxGroup As NUniSizeBoxGroup = New NUniSizeBoxGroup(stack)

            stack.Add(New NLabel("Allowed Date Time Units"))

            Dim dateTimeUnits = New NDateTimeUnit() {NDateTimeUnit.Century, NDateTimeUnit.Decade, NDateTimeUnit.Year, NDateTimeUnit.HalfYear, NDateTimeUnit.Quarter, NDateTimeUnit.Month, NDateTimeUnit.Week, NDateTimeUnit.Day, NDateTimeUnit.HalfDay, NDateTimeUnit.Hour, NDateTimeUnit.Minute, NDateTimeUnit.Second, NDateTimeUnit.Millisecond, NDateTimeUnit.Tick}

            m_DateTimeUnitListBox = New NListBox()
            For i = 0 To dateTimeUnits.Length - 1
                Dim dateTimeUnit = dateTimeUnits(i)
                Dim dateTimeUnitCheckBox As NCheckBox = New NCheckBox(NStringHelpers.InsertSpacesBeforeUppersAndDigits(dateTimeUnit.DateTimeUnit.ToString()))

                dateTimeUnitCheckBox.Checked = True
                dateTimeUnitCheckBox.CheckedChanged += AddressOf OnDateTimeUnitCheckBoxCheckedChanged
                dateTimeUnitCheckBox.Tag = dateTimeUnit

                m_DateTimeUnitListBox.Items.Add(New NListBoxItem(dateTimeUnitCheckBox))
            Next

            stack.Add(m_DateTimeUnitListBox)
            OnDateTimeUnitCheckBoxCheckedChanged(Nothing)

            Dim enableUnitSensitiveFormattingCheckBox As NCheckBox = New NCheckBox("Enable Unit Sensitive Formatting")
            enableUnitSensitiveFormattingCheckBox.CheckedChanged += AddressOf OnEnableUnitSensitiveFormattingCheckBoxCheckedChanged
            stack.Add(enableUnitSensitiveFormattingCheckBox)

            enableUnitSensitiveFormattingCheckBox.Checked = True

            stack.Add(New NLabel("Start Date:"))
            m_StartDateTimeBox = New NDateTimeBox()
            Me.m_StartDateTimeBox.SelectedDateChanged += AddressOf OnStartDateTimeBoxSelectedDateChanged
            stack.Add(m_StartDateTimeBox)

            stack.Add(New NLabel("End Date:"))
            m_EndDateTimeBox = New NDateTimeBox()
            Me.m_EndDateTimeBox.SelectedDateChanged += AddressOf OnEndDateTimeBoxSelectedDateChanged
            stack.Add(m_EndDateTimeBox)

            Dim generateRandomDataButton As NButton = New NButton("Generate Random Data")
            generateRandomDataButton.Click += AddressOf OnGenerateRandomDataButtonClick
            stack.Add(generateRandomDataButton)

            m_StartDateTimeBox.SelectedDate = Date.Now
            m_EndDateTimeBox.SelectedDate = CultureInfo.CurrentCulture.Calendar.AddYears(m_StartDateTimeBox.SelectedDate, 2)

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

This example demonstrates how to create a standard date/time scale.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("DateTime Scale", "https://www.nevron.com/docs/openvision/datetime-scale.html"), ("Scales Overview", "https://www.nevron.com/docs/openvision/scales-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnEnableUnitSensitiveFormattingCheckBoxCheckedChanged(arg As NValueChangeEventArgs) If CType(arg.TargetNode, NCheckBox).Checked Then m_DateTimeScale.Labels.TextProvider = New NDateTimeUnitSensitiveLabelTextProvider() Else m_DateTimeScale.Labels.TextProvider = New NFormattedScaleLabelTextProvider(New NDateTimeValueFormatter(ENDateTimeValueFormat.Date)) End If End Sub Private Sub OnEndDateTimeBoxSelectedDateChanged(arg As NValueChangeEventArgs) OnGenerateRandomDataButtonClick(Nothing) End Sub Private Sub OnStartDateTimeBoxSelectedDateChanged(arg As NValueChangeEventArgs) OnGenerateRandomDataButtonClick(Nothing) End Sub Private Sub OnGenerateRandomDataButtonClick(arg As NEventArgs) Dim startDate As Date = m_StartDateTimeBox.SelectedDate Dim endDate As Date = m_EndDateTimeBox.SelectedDate If startDate > endDate Then Dim temp = startDate startDate = endDate endDate = temp End If ' Get the line series from the chart Dim line = CType(m_Chart.Series(0), NLineSeries) Dim span = endDate - startDate span = New TimeSpan(span.Ticks / 30) line.DataPoints.Clear() If span.Ticks > 0 Then Dim random As Random = New Random() While startDate < endDate line.DataPoints.Add(New NLineDataPoint(NDateTimeHelpers.ToOADate(startDate), random.Next(100))) startDate += span End While End If End Sub Private Sub OnDateTimeUnitCheckBoxCheckedChanged(arg As NValueChangeEventArgs) Dim dateTimeUnits As NList(Of NDateTimeUnit) = New NList(Of NDateTimeUnit)() ' collect the checked date time units For i = 0 To m_DateTimeUnitListBox.Items.Count - 1 Dim dateTimeUnitCheckBox As NCheckBox = TryCast(m_DateTimeUnitListBox.Items(i).Content, NCheckBox) If dateTimeUnitCheckBox.Checked Then dateTimeUnits.Add(CType(dateTimeUnitCheckBox.Tag, NDateTimeUnit)) End If Next m_DateTimeScale.AutoDateTimeUnits = New NDomArray(Of NDateTimeUnit)(dateTimeUnits.ToArray()) End Sub #End Region #Region "Fields" Private m_Chart As NCartesianChart Private m_DateTimeScale As NDateTimeScale Private m_DateTimeUnitListBox As NListBox Private m_StartDateTimeBox As NDateTimeBox Private m_EndDateTimeBox As NDateTimeBox #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) Date Time Scale Example for WinForms Solution

Related Documentation Links