This example demonstrates how to create a stacked area chart.

| 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 - NStackedAreaExample";
// 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 = "Stacked Area";
// configure chart
NCartesianChart chart = (NCartesianChart)m_ChartView.Surface.Charts[0];
// setup X axis
NOrdinalScale scaleX = (NOrdinalScale)chart.Axes[ENCartesianAxis.PrimaryX].Scale;
scaleX.InflateContentRange = false;
// add interlaced stripe for Y axis
NScaleStrip strip = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
strip.Interlaced = true;
NLinearScale scaleY = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryY].Scale;
scaleY.Strips.Add(strip);
// add the first area
m_Area1 = new NAreaSeries();
m_Area1.MultiAreaMode = ENMultiAreaMode.Series;
m_Area1.Name = "Product A";
m_Area1.DataLabelStyle = new NDataLabelStyle();
chart.Series.Add(m_Area1);
SetupDataLabels(m_Area1);
// add the second Area
m_Area2 = new NAreaSeries();
m_Area2.MultiAreaMode = ENMultiAreaMode.Stacked;
m_Area2.Name = "Product B";
m_Area2.DataLabelStyle = new NDataLabelStyle();
chart.Series.Add(m_Area2);
SetupDataLabels(m_Area2);
// add the third Area
m_Area3 = new NAreaSeries();
m_Area3.MultiAreaMode = ENMultiAreaMode.Stacked;
m_Area3.Name = "Product C";
m_Area3.DataLabelStyle = new NDataLabelStyle();
chart.Series.Add(m_Area3);
SetupDataLabels(m_Area3);
// fill with random data
Random random = new Random();
for (int i = 0; i < 10; i++)
{
m_Area1.DataPoints.Add(new NAreaDataPoint(random.Next(20, 50)));
m_Area2.DataPoints.Add(new NAreaDataPoint(random.Next(20, 50)));
m_Area3.DataPoints.Add(new NAreaDataPoint(random.Next(20, 50)));
}
return chartViewWithCommandBars;
}
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);
NComboBox stackModeComboBox = new NComboBox();
stackModeComboBox.Items.Add(new NComboBoxItem("Stacked"));
stackModeComboBox.Items.Add(new NComboBoxItem("Stacked Percent"));
stackModeComboBox.SelectedIndex = 0;
stackModeComboBox.SelectedIndexChanged += OnStackModeComboBoxSelectedIndexChanged;
stack.Add(NPairBox.Create("Stack Mode: ", stackModeComboBox));
NCheckBox showDataLabelsCheckBox = new NCheckBox("Show Data Labels");
showDataLabelsCheckBox.CheckedChanged += OnShowDataLabelsCheckedChanged;
stack.Add(showDataLabelsCheckBox);
m_Area1LabelFormatCombox = CreateLabelsCombo();
m_Area1LabelFormatCombox.SelectedIndexChanged += OnArea1LabelFormatComboxSelectedIndexChanged;
stack.Add(NPairBox.Create("Area 1 Label Format: ", m_Area1LabelFormatCombox));
m_Area2LabelFormatCombox = CreateLabelsCombo();
m_Area2LabelFormatCombox.SelectedIndexChanged += OnArea2LabelFormatComboxSelectedIndexChanged;
stack.Add(NPairBox.Create("Area 2 Label Format: ", m_Area2LabelFormatCombox));
m_Area3LabelFormatCombox = CreateLabelsCombo();
m_Area3LabelFormatCombox.SelectedIndexChanged += OnArea3LabelFormatComboxSelectedIndexChanged;
stack.Add(NPairBox.Create("Area 3 Label Format: ", m_Area3LabelFormatCombox));
showDataLabelsCheckBox.Checked = true;
return boxGroup;
}
public string GetExampleDescription()
{
return @"This example demonstrates how to create a stacked area chart.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Area Series", "https://www.nevron.com/docs/openvision/area-chart-series.html"),
("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"),
("Ordinal Scale", "https://www.nevron.com/docs/openvision/ordinal-scale.html"),
};
}
#endregion
#region Implementation
private string GetFormatStringFromIndex(int index)
{
switch (index)
{
case 0:
return "";
case 1:
return "";
case 2:
return "";
case 3:
return "";
default:
return "";
}
}
private NComboBox CreateLabelsCombo()
{
NComboBox comboBox = new NComboBox();
comboBox.Items.Add(new NComboBoxItem("Value"));
comboBox.Items.Add(new NComboBoxItem("Total"));
comboBox.Items.Add(new NComboBoxItem("Cumulative"));
comboBox.Items.Add(new NComboBoxItem("Percent"));
comboBox.SelectedIndex = 0;
return comboBox;
}
private void SetupDataLabels(NAreaSeries area)
{
NDataLabelStyle dataLabel = area.DataLabelStyle;
dataLabel.ArrowLength = 0;
dataLabel.VertAlign = ENVerticalAlignment.Center;
dataLabel.TextStyle.Background.Padding = new NMargins(5);
dataLabel.TextStyle.Font = new NFont("Arial", 8, ENFontStyle.Bold);
}
#endregion
#region Event Handlers
void OnStackModeComboBoxSelectedIndexChanged(NValueChangeEventArgs arg)
{
// configure chart
NCartesianChart chart = (NCartesianChart)m_ChartView.Surface.Charts[0];
NLinearScale scale = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryY].Scale;
switch (((NComboBox)arg.TargetNode).SelectedIndex)
{
case 0:
m_Area2.MultiAreaMode = ENMultiAreaMode.Stacked;
m_Area3.MultiAreaMode = ENMultiAreaMode.Stacked;
scale.Labels.TextProvider = new NFormattedScaleLabelTextProvider(new NNumericValueFormatter(ENNumericValueFormat.General));
break;
case 1:
m_Area2.MultiAreaMode = ENMultiAreaMode.StackedPercent;
m_Area3.MultiAreaMode = ENMultiAreaMode.StackedPercent;
scale.Labels.TextProvider = new NFormattedScaleLabelTextProvider(new NNumericValueFormatter(ENNumericValueFormat.Percentage));
break;
}
}
void OnShowDataLabelsCheckedChanged(NValueChangeEventArgs arg)
{
NCheckBox showDataLabelsCheckBox = (NCheckBox)arg.TargetNode;
m_Area1.DataLabelStyle.Visible = showDataLabelsCheckBox.Checked;
m_Area2.DataLabelStyle.Visible = showDataLabelsCheckBox.Checked;
m_Area3.DataLabelStyle.Visible = showDataLabelsCheckBox.Checked;
m_Area1LabelFormatCombox.Enabled = showDataLabelsCheckBox.Checked;
m_Area2LabelFormatCombox.Enabled = showDataLabelsCheckBox.Checked;
m_Area3LabelFormatCombox.Enabled = showDataLabelsCheckBox.Checked;
}
void OnArea1LabelFormatComboxSelectedIndexChanged(NValueChangeEventArgs arg)
{
m_Area1.DataLabelStyle.Format = GetFormatStringFromIndex(m_Area1LabelFormatCombox.SelectedIndex);
}
void OnArea2LabelFormatComboxSelectedIndexChanged(NValueChangeEventArgs arg)
{
m_Area2.DataLabelStyle.Format = GetFormatStringFromIndex(m_Area2LabelFormatCombox.SelectedIndex);
}
void OnArea3LabelFormatComboxSelectedIndexChanged(NValueChangeEventArgs arg)
{
m_Area3.DataLabelStyle.Format = GetFormatStringFromIndex(m_Area3LabelFormatCombox.SelectedIndex);
}
#endregion
#region Fields
private NChartView m_ChartView;
private NAreaSeries m_Area1;
private NAreaSeries m_Area2;
private NAreaSeries m_Area3;
private NComboBox m_Area1LabelFormatCombox;
private NComboBox m_Area2LabelFormatCombox;
private NComboBox m_Area3LabelFormatCombox;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
}