Legend Layout 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.

Legend Layout Example for WinForms Description

This example demonstrates different settings related to legend layout.


Legend Layout Example

C# Legend Layout 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 - NLegendLayoutExample";

			// 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();
			m_ChartView = chartViewWithCommandBars.View;
			m_ChartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);

            // configure title
            m_ChartView.Surface.Titles[0].Text = "Legend Layout";

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

            // add interlace stripe
            NLinearScale linearScale = 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);

            // add a bar series
            NBarSeries bar1 = new NBarSeries();
            bar1.Name = "Bar1";
            bar1.MultiBarMode = ENMultiBarMode.Series;
            bar1.LegendView.Mode = ENSeriesLegendMode.DataPoints;
            bar1.DataLabelStyle = new NDataLabelStyle(false);
            bar1.ValueFormatter = new NNumericValueFormatter("0.###");
            chart.Series.Add(bar1);

            // add another bar series
            NBarSeries bar2 = new NBarSeries();
            bar2.Name = "Bar2";
            bar2.MultiBarMode = ENMultiBarMode.Clustered;
            bar2.LegendView.Mode = ENSeriesLegendMode.DataPoints;
            bar2.DataLabelStyle = new NDataLabelStyle(false); 
            bar2.ValueFormatter = new NNumericValueFormatter("0.###");
            chart.Series.Add(bar2);

            // add another bar series
            NBarSeries bar3 = new NBarSeries();
            bar3.Name = "Bar2";
            bar3.MultiBarMode = ENMultiBarMode.Clustered;
            bar3.LegendView.Mode = ENSeriesLegendMode.DataPoints;
            bar3.DataLabelStyle = new NDataLabelStyle(false);
            bar3.ValueFormatter = new NNumericValueFormatter("0.###");
            chart.Series.Add(bar3);

            Random random = new Random();
            for (int i = 0; i < 5; i++)
            {
                bar1.DataPoints.Add(new NBarDataPoint(random.Next(10, 100)));
                bar2.DataPoints.Add(new NBarDataPoint(random.Next(10, 100)));
                bar3.DataPoints.Add(new NBarDataPoint(random.Next(10, 100)));
            }

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

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

            NComboBox legendExpandModeComboBox = new NComboBox();
            legendExpandModeComboBox.Items.FillFromEnum();
            legendExpandModeComboBox.SelectedIndexChanged += OnLegendExpandModeComboBoxSelectedIndexChanged;
            stack.Add(NPairBox.Create("Expand Mode: ", legendExpandModeComboBox));

            m_RowCountUpDown = new NNumericUpDown();
            m_RowCountUpDown.Enabled = false;
            m_RowCountUpDown.Minimum = 1;
            m_RowCountUpDown.Value = 1;
            m_RowCountUpDown.ValueChanged += OnRowCountUpDownValueChanged;
            stack.Add(NPairBox.Create("Row Count: ", m_RowCountUpDown));

            m_ColCountUpDown = new NNumericUpDown();
            m_ColCountUpDown.Enabled = false;
            m_ColCountUpDown.Minimum = 1;
            m_ColCountUpDown.Value = 1;
            m_ColCountUpDown.ValueChanged += OnColCountUpDownValueChanged;
            stack.Add(NPairBox.Create("Col Count: ", m_ColCountUpDown));

            legendExpandModeComboBox.SelectedIndex = (int)ENLegendExpandMode.RowsOnly;

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

This example demonstrates different settings related to legend layout.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Legend Layout", "https://www.nevron.com/docs/openvision/chart-legend-layout.html"), ("Overview", "https://www.nevron.com/docs/openvision/chart-legeng-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers void OnRowCountUpDownValueChanged(NValueChangeEventArgs arg) { m_ChartView.Surface.Legends[0].RowCount = (int)((NNumericUpDown)arg.TargetNode).Value; } void OnColCountUpDownValueChanged(NValueChangeEventArgs arg) { m_ChartView.Surface.Legends[0].ColCount = (int)((NNumericUpDown)arg.TargetNode).Value; } void OnLegendExpandModeComboBoxSelectedIndexChanged(NValueChangeEventArgs arg) { m_ChartView.Surface.Legends[0].ExpandMode = (ENLegendExpandMode)(((NComboBox)arg.TargetNode).SelectedIndex); switch (m_ChartView.Surface.Legends[0].ExpandMode) { case ENLegendExpandMode.ColsFixed: m_ColCountUpDown.Enabled = true; m_RowCountUpDown.Enabled = false; break; case ENLegendExpandMode.RowsFixed: m_ColCountUpDown.Enabled = false; m_RowCountUpDown.Enabled = true; break; default: m_ColCountUpDown.Enabled = false; m_RowCountUpDown.Enabled = false; break; } } #endregion #region Fields private NChartView m_ChartView; private NNumericUpDown m_ColCountUpDown; private NNumericUpDown m_RowCountUpDown; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Legend Layout Example for WinForms Solution

VB.NET Legend Layout 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 - NLegendLayoutExample"

        ' 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()
            m_ChartView = chartViewWithCommandBars.View
            m_ChartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian)

            ' configure title
            m_ChartView.Surface.Titles(0).Text = "Legend Layout"

            ' configure chart
            Dim chart = CType(m_ChartView.Surface.Charts(0), NCartesianChart)

            ' add interlace stripe
            Dim linearScale As NLinearScale = TryCast(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)

            ' add a bar series
            Dim bar1 As NBarSeries = New NBarSeries()
            bar1.Name = "Bar1"
            bar1.MultiBarMode = ENMultiBarMode.Series
            bar1.LegendView.Mode = ENSeriesLegendMode.DataPoints
            bar1.DataLabelStyle = New NDataLabelStyle(False)
            bar1.ValueFormatter = New NNumericValueFormatter("0.###")
            chart.Series.Add(bar1)

            ' add another bar series
            Dim bar2 As NBarSeries = New NBarSeries()
            bar2.Name = "Bar2"
            bar2.MultiBarMode = ENMultiBarMode.Clustered
            bar2.LegendView.Mode = ENSeriesLegendMode.DataPoints
            bar2.DataLabelStyle = New NDataLabelStyle(False)
            bar2.ValueFormatter = New NNumericValueFormatter("0.###")
            chart.Series.Add(bar2)

            ' add another bar series
            Dim bar3 As NBarSeries = New NBarSeries()
            bar3.Name = "Bar2"
            bar3.MultiBarMode = ENMultiBarMode.Clustered
            bar3.LegendView.Mode = ENSeriesLegendMode.DataPoints
            bar3.DataLabelStyle = New NDataLabelStyle(False)
            bar3.ValueFormatter = New NNumericValueFormatter("0.###")
            chart.Series.Add(bar3)

            Dim random As Random = New Random()
            For i = 0 To 4
                bar1.DataPoints.Add(New NBarDataPoint(random.Next(10, 100)))
                bar2.DataPoints.Add(New NBarDataPoint(random.Next(10, 100)))
                bar3.DataPoints.Add(New NBarDataPoint(random.Next(10, 100)))
            Next

            m_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)

            Dim legendExpandModeComboBox As NComboBox = New NComboBox()
            legendExpandModeComboBox.Items.FillFromEnum(Of ENLegendExpandMode)()
            legendExpandModeComboBox.SelectedIndexChanged += AddressOf OnLegendExpandModeComboBoxSelectedIndexChanged
            stack.Add(NPairBox.Create("Expand Mode: ", legendExpandModeComboBox))

            m_RowCountUpDown = New NNumericUpDown()
            m_RowCountUpDown.Enabled = False
            m_RowCountUpDown.Minimum = 1
            m_RowCountUpDown.Value = 1
            Me.m_RowCountUpDown.ValueChanged += AddressOf OnRowCountUpDownValueChanged
            stack.Add(NPairBox.Create("Row Count: ", m_RowCountUpDown))

            m_ColCountUpDown = New NNumericUpDown()
            m_ColCountUpDown.Enabled = False
            m_ColCountUpDown.Minimum = 1
            m_ColCountUpDown.Value = 1
            Me.m_ColCountUpDown.ValueChanged += AddressOf OnColCountUpDownValueChanged
            stack.Add(NPairBox.Create("Col Count: ", m_ColCountUpDown))

            legendExpandModeComboBox.SelectedIndex = CInt(ENLegendExpandMode.RowsOnly)

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

This example demonstrates different settings related to legend layout.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Legend Layout", "https://www.nevron.com/docs/openvision/chart-legend-layout.html"), ("Overview", "https://www.nevron.com/docs/openvision/chart-legeng-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnRowCountUpDownValueChanged(arg As NValueChangeEventArgs) m_ChartView.Surface.Legends(0).RowCount = CInt(CType(arg.TargetNode, NNumericUpDown).Value) End Sub Private Sub OnColCountUpDownValueChanged(arg As NValueChangeEventArgs) m_ChartView.Surface.Legends(0).ColCount = CInt(CType(arg.TargetNode, NNumericUpDown).Value) End Sub Private Sub OnLegendExpandModeComboBoxSelectedIndexChanged(arg As NValueChangeEventArgs) m_ChartView.Surface.Legends(0).ExpandMode = CType(CType(arg.TargetNode, NComboBox).SelectedIndex, ENLegendExpandMode) Select Case m_ChartView.Surface.Legends(CInt(0)).ExpandMode Case ENLegendExpandMode.ColsFixed m_ColCountUpDown.Enabled = True m_RowCountUpDown.Enabled = False Case ENLegendExpandMode.RowsFixed m_ColCountUpDown.Enabled = False m_RowCountUpDown.Enabled = True Case Else m_ColCountUpDown.Enabled = False m_RowCountUpDown.Enabled = False End Select End Sub #End Region #Region "Fields" Private m_ChartView As NChartView Private m_ColCountUpDown As NNumericUpDown Private m_RowCountUpDown 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) Legend Layout Example for WinForms Solution

Related Documentation Links