This example demonstrates how to position the legend.

| 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 - NLegendPositionExample";
// 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";
NLegend legend = m_ChartView.Surface.Legends[0];
legend.BackgroundFill = new NColorFill(NColor.White);
// 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);
Random random = new Random();
for (int i = 0; i < 5; i++)
{
bar1.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 legendDockAreaComboBox = new NComboBox();
legendDockAreaComboBox.Items.FillFromEnum();
legendDockAreaComboBox.SelectedIndexChanged += OnLegendDockAreaComboBoxSelectedIndexChanged;
legendDockAreaComboBox.SelectedIndex = (int)ENDockArea.Right;
stack.Add(NPairBox.Create("Dock Area: ", legendDockAreaComboBox));
NCheckBox dockInsideChartPlotCheckBox = new NCheckBox("Dock in Chart Plot Area");
dockInsideChartPlotCheckBox.CheckedChanged += OnDockInsideChartPlotCheckBoxCheckedChanged;
stack.Add(dockInsideChartPlotCheckBox);
return boxGroup;
}
public string GetExampleDescription()
{
return @"This example demonstrates how to position the legend.
";
}
///
/// 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
private void OnDockInsideChartPlotCheckBoxCheckedChanged(NValueChangeEventArgs arg)
{
NLegend legend = m_ChartView.Surface.Legends[0];
NCartesianChart chart = (NCartesianChart)m_ChartView.Surface.Charts[0];
legend.ParentNode.RemoveChild(legend);
if (((NCheckBox)arg.TargetNode).Checked)
{
// dock the legend inside the chart
NDockPanel dockPanel = new NDockPanel();
chart.Content = dockPanel;
dockPanel.Add(legend);
}
else
{
// dock the legend inside the chart
NDockPanel content = m_ChartView.Surface.Content as NDockPanel;
content.Add(legend);
}
}
private void OnLegendDockAreaComboBoxSelectedIndexChanged(NValueChangeEventArgs arg)
{
ENDockArea dockArea = (ENDockArea)((NComboBox)arg.TargetNode).SelectedIndex;
NLegend legend = m_ChartView.Surface.Legends[0];
// adjust the legend layout / position accordingly to the dock area
switch (dockArea)
{
case ENDockArea.Left:
legend.ExpandMode = ENLegendExpandMode.RowsOnly;
legend.VerticalPlacement = ENVerticalPlacement.Center;
break;
case ENDockArea.Top:
legend.ExpandMode = ENLegendExpandMode.ColsOnly;
legend.HorizontalPlacement = ENHorizontalPlacement.Center;
break;
case ENDockArea.Right:
legend.ExpandMode = ENLegendExpandMode.RowsOnly;
legend.VerticalPlacement = ENVerticalPlacement.Center;
break;
case ENDockArea.Bottom:
legend.ExpandMode = ENLegendExpandMode.ColsOnly;
legend.HorizontalPlacement = ENHorizontalPlacement.Center;
break;
case ENDockArea.Center:
legend.ExpandMode = ENLegendExpandMode.RowsOnly;
legend.HorizontalPlacement = ENHorizontalPlacement.Center;
legend.VerticalPlacement = ENVerticalPlacement.Center;
break;
}
NDockLayout.SetDockArea(legend, dockArea);
}
#endregion
#region Fields
private NChartView m_ChartView;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
}