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

Line Labels Example for WinForms Description

This example demonstrates how automatic data label layout works with line data labels.


Line Labels Example

C# Line Labels 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 - NLineLabelsExample";

			// 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 = "Line Labels";

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

			m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear);

			// configure Y axis
			NLinearScale scaleY = (NLinearScale)m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale;
			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);

			// line series
			m_Line = new NLineSeries();
			m_Chart.Series.Add(m_Line);

			m_Line.InflateMargins = true;

			NMarkerStyle markerStyle = new NMarkerStyle();
			markerStyle.Visible = true;
			markerStyle.Fill = new NColorFill(NColor.DarkOrange);
			markerStyle.Shape = ENPointShape3D.Ellipse;
			markerStyle.Size = new NSize(2, 2);
			m_Line.MarkerStyle = markerStyle;
			m_Line.ValueFormatter = new NNumericValueFormatter("0.000");

			NDataLabelStyle dataLabelStyle = new NDataLabelStyle();
			dataLabelStyle.Visible = true;
			dataLabelStyle.VertAlign = ENVerticalAlignment.Top;
			dataLabelStyle.ArrowLength = 10;
			dataLabelStyle.ArrowStroke = new NStroke(NColor.DarkOrange);
			dataLabelStyle.TextStyle.Background.Border = new NStroke(NColor.DarkOrange);
			dataLabelStyle.Format = "";
			m_Line.DataLabelStyle = dataLabelStyle;

			m_Chart.LabelLayout.EnableInitialPositioning = true;
			m_Chart.LabelLayout.EnableLabelAdjustment = true;

			m_Line.LabelLayout.EnableDataPointSafeguard = true;
			m_Line.LabelLayout.DataPointSafeguardSize = new NSize(2, 2);
			m_Line.LabelLayout.UseLabelLocations = true;
			m_Line.LabelLayout.OutOfBoundsLocationMode = ENOutOfBoundsLocationMode.PushWithinBounds;
			m_Line.LabelLayout.InvertLocationsIfIgnored = true;

			// fill with random data
			OnGenerateDataButtonClick(null);

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

			NCheckBox enableInitialPositioningCheckBox = new NCheckBox("Enable Initial Positioning");
			enableInitialPositioningCheckBox.CheckedChanged += OnEnableInitialPositioningCheckBoxCheckedChanged;
			stack.Add(enableInitialPositioningCheckBox);

			NCheckBox enableLabelAdjustmentCheckBox = new NCheckBox("Enable Label Adjustment");
			enableLabelAdjustmentCheckBox.CheckedChanged += OnEnableLabelAdjustmentCheckBoxCheckedChanged;
			stack.Add(enableLabelAdjustmentCheckBox);

			NButton generateDataButton = new NButton("Generate Data");
			generateDataButton.Click += OnGenerateDataButtonClick;
			stack.Add(generateDataButton);

			enableInitialPositioningCheckBox.Checked = true;
			enableLabelAdjustmentCheckBox.Checked = true;

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

This example demonstrates how automatic data label layout works with line data labels.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Line Series", "https://www.nevron.com/docs/openvision/line-chart-series.html"), ("Series Label Layout", "https://www.nevron.com/docs/openvision/chart-series-label-layout.html"), ("Series Labels Appearance", "https://www.nevron.com/docs/openvision/chart-series-labels-appearance.html"), ("Label Formatting", "https://www.nevron.com/docs/openvision/chart-label-formatting.html"), }; } #endregion #region Implementation #endregion #region Event Handlers void OnEnableLabelAdjustmentCheckBoxCheckedChanged(NValueChangeEventArgs arg) { m_Chart.LabelLayout.EnableLabelAdjustment = ((NCheckBox)arg.TargetNode).Checked; } void OnEnableInitialPositioningCheckBoxCheckedChanged(NValueChangeEventArgs arg) { m_Chart.LabelLayout.EnableInitialPositioning = ((NCheckBox)arg.TargetNode).Checked; } void OnGenerateDataButtonClick(NEventArgs arg) { m_Line.DataPoints.Clear(); Random random = new Random(); for (int i = 0; i < 30; i++) { double value = Math.Sin(i * 0.2) * 10 + random.NextDouble() * 2; m_Line.DataPoints.Add(new NLineDataPoint(value)); } } #endregion #region Fields NCartesianChart m_Chart; NLineSeries m_Line; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Line Labels Example for WinForms Solution

VB.NET Line Labels 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 - NLineLabelsExample"

        ' 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 = "Line Labels"

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

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear)

            ' configure Y axis
            Dim scaleY As NLinearScale = m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale
            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)

            ' line series
            m_Line = New NLineSeries()
            m_Chart.Series.Add(m_Line)

            m_Line.InflateMargins = True

            Dim markerStyle As NMarkerStyle = New NMarkerStyle()
            markerStyle.Visible = True
            markerStyle.Fill = New NColorFill(NColor.DarkOrange)
            markerStyle.Shape = ENPointShape3D.Ellipse
            markerStyle.Size = New NSize(2, 2)
            m_Line.MarkerStyle = markerStyle
            m_Line.ValueFormatter = New NNumericValueFormatter("0.000")

            Dim dataLabelStyle As NDataLabelStyle = New NDataLabelStyle()
            dataLabelStyle.Visible = True
            dataLabelStyle.VertAlign = ENVerticalAlignment.Top
            dataLabelStyle.ArrowLength = 10
            dataLabelStyle.ArrowStroke = New NStroke(NColor.DarkOrange)
            dataLabelStyle.TextStyle.Background.Border = New NStroke(NColor.DarkOrange)
            dataLabelStyle.Format = ""
            m_Line.DataLabelStyle = dataLabelStyle

            m_Chart.LabelLayout.EnableInitialPositioning = True
            m_Chart.LabelLayout.EnableLabelAdjustment = True

            m_Line.LabelLayout.EnableDataPointSafeguard = True
            m_Line.LabelLayout.DataPointSafeguardSize = New NSize(2, 2)
            m_Line.LabelLayout.UseLabelLocations = True
            m_Line.LabelLayout.OutOfBoundsLocationMode = ENOutOfBoundsLocationMode.PushWithinBounds
            m_Line.LabelLayout.InvertLocationsIfIgnored = True

            ' fill with random data
            OnGenerateDataButtonClick(Nothing)

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

            Dim enableInitialPositioningCheckBox As NCheckBox = New NCheckBox("Enable Initial Positioning")
            enableInitialPositioningCheckBox.CheckedChanged += AddressOf OnEnableInitialPositioningCheckBoxCheckedChanged
            stack.Add(enableInitialPositioningCheckBox)

            Dim enableLabelAdjustmentCheckBox As NCheckBox = New NCheckBox("Enable Label Adjustment")
            enableLabelAdjustmentCheckBox.CheckedChanged += AddressOf OnEnableLabelAdjustmentCheckBoxCheckedChanged
            stack.Add(enableLabelAdjustmentCheckBox)

            Dim generateDataButton As NButton = New NButton("Generate Data")
            generateDataButton.Click += AddressOf OnGenerateDataButtonClick
            stack.Add(generateDataButton)

            enableInitialPositioningCheckBox.Checked = True
            enableLabelAdjustmentCheckBox.Checked = True

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

This example demonstrates how automatic data label layout works with line data labels.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Line Series", "https://www.nevron.com/docs/openvision/line-chart-series.html"), ("Series Label Layout", "https://www.nevron.com/docs/openvision/chart-series-label-layout.html"), ("Series Labels Appearance", "https://www.nevron.com/docs/openvision/chart-series-labels-appearance.html"), ("Label Formatting", "https://www.nevron.com/docs/openvision/chart-label-formatting.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnEnableLabelAdjustmentCheckBoxCheckedChanged(arg As NValueChangeEventArgs) m_Chart.LabelLayout.EnableLabelAdjustment = CType(arg.TargetNode, NCheckBox).Checked End Sub Private Sub OnEnableInitialPositioningCheckBoxCheckedChanged(arg As NValueChangeEventArgs) m_Chart.LabelLayout.EnableInitialPositioning = CType(arg.TargetNode, NCheckBox).Checked End Sub Private Sub OnGenerateDataButtonClick(arg As NEventArgs) m_Line.DataPoints.Clear() Dim random As Random = New Random() For i = 0 To 29 Dim value As Double = Math.Sin(i * 0.2) * 10 + random.NextDouble() * 2 m_Line.DataPoints.Add(New NLineDataPoint(value)) Next End Sub #End Region #Region "Fields" Private m_Chart As NCartesianChart Private m_Line As NLineSeries #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 Labels Example for WinForms Solution