Custom Legend Items 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.

Custom Legend Items Example for WPF Description

This example demonstrates how to add custom legend items.


Custom Legend Items Example

C# Custom Legend Items 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;
using System.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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.Document.StyleSheets.ApplyTheme(new NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series));

            NDockPanel dockPanel = new NDockPanel();

            // set a chart title
            NLabel header = new NLabel("Legend Custom Items");
            NDockLayout.SetDockArea(header, ENDockArea.Top);
            header.Margins = new NMargins(0, 10, 0, 10);
            dockPanel.AddChild(header);

            NDockPanel container = new NDockPanel();
            NDockLayout.SetDockArea(container, ENDockArea.Center);
            container.Margins = new NMargins(10, 10, 10, 10);
            dockPanel.AddChild(container);

            // configure the legend
            CreateCustomLegend1(container);
            CreateCustomLegend2(container);
            CreateCustomLegend3(container);

            chartView.Surface.Content = dockPanel;

            return chartViewWithCommandBars;
        }
        public NWidget CreateExampleControls()
        {
            return null;
        }
        public string GetExampleDescription()
        {
            return @"

This example demonstrates how to add custom legend items.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Overview", "https://www.nevron.com/docs/openvision/chart-legeng-overview.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), ("Legend Appearance", "https://www.nevron.com/docs/openvision/chart-legend-appearance.html"), ("Legend Layout", "https://www.nevron.com/docs/openvision/chart-legend-layout.html"), }; } #endregion #region Implementation /// /// /// /// private void CreateCustomLegend1(NDockPanel container) { NLegend markShapesLegend = CreateLegend(container, "Mark Shapes"); Array markShapes = Enum.GetValues(typeof(ENLegendMarkShape)); NColor[] palette = NChartPalette.GetColors(ENChartPalette.Fresh); for (int i = 0; i < markShapes.Length; i++) { ENLegendMarkShape markShape = (ENLegendMarkShape)markShapes.GetValue(i); NFill markFill = new NColorFill(palette[i % palette.Length]); NWidget legendItemSymbol = NLegend.CreateLegendSymbol(markShape, new NSize(20, 20), new NMargins(2), markFill, new NStroke(NColor.Black), new NStroke(NColor.Black));; NLabel legendItemLabel = new NLabel(markShape.ToString()); legendItemLabel.Margins = new NMargins(2); markShapesLegend.Items.Add(new NPairBox(legendItemSymbol, legendItemLabel)); } } /// /// /// /// private void CreateCustomLegend2(NDockPanel container) { NLegend markShapesNoStroke = CreateLegend(container, "Mark Shapes (No stroke)"); Array markShapes = Enum.GetValues(typeof(ENLegendMarkShape)); NColor[] palette = NChartPalette.GetColors(ENChartPalette.Fresh); for (int i = 0; i < markShapes.Length; i++) { ENLegendMarkShape markShape = (ENLegendMarkShape)markShapes.GetValue(i); NFill markFill = new NColorFill(palette[i % palette.Length]); NWidget legendItemSymbol = NLegend.CreateLegendSymbol(markShape, new NSize(20, 20), new NMargins(2), markFill, null, null); ; NLabel legendItemLabel = new NLabel(markShape.ToString()); legendItemLabel.Margins = new NMargins(2); markShapesNoStroke.Items.Add(new NPairBox(legendItemSymbol, legendItemLabel)); } } /// /// /// /// private void CreateCustomLegend3(NDockPanel container) { NLegend markShapesBackground = CreateLegend(container, "Mark Shapes (Margins, Background)"); Array markShapes = Enum.GetValues(typeof(ENLegendMarkShape)); NColor[] palette = NChartPalette.GetColors(ENChartPalette.Fresh); for (int i = 0; i < markShapes.Length; i++) { ENLegendMarkShape markShape = (ENLegendMarkShape)markShapes.GetValue(i); NWidget legendItemSymbol = NLegend.CreateLegendSymbol(markShape, new NSize(20, 20), new NMargins(2), new NColorFill(NColor.White), null, null); ; NLabel legendItemLabel = new NLabel(markShape.ToString()); legendItemLabel.TextFill = new NColorFill(NColor.White); legendItemLabel.Font = new NFont("Arial", 10 + i); legendItemLabel.Margins = new NMargins(2); NPairBox legendItem = new NPairBox(legendItemSymbol, legendItemLabel); legendItem.BackgroundFill = new NColorFill(palette[i % palette.Length]); legendItem.Margins = new NMargins(4); markShapesBackground.Items.Add(legendItem); } } /// /// /// /// /// /// private NLegend CreateLegend(NDockPanel container, string title) { // configure the legend NLegend legend = new NLegend(); legend.Header = new NLabel(title); legend.Mode = ENLegendMode.Custom; legend.ExpandMode = ENLegendExpandMode.ColsOnly; NDockLayout.SetDockArea(legend, ENDockArea.Top); container.AddChild(legend); return legend; } #endregion #region Event Handlers #endregion #region Fields #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

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

VB.NET Custom Legend Items 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
Imports System

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

		' 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.Document.StyleSheets.ApplyTheme(New NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series))

            Dim dockPanel As NDockPanel = New NDockPanel()

            ' set a chart title
            Dim header As NLabel = New NLabel("Legend Custom Items")
            NDockLayout.SetDockArea(header, ENDockArea.Top)
            header.Margins = New NMargins(0, 10, 0, 10)
            dockPanel.AddChild(header)

            Dim container As NDockPanel = New NDockPanel()
            NDockLayout.SetDockArea(container, ENDockArea.Center)
            container.Margins = New NMargins(10, 10, 10, 10)
            dockPanel.AddChild(container)

            ' configure the legend
            CreateCustomLegend1(container)
            CreateCustomLegend2(container)
            CreateCustomLegend3(container)

            chartView.Surface.Content = dockPanel

            Return chartViewWithCommandBars
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to add custom legend items.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Overview", "https://www.nevron.com/docs/openvision/chart-legeng-overview.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), ("Legend Appearance", "https://www.nevron.com/docs/openvision/chart-legend-appearance.html"), ("Legend Layout", "https://www.nevron.com/docs/openvision/chart-legend-layout.html")} End Function #End Region #Region "Implementation" ''' ''' ''' ''' Private Sub CreateCustomLegend1(container As NDockPanel) Dim markShapesLegend = CreateLegend(container, "Mark Shapes") Dim markShapes = [Enum].GetValues(GetType(ENLegendMarkShape)) Dim palette = NChartPalette.GetColors(ENChartPalette.Fresh) For i = 0 To markShapes.Length - 1 Dim markShape As ENLegendMarkShape = markShapes.GetValue(i) Dim markFill As NFill = New NColorFill(palette(i Mod palette.Length)) Dim legendItemSymbol As NWidget = NLegend.CreateLegendSymbol(markShape, New NSize(20, 20), New NMargins(2), markFill, New NStroke(NColor.Black), New NStroke(NColor.Black)) Dim legendItemLabel As NLabel = New NLabel(markShape.ToString()) legendItemLabel.Margins = New NMargins(2) markShapesLegend.Items.Add(New NPairBox(legendItemSymbol, legendItemLabel)) Next End Sub ''' ''' ''' ''' Private Sub CreateCustomLegend2(container As NDockPanel) Dim markShapesNoStroke = CreateLegend(container, "Mark Shapes (No stroke)") Dim markShapes = [Enum].GetValues(GetType(ENLegendMarkShape)) Dim palette = NChartPalette.GetColors(ENChartPalette.Fresh) For i = 0 To markShapes.Length - 1 Dim markShape As ENLegendMarkShape = markShapes.GetValue(i) Dim markFill As NFill = New NColorFill(palette(i Mod palette.Length)) Dim legendItemSymbol As NWidget = NLegend.CreateLegendSymbol(markShape, New NSize(20, 20), New NMargins(2), markFill, Nothing, Nothing) Dim legendItemLabel As NLabel = New NLabel(markShape.ToString()) legendItemLabel.Margins = New NMargins(2) markShapesNoStroke.Items.Add(New NPairBox(legendItemSymbol, legendItemLabel)) Next End Sub ''' ''' ''' ''' Private Sub CreateCustomLegend3(container As NDockPanel) Dim markShapesBackground = CreateLegend(container, "Mark Shapes (Margins, Background)") Dim markShapes = [Enum].GetValues(GetType(ENLegendMarkShape)) Dim palette = NChartPalette.GetColors(ENChartPalette.Fresh) For i = 0 To markShapes.Length - 1 Dim markShape As ENLegendMarkShape = markShapes.GetValue(i) Dim legendItemSymbol As NWidget = NLegend.CreateLegendSymbol(markShape, New NSize(20, 20), New NMargins(2), New NColorFill(NColor.White), Nothing, Nothing) Dim legendItemLabel As NLabel = New NLabel(markShape.ToString()) legendItemLabel.TextFill = New NColorFill(NColor.White) legendItemLabel.Font = New NFont("Arial", 10 + i) legendItemLabel.Margins = New NMargins(2) Dim legendItem As NPairBox = New NPairBox(legendItemSymbol, legendItemLabel) legendItem.BackgroundFill = New NColorFill(palette(i Mod palette.Length)) legendItem.Margins = New NMargins(4) markShapesBackground.Items.Add(legendItem) Next End Sub ''' ''' ''' ''' ''' ''' Private Function CreateLegend(container As NDockPanel, title As String) As NLegend ' configure the legend Dim legend As NLegend = New NLegend() legend.Header = New NLabel(title) legend.Mode = ENLegendMode.Custom legend.ExpandMode = ENLegendExpandMode.ColsOnly NDockLayout.SetDockArea(legend, ENDockArea.Top) container.AddChild(legend) Return legend End Function #End Region #Region "Event Handlers" #End Region #Region "Fields" #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) Custom Legend Items Example for WPF Solution