This example demonstrates how to add custom legend items.

| 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
}
}