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

Interactive Legend Example for WPF Description

This example demonstrates how to create an interactive legend.


Interactive Legend Example

C# Interactive Legend 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 - NInteractiveLegendExample";

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

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

			// add interlace stripe
			NLinearScale linearScale = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryY].Scale;
			NScaleStrip stripStyle = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
			stripStyle.Interlaced = true;
			linearScale.Strips.Add(stripStyle);

			// add the first bar
            NBarSeries bar1 = new NBarSeries();
			bar1.Name = "Bar1";
			bar1.MultiBarMode = ENMultiBarMode.Series;
            bar1.LegendView.Mode = ENSeriesLegendMode.SeriesVisibility;
			chart.Series.Add(bar1);

			// add the second bar
            NBarSeries bar2 = new NBarSeries();
			bar2.Name = "Bar2";
			bar2.MultiBarMode = ENMultiBarMode.Stacked;
            bar2.LegendView.Mode = ENSeriesLegendMode.SeriesVisibility;
			chart.Series.Add(bar2);

			// add the third bar
            NBarSeries bar3 = new NBarSeries();
			bar3.Name = "Bar3";
			bar3.MultiBarMode = ENMultiBarMode.Stacked;
            bar3.LegendView.Mode = ENSeriesLegendMode.SeriesVisibility;
			chart.Series.Add(bar3);

			// setup value formatting
			bar1.ValueFormatter = new NNumericValueFormatter("0.###");
			bar2.ValueFormatter = new NNumericValueFormatter("0.###");
			bar3.ValueFormatter = new NNumericValueFormatter("0.###");

			// position data labels in the center of the bars
			bar1.DataLabelStyle = CreateDataLabelStyle();
			bar2.DataLabelStyle = CreateDataLabelStyle();
			bar3.DataLabelStyle = CreateDataLabelStyle();

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

			// pass some random data
            Random random = new Random();
            for (int i = 0; i < 12; i++)
            {
                bar1.DataPoints.Add(new NBarDataPoint(random.Next(90) + 10));
                bar2.DataPoints.Add(new NBarDataPoint(random.Next(90) + 10));
                bar3.DataPoints.Add(new NBarDataPoint(random.Next(90) + 10));
            }

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

This example demonstrates how to create an interactive legend.

"; } /// /// 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"), ("Hit Testing", "https://www.nevron.com/docs/openvision/chart-interactivity-hit-testing.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), ("Legend Layout", "https://www.nevron.com/docs/openvision/chart-legend-layout.html"), }; } #endregion #region Implementation /// /// Creates a new data label style object /// /// private NDataLabelStyle CreateDataLabelStyle() { NDataLabelStyle dataLabelStyle = new NDataLabelStyle(); dataLabelStyle.VertAlign = ENVerticalAlignment.Center; dataLabelStyle.ArrowLength = 0; return dataLabelStyle; } #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) Interactive Legend Example for WPF Solution

VB.NET Interactive Legend 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 - NInteractiveLegendExample"

		' 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 = "Interactive Legend"

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

            ' add interlace stripe
            Dim linearScale As NLinearScale = chart.Axes(ENCartesianAxis.PrimaryY).Scale
            Dim stripStyle As NScaleStrip = New NScaleStrip(New NColorFill(NColor.Beige), Nothing, True, 0, 0, 1, 1)
            stripStyle.Interlaced = True
            linearScale.Strips.Add(stripStyle)

            ' add the first bar
            Dim bar1 As NBarSeries = New NBarSeries()
            bar1.Name = "Bar1"
            bar1.MultiBarMode = ENMultiBarMode.Series
            bar1.LegendView.Mode = ENSeriesLegendMode.SeriesVisibility
            chart.Series.Add(bar1)

            ' add the second bar
            Dim bar2 As NBarSeries = New NBarSeries()
            bar2.Name = "Bar2"
            bar2.MultiBarMode = ENMultiBarMode.Stacked
            bar2.LegendView.Mode = ENSeriesLegendMode.SeriesVisibility
            chart.Series.Add(bar2)

            ' add the third bar
            Dim bar3 As NBarSeries = New NBarSeries()
            bar3.Name = "Bar3"
            bar3.MultiBarMode = ENMultiBarMode.Stacked
            bar3.LegendView.Mode = ENSeriesLegendMode.SeriesVisibility
            chart.Series.Add(bar3)

            ' setup value formatting
            bar1.ValueFormatter = New NNumericValueFormatter("0.###")
            bar2.ValueFormatter = New NNumericValueFormatter("0.###")
            bar3.ValueFormatter = New NNumericValueFormatter("0.###")

            ' position data labels in the center of the bars
            bar1.DataLabelStyle = CreateDataLabelStyle()
            bar2.DataLabelStyle = CreateDataLabelStyle()
            bar3.DataLabelStyle = CreateDataLabelStyle()

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

            ' pass some random data
            Dim random As Random = New Random()
            For i = 0 To 11
                bar1.DataPoints.Add(New NBarDataPoint(random.Next(90) + 10))
                bar2.DataPoints.Add(New NBarDataPoint(random.Next(90) + 10))
                bar3.DataPoints.Add(New NBarDataPoint(random.Next(90) + 10))
            Next

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

This example demonstrates how to create an interactive legend.

" 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"), ("Hit Testing", "https://www.nevron.com/docs/openvision/chart-interactivity-hit-testing.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), ("Legend Layout", "https://www.nevron.com/docs/openvision/chart-legend-layout.html")} End Function #End Region #Region "Implementation" ''' ''' Creates a new data label style object ''' ''' Private Function CreateDataLabelStyle() As NDataLabelStyle Dim dataLabelStyle As NDataLabelStyle = New NDataLabelStyle() dataLabelStyle.VertAlign = ENVerticalAlignment.Center dataLabelStyle.ArrowLength = 0 Return dataLabelStyle 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) Interactive Legend Example for WPF Solution