This example demonstrates how automatic data label layout works with line data labels.

| 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 - NLineLabelsExample";
// 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 = "Line Labels";
// configure chart
m_Chart = (NCartesianChart)chartView.Surface.Charts[0];
m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear);
// configure Y axis
NLinearScale scaleY = (NLinearScale)m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale;
scaleY.MajorGridLines.Stroke.DashStyle = ENDashStyle.Dot;
// add interlaced stripe for Y axis
NScaleStrip strip = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
strip.Interlaced = true;
scaleY.Strips.Add(strip);
// line series
m_Line = new NLineSeries();
m_Chart.Series.Add(m_Line);
m_Line.InflateMargins = true;
NMarkerStyle markerStyle = new NMarkerStyle();
markerStyle.Visible = true;
markerStyle.Fill = new NColorFill(NColor.DarkOrange);
markerStyle.Shape = ENPointShape3D.Ellipse;
markerStyle.Size = new NSize(2, 2);
m_Line.MarkerStyle = markerStyle;
m_Line.ValueFormatter = new NNumericValueFormatter("0.000");
NDataLabelStyle dataLabelStyle = new NDataLabelStyle();
dataLabelStyle.Visible = true;
dataLabelStyle.VertAlign = ENVerticalAlignment.Top;
dataLabelStyle.ArrowLength = 10;
dataLabelStyle.ArrowStroke = new NStroke(NColor.DarkOrange);
dataLabelStyle.TextStyle.Background.Border = new NStroke(NColor.DarkOrange);
dataLabelStyle.Format = "";
m_Line.DataLabelStyle = dataLabelStyle;
m_Chart.LabelLayout.EnableInitialPositioning = true;
m_Chart.LabelLayout.EnableLabelAdjustment = true;
m_Line.LabelLayout.EnableDataPointSafeguard = true;
m_Line.LabelLayout.DataPointSafeguardSize = new NSize(2, 2);
m_Line.LabelLayout.UseLabelLocations = true;
m_Line.LabelLayout.OutOfBoundsLocationMode = ENOutOfBoundsLocationMode.PushWithinBounds;
m_Line.LabelLayout.InvertLocationsIfIgnored = true;
// fill with random data
OnGenerateDataButtonClick(null);
return chartViewWithCommandBars;
}
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);
NCheckBox enableInitialPositioningCheckBox = new NCheckBox("Enable Initial Positioning");
enableInitialPositioningCheckBox.CheckedChanged += OnEnableInitialPositioningCheckBoxCheckedChanged;
stack.Add(enableInitialPositioningCheckBox);
NCheckBox enableLabelAdjustmentCheckBox = new NCheckBox("Enable Label Adjustment");
enableLabelAdjustmentCheckBox.CheckedChanged += OnEnableLabelAdjustmentCheckBoxCheckedChanged;
stack.Add(enableLabelAdjustmentCheckBox);
NButton generateDataButton = new NButton("Generate Data");
generateDataButton.Click += OnGenerateDataButtonClick;
stack.Add(generateDataButton);
enableInitialPositioningCheckBox.Checked = true;
enableLabelAdjustmentCheckBox.Checked = true;
return boxGroup;
}
public string GetExampleDescription()
{
return @"This example demonstrates how automatic data label layout works with line data labels.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Line Series", "https://www.nevron.com/docs/openvision/line-chart-series.html"),
("Series Label Layout", "https://www.nevron.com/docs/openvision/chart-series-label-layout.html"),
("Series Labels Appearance", "https://www.nevron.com/docs/openvision/chart-series-labels-appearance.html"),
("Label Formatting", "https://www.nevron.com/docs/openvision/chart-label-formatting.html"),
};
}
#endregion
#region Implementation
#endregion
#region Event Handlers
void OnEnableLabelAdjustmentCheckBoxCheckedChanged(NValueChangeEventArgs arg)
{
m_Chart.LabelLayout.EnableLabelAdjustment = ((NCheckBox)arg.TargetNode).Checked;
}
void OnEnableInitialPositioningCheckBoxCheckedChanged(NValueChangeEventArgs arg)
{
m_Chart.LabelLayout.EnableInitialPositioning = ((NCheckBox)arg.TargetNode).Checked;
}
void OnGenerateDataButtonClick(NEventArgs arg)
{
m_Line.DataPoints.Clear();
Random random = new Random();
for (int i = 0; i < 30; i++)
{
double value = Math.Sin(i * 0.2) * 10 + random.NextDouble() * 2;
m_Line.DataPoints.Add(new NLineDataPoint(value));
}
}
#endregion
#region Fields
NCartesianChart m_Chart;
NLineSeries m_Line;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
}