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

Legend Appearance Example for WPF Description

This example demonstrates how to modify the legend appearance.


Legend Appearance Example

C# Legend Appearance 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.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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 = "Legend Appearance";

            m_Legend = chartView.Surface.Legends[0];
            m_Legend.ExpandMode = ENLegendExpandMode.ColsFixed;
            m_Legend.ColCount = 3;

            m_Legend.Border = NBorder.CreateFilledBorder(NColor.Black);
            m_Legend.BorderThickness = new NMargins(2);
            m_Legend.BackgroundFill = new NStockGradientFill(NColor.White, NColor.LightGray);
            m_Legend.VerticalPlacement = ENVerticalPlacement.Top;

            // configure chart
            NCartesianChart chart = (NCartesianChart)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);

			// setup a bar series
			NBarSeries bar = new NBarSeries();
			bar.Name = "Bar Series";
			bar.InflateMargins = true;
			bar.UseXValues = false;
            bar.LegendView.Mode = ENSeriesLegendMode.DataPoints;

			// add some data to the bar series
			bar.LegendView.Mode = ENSeriesLegendMode.DataPoints;
			bar.DataPoints.Add(new NBarDataPoint(18, "C++"));
			bar.DataPoints.Add(new NBarDataPoint(15, "Ruby"));
			bar.DataPoints.Add(new NBarDataPoint(21, "Python"));
			bar.DataPoints.Add(new NBarDataPoint(23, "Java"));
			bar.DataPoints.Add(new NBarDataPoint(27, "Javascript"));
			bar.DataPoints.Add(new NBarDataPoint(29, "C#"));
			bar.DataPoints.Add(new NBarDataPoint(26, "PHP"));
            bar.DataPoints.Add(new NBarDataPoint(17, "Objective C"));
            bar.DataPoints.Add(new NBarDataPoint(24, "SQL"));
            bar.DataPoints.Add(new NBarDataPoint(13, "Object Pascal"));
            bar.DataPoints.Add(new NBarDataPoint(19, "Visual Basic"));
            bar.DataPoints.Add(new NBarDataPoint(16, "Open Edge ABL"));

			chart.Series.Add(bar);

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

            NTextBox legendHeaderTextBox = new NTextBox();
            legendHeaderTextBox.TextChanged += OnLegendHeaderTextBoxChanged;
			stack.Add(NPairBox.Create("Header: ", legendHeaderTextBox));

            NTextBox legendFooterTextBox = new NTextBox();
            legendFooterTextBox.TextChanged += OnLegendFooterTextBoxChanged;
            stack.Add(NPairBox.Create("Footer: ", legendFooterTextBox));

            m_HorizontalInterlaceStripesCheckBox = new NCheckBox("Horizontal Interlace Stripes");
            m_HorizontalInterlaceStripesCheckBox.CheckedChanged += OnVerticalInterlaceStripesCheckBoxCheckedChanged;
            stack.Add(m_HorizontalInterlaceStripesCheckBox);

            m_VerticalInterlaceStripesCheckBox = new NCheckBox("Vertical Interlace Stripes");
            m_VerticalInterlaceStripesCheckBox.CheckedChanged += OnHorizontalInterlaceStripesCheckBoxCheckedChanged;
            stack.Add(m_VerticalInterlaceStripesCheckBox);

            NCheckBox showHorizontalGridLinesCheckBox = new NCheckBox("Show Horizontal Gridlines");
            showHorizontalGridLinesCheckBox.Checked = true;
            showHorizontalGridLinesCheckBox.CheckedChanged += OnShowHorizontalGridLinesCheckBoxCheckedChanged;
            stack.Add(showHorizontalGridLinesCheckBox);

            NCheckBox showVerticalGridLinesCheckBox = new NCheckBox("Show Vertical Gridlines");
            showVerticalGridLinesCheckBox.Checked = true;
            showVerticalGridLinesCheckBox.CheckedChanged += OnShowVerticalGridLinesCheckBoxCheckedChanged;
            stack.Add(showVerticalGridLinesCheckBox);



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

This example demonstrates how to modify the legend appearance.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Legend Appearance", "https://www.nevron.com/docs/openvision/chart-legend-appearance.html"), ("Overview", "https://www.nevron.com/docs/openvision/chart-legeng-overview.html"), }; } #endregion #region Implementation private void ApplyInterlaceStyles() { NLegendInterlaceStylesCollection interlaceStyles = new NLegendInterlaceStylesCollection(); if (m_HorizontalInterlaceStripesCheckBox.Checked) { NLegendInterlaceStyle horzInterlaceStyle = new NLegendInterlaceStyle(); horzInterlaceStyle.Fill = new NColorFill(NColor.FromColor(NColor.LightBlue, 0.5f)); horzInterlaceStyle.Type = ENLegendInterlaceStyleType.Row; horzInterlaceStyle.Length = 1; horzInterlaceStyle.Interval = 1; interlaceStyles.Add(horzInterlaceStyle); } if (m_VerticalInterlaceStripesCheckBox.Checked) { NLegendInterlaceStyle vertInterlaceStyle = new NLegendInterlaceStyle(); vertInterlaceStyle.Fill = new NColorFill(NColor.FromColor(NColor.DarkGray, 0.5f)); vertInterlaceStyle.Type = ENLegendInterlaceStyleType.Col; vertInterlaceStyle.Length = 1; vertInterlaceStyle.Interval = 1; interlaceStyles.Add(vertInterlaceStyle); } m_Legend.InterlaceStyles = interlaceStyles; } #endregion #region Event Handlers private void OnShowVerticalGridLinesCheckBoxCheckedChanged(NValueChangeEventArgs arg) { if (((NCheckBox)arg.TargetNode).Checked) { m_Legend.ClearLocalValue(NLegend.VerticalGridStrokeProperty); } else { m_Legend.VerticalGridStroke = null; } } private void OnShowHorizontalGridLinesCheckBoxCheckedChanged(NValueChangeEventArgs arg) { if (((NCheckBox)arg.TargetNode).Checked) { m_Legend.ClearLocalValue(NLegend.HorizontalGridStrokeProperty); } else { m_Legend.HorizontalGridStroke = null; } } void OnLegendHeaderTextBoxChanged(NValueChangeEventArgs arg) { NLabel header = new NLabel(((NTextBox)arg.TargetNode).Text); header.HorizontalPlacement = ENHorizontalPlacement.Center; header.TextAlignment = ENContentAlignment.MiddleCenter; header.Font = new NFont("Arimo", 14, ENFontStyle.Bold); m_Legend.Header = header; } void OnLegendFooterTextBoxChanged(NValueChangeEventArgs arg) { NLabel footer = new NLabel(((NTextBox)arg.TargetNode).Text); footer.HorizontalPlacement = ENHorizontalPlacement.Center; footer.TextAlignment = ENContentAlignment.MiddleCenter; footer.Font = new NFont("Arimo", 14, ENFontStyle.Bold); m_Legend.Footer = footer; } void OnVerticalInterlaceStripesCheckBoxCheckedChanged(NValueChangeEventArgs arg) { ApplyInterlaceStyles(); } void OnHorizontalInterlaceStripesCheckBoxCheckedChanged(NValueChangeEventArgs arg) { ApplyInterlaceStyles(); } #endregion #region Fields NLegend m_Legend; NCheckBox m_HorizontalInterlaceStripesCheckBox; NCheckBox m_VerticalInterlaceStripesCheckBox; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Legend Appearance Example for WPF Solution

VB.NET Legend Appearance 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

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

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

            ' configure title
            chartView.Surface.Titles(0).Text = "Legend Appearance"

            m_Legend = chartView.Surface.Legends(0)
            m_Legend.ExpandMode = ENLegendExpandMode.ColsFixed
            m_Legend.ColCount = 3

            m_Legend.Border = NBorder.CreateFilledBorder(NColor.Black)
            m_Legend.BorderThickness = New NMargins(2)
            m_Legend.BackgroundFill = New NStockGradientFill(NColor.White, NColor.LightGray)
            m_Legend.VerticalPlacement = ENVerticalPlacement.Top

            ' configure chart
            Dim chart = CType(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)

            ' setup a bar series
            Dim bar As NBarSeries = New NBarSeries()
            bar.Name = "Bar Series"
            bar.InflateMargins = True
            bar.UseXValues = False
            bar.LegendView.Mode = ENSeriesLegendMode.DataPoints

            ' add some data to the bar series
            bar.LegendView.Mode = ENSeriesLegendMode.DataPoints
            bar.DataPoints.Add(New NBarDataPoint(18, "C++"))
            bar.DataPoints.Add(New NBarDataPoint(15, "Ruby"))
            bar.DataPoints.Add(New NBarDataPoint(21, "Python"))
            bar.DataPoints.Add(New NBarDataPoint(23, "Java"))
            bar.DataPoints.Add(New NBarDataPoint(27, "Javascript"))
            bar.DataPoints.Add(New NBarDataPoint(29, "C#"))
            bar.DataPoints.Add(New NBarDataPoint(26, "PHP"))
            bar.DataPoints.Add(New NBarDataPoint(17, "Objective C"))
            bar.DataPoints.Add(New NBarDataPoint(24, "SQL"))
            bar.DataPoints.Add(New NBarDataPoint(13, "Object Pascal"))
            bar.DataPoints.Add(New NBarDataPoint(19, "Visual Basic"))
            bar.DataPoints.Add(New NBarDataPoint(16, "Open Edge ABL"))

            chart.Series.Add(bar)

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

            Dim legendHeaderTextBox As NTextBox = New NTextBox()
            legendHeaderTextBox.TextChanged += AddressOf OnLegendHeaderTextBoxChanged
            stack.Add(NPairBox.Create("Header: ", legendHeaderTextBox))

            Dim legendFooterTextBox As NTextBox = New NTextBox()
            legendFooterTextBox.TextChanged += AddressOf OnLegendFooterTextBoxChanged
            stack.Add(NPairBox.Create("Footer: ", legendFooterTextBox))

            m_HorizontalInterlaceStripesCheckBox = New NCheckBox("Horizontal Interlace Stripes")
            Me.m_HorizontalInterlaceStripesCheckBox.CheckedChanged += AddressOf OnVerticalInterlaceStripesCheckBoxCheckedChanged
            stack.Add(m_HorizontalInterlaceStripesCheckBox)

            m_VerticalInterlaceStripesCheckBox = New NCheckBox("Vertical Interlace Stripes")
            Me.m_VerticalInterlaceStripesCheckBox.CheckedChanged += AddressOf OnHorizontalInterlaceStripesCheckBoxCheckedChanged
            stack.Add(m_VerticalInterlaceStripesCheckBox)

            Dim showHorizontalGridLinesCheckBox As NCheckBox = New NCheckBox("Show Horizontal Gridlines")
            showHorizontalGridLinesCheckBox.Checked = True
            showHorizontalGridLinesCheckBox.CheckedChanged += AddressOf OnShowHorizontalGridLinesCheckBoxCheckedChanged
            stack.Add(showHorizontalGridLinesCheckBox)

            Dim showVerticalGridLinesCheckBox As NCheckBox = New NCheckBox("Show Vertical Gridlines")
            showVerticalGridLinesCheckBox.Checked = True
            showVerticalGridLinesCheckBox.CheckedChanged += AddressOf OnShowVerticalGridLinesCheckBoxCheckedChanged
            stack.Add(showVerticalGridLinesCheckBox)



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

This example demonstrates how to modify the legend appearance.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Legend Appearance", "https://www.nevron.com/docs/openvision/chart-legend-appearance.html"), ("Overview", "https://www.nevron.com/docs/openvision/chart-legeng-overview.html")} End Function #End Region #Region "Implementation" Private Sub ApplyInterlaceStyles() Dim interlaceStyles As NLegendInterlaceStylesCollection = New NLegendInterlaceStylesCollection() If m_HorizontalInterlaceStripesCheckBox.Checked Then Dim horzInterlaceStyle As NLegendInterlaceStyle = New NLegendInterlaceStyle() horzInterlaceStyle.Fill = New NColorFill(NColor.FromColor(NColor.LightBlue, 0.5F)) horzInterlaceStyle.Type = ENLegendInterlaceStyleType.Row horzInterlaceStyle.Length = 1 horzInterlaceStyle.Interval = 1 interlaceStyles.Add(horzInterlaceStyle) End If If m_VerticalInterlaceStripesCheckBox.Checked Then Dim vertInterlaceStyle As NLegendInterlaceStyle = New NLegendInterlaceStyle() vertInterlaceStyle.Fill = New NColorFill(NColor.FromColor(NColor.DarkGray, 0.5F)) vertInterlaceStyle.Type = ENLegendInterlaceStyleType.Col vertInterlaceStyle.Length = 1 vertInterlaceStyle.Interval = 1 interlaceStyles.Add(vertInterlaceStyle) End If m_Legend.InterlaceStyles = interlaceStyles End Sub #End Region #Region "Event Handlers" Private Sub OnShowVerticalGridLinesCheckBoxCheckedChanged(arg As NValueChangeEventArgs) If CType(arg.TargetNode, NCheckBox).Checked Then m_Legend.ClearLocalValue(NLegend.VerticalGridStrokeProperty) Else m_Legend.VerticalGridStroke = Nothing End If End Sub Private Sub OnShowHorizontalGridLinesCheckBoxCheckedChanged(arg As NValueChangeEventArgs) If CType(arg.TargetNode, NCheckBox).Checked Then m_Legend.ClearLocalValue(NLegend.HorizontalGridStrokeProperty) Else m_Legend.HorizontalGridStroke = Nothing End If End Sub Private Sub OnLegendHeaderTextBoxChanged(arg As NValueChangeEventArgs) Dim header As NLabel = New NLabel(CType(arg.TargetNode, NTextBox).Text) header.HorizontalPlacement = ENHorizontalPlacement.Center header.TextAlignment = ENContentAlignment.MiddleCenter header.Font = New NFont("Arimo", 14, ENFontStyle.Bold) m_Legend.Header = header End Sub Private Sub OnLegendFooterTextBoxChanged(arg As NValueChangeEventArgs) Dim footer As NLabel = New NLabel(CType(arg.TargetNode, NTextBox).Text) footer.HorizontalPlacement = ENHorizontalPlacement.Center footer.TextAlignment = ENContentAlignment.MiddleCenter footer.Font = New NFont("Arimo", 14, ENFontStyle.Bold) m_Legend.Footer = footer End Sub Private Sub OnVerticalInterlaceStripesCheckBoxCheckedChanged(arg As NValueChangeEventArgs) ApplyInterlaceStyles() End Sub Private Sub OnHorizontalInterlaceStripesCheckBoxCheckedChanged(arg As NValueChangeEventArgs) ApplyInterlaceStyles() End Sub #End Region #Region "Fields" Private m_Legend As NLegend Private m_HorizontalInterlaceStripesCheckBox As NCheckBox Private m_VerticalInterlaceStripesCheckBox As NCheckBox #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 Appearance Example for WPF Solution

Related Documentation Links