Descriptive statistics is a set of brief descriptive coefficients that summarize a given data set representative of an entire or sample population.
In this example we demonstrate how to calculate the Mean, Median and Standard Deviation of a set of values, represented by the Line Series.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Dom;
using Nevron.Nov.Formulas;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;
namespace NovWinFormsProject
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
// Set form title
Text = "NOV WinForms - NDescriptiveStatisticsExample";
// 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 = "Descriptive Statistics";
// configure chart
m_Chart = (NCartesianChart)chartView.Surface.Charts[0];
m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear);
// configure legend
m_Chart.Legend.Mode = ENLegendMode.Custom;
// add interlace stripe
NLinearScale linearScale = m_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);
// setup a bar series
m_LineSeries = new NLineSeries();
m_LineSeries.Name = "Line Series";
m_LineSeries.InflateMargins = false;
m_LineSeries.UseXValues = false;
m_LineSeries.Stroke = new NStroke(3, LineColor);
// add some data to the bar series
m_Chart.Series.Add(m_LineSeries);
// create median const line
m_MedianConstLine = new NAxisReferenceLine();
m_MedianConstLine.Stroke = new NStroke(2, MedianColor);
m_Chart.Axes[ENCartesianAxis.PrimaryY].ReferenceLines.Add(m_MedianConstLine);
// create mean const line
m_MeanConstLine = new NAxisReferenceLine();
m_MeanConstLine.Stroke = new NStroke(2, MeanColor);
m_Chart.Axes[ENCartesianAxis.PrimaryY].ReferenceLines.Add(m_MeanConstLine);
// create standard deviation stripe
m_StandardDeviationStripe = new NAxisStripe();
m_StandardDeviationStripe.Fill = new NColorFill(StdDevColor);
m_Chart.Axes[ENCartesianAxis.PrimaryY].Stripes.Add(m_StandardDeviationStripe);
return chartViewWithCommandBars;
}
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);
// new positive data button
NButton newPositiveDataButton = new NButton("New Positive Data");
newPositiveDataButton.Click += (e) =>
{
// fill with random data
RandomData(false);
};
stack.Add(newPositiveDataButton);
// new positive & negative data button
NButton newPositiveAndNegativeDataButton = new NButton("New Positive & Negative Data");
newPositiveAndNegativeDataButton.Click += (e) =>
{
// fill with random data
RandomData(true);
};
stack.Add(newPositiveAndNegativeDataButton);
// fill with random data
RandomData(true);
return boxGroup;
}
public string GetExampleDescription()
{
return @"
Descriptive statistics is a set of brief descriptive coefficients that summarize a given data set representative of an entire or sample population.
In this example we demonstrate how to calculate the Mean, Median and Standard Deviation of a set of values, represented by the Line Series.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Chart Data Series", "https://www.nevron.com/docs/openvision/chart-data-series.html"),
("Statistical Functions", "https://www.nevron.com/docs/openvision/formula-statistical-functions.html"),
("Formulas Overview", "https://www.nevron.com/docs/openvision/formula-overview.html"),
("Array Functions", "https://www.nevron.com/docs/openvision/formula-array-functions.html"),
};
}
#endregion
#region Implementation
private void RandomData(bool positiveAndNegative)
{
double[] randomData;
if (positiveAndNegative)
{
randomData = CreateRandomData(20, -100, 100);
}
else
{
randomData = CreateRandomData(20, 0, 100);
}
m_LineSeries.DataPoints.Clear();
for (int i = 0; i < randomData.Length; i++)
{
m_LineSeries.DataPoints.Add(new NLineDataPoint(randomData[i]));
}
CalculateStatistics();
}
void CalculateStatistics()
{
// create a chart formula calculator in which:
// LineValues variable represents m_LineSeries Values
NFormulaCalculator calculator = new NFormulaCalculator();
calculator.Variables["LineValues"] = m_LineSeries.DataPoints.GetDataSeriesVariant("Value");
// calculate the mean (average)
calculator.Formula = "AVG(LineValues)";
double mean = (double)calculator.Evaluate();
m_MeanConstLine.Value = mean;
// calculate median
calculator.Formula = "MEDIAN(LineValues)";
double median = (double)calculator.Evaluate();
m_MedianConstLine.Value = median;
// calculate standard deviation
calculator.Formula = "STDDEV(LineValues, false)";
double stdDev = (double)calculator.Evaluate();
m_StandardDeviationStripe.Range = new NRange(mean - stdDev, mean + stdDev);
// update legend
m_Chart.Legend.Items.Clear();
CreateLegendItem(ENLegendMarkShape.Rectangle, "Mean: " + mean.ToString("G2"), MeanColor);
CreateLegendItem(ENLegendMarkShape.Rectangle, "Median: " + median.ToString("G2"), MedianColor);
CreateLegendItem(ENLegendMarkShape.Rectangle, "Standard Deviation: " + stdDev.ToString("G2"), StdDevColor);
}
private void CreateLegendItem(ENLegendMarkShape shape, string text, NColor color)
{
NWidget symbol;
if (shape == ENLegendMarkShape.Line)
{
symbol = NLegend.CreateLegendSymbol(
shape,
new NSize(20, 20),
new NMargins(2),
null,
new NStroke(NColor.Black),
new NStroke(color));
}
else
{
symbol = NLegend.CreateLegendSymbol(
shape,
new NSize(20, 20),
new NMargins(2),
new NColorFill(color),
new NStroke(NColor.Black),
new NStroke(NColor.Black));
}
NLabel label = new NLabel(text);
label.Margins = new NMargins(2);
m_Chart.Legend.Items.Add(new NPairBox(symbol, label));
}
private NColor MeanColor
{
get
{
return NChartPalette.FreshPalette[0];
}
}
private NColor MedianColor
{
get
{
return NChartPalette.FreshPalette[1];
}
}
private NColor StdDevColor
{
get
{
return new NColor(NChartPalette.FreshPalette[2], 128);
}
}
private NColor LineColor
{
get
{
return NChartPalette.FreshPalette[3];
}
}
#endregion
#region Event Handlers
#endregion
#region Fields
NLineSeries m_LineSeries;
NAxisReferenceLine m_MedianConstLine;
NAxisReferenceLine m_MeanConstLine;
NAxisStripe m_StandardDeviationStripe;
NCartesianChart m_Chart;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
} | Language: VB.NET |
Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Formulas
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
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 - NDescriptiveStatisticsExample"
' 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 = "Descriptive Statistics"
' configure chart
m_Chart = CType(chartView.Surface.Charts(0), NCartesianChart)
m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear)
' configure legend
m_Chart.Legend.Mode = ENLegendMode.Custom
' add interlace stripe
Dim linearScale As NLinearScale = TryCast(m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale, NLinearScale)
Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(ENNamedColor.Beige), Nothing, True, 0, 0, 1, 1)
strip.Interlaced = True
linearScale.Strips.Add(strip)
' setup a bar series
m_LineSeries = New NLineSeries()
m_LineSeries.Name = "Line Series"
m_LineSeries.InflateMargins = False
m_LineSeries.UseXValues = False
m_LineSeries.Stroke = New NStroke(3, LineColor)
' add some data to the bar series
m_Chart.Series.Add(m_LineSeries)
' create median const line
m_MedianConstLine = New NAxisReferenceLine()
m_MedianConstLine.Stroke = New NStroke(2, MedianColor)
m_Chart.Axes(ENCartesianAxis.PrimaryY).ReferenceLines.Add(m_MedianConstLine)
' create mean const line
m_MeanConstLine = New NAxisReferenceLine()
m_MeanConstLine.Stroke = New NStroke(2, MeanColor)
m_Chart.Axes(ENCartesianAxis.PrimaryY).ReferenceLines.Add(m_MeanConstLine)
' create standard deviation stripe
m_StandardDeviationStripe = New NAxisStripe()
m_StandardDeviationStripe.Fill = New NColorFill(StdDevColor)
m_Chart.Axes(ENCartesianAxis.PrimaryY).Stripes.Add(m_StandardDeviationStripe)
Return chartViewWithCommandBars
End Function
Public Function CreateExampleControls() As NWidget
Dim stack As NStackPanel = New NStackPanel()
Dim boxGroup As NUniSizeBoxGroup = New NUniSizeBoxGroup(stack)
' new positive data button
Dim newPositiveDataButton As NButton = New NButton("New Positive Data")
' fill with random data
newPositiveDataButton.Click += Sub(e) RandomData(False)
stack.Add(newPositiveDataButton)
' new positive & negative data button
Dim newPositiveAndNegativeDataButton As NButton = New NButton("New Positive & Negative Data")
' fill with random data
newPositiveAndNegativeDataButton.Click += Sub(e) RandomData(True)
stack.Add(newPositiveAndNegativeDataButton)
' fill with random data
RandomData(True)
Return boxGroup
End Function
Public Function GetExampleDescription() As String
Return "
Descriptive statistics is a set of brief descriptive coefficients that summarize a given data set representative of an entire or sample population.
In this example we demonstrate how to calculate the Mean, Median and Standard Deviation of a set of values, represented by the Line Series.
"
End Function
'''
''' Gets the relevant documentation links for the example
'''
'''
Public Function GetDocumentationUrls() As (String, String)()
Return {("Chart Data Series", "https://www.nevron.com/docs/openvision/chart-data-series.html"), ("Statistical Functions", "https://www.nevron.com/docs/openvision/formula-statistical-functions.html"), ("Formulas Overview", "https://www.nevron.com/docs/openvision/formula-overview.html"), ("Array Functions", "https://www.nevron.com/docs/openvision/formula-array-functions.html")}
End Function
#End Region
#Region "Implementation"
Private Sub RandomData(positiveAndNegative As Boolean)
Dim lRandomData As Double()
If positiveAndNegative Then
lRandomData = CreateRandomData(20, -100, 100)
Else
lRandomData = CreateRandomData(20, 0, 100)
End If
m_LineSeries.DataPoints.Clear()
For i = 0 To lRandomData.Length - 1
m_LineSeries.DataPoints.Add(New NLineDataPoint(lRandomData(i)))
Next
CalculateStatistics()
End Sub
Private Sub CalculateStatistics()
' create a chart formula calculator in which:
' LineValues variable represents m_LineSeries Values
Dim calculator As NFormulaCalculator = New NFormulaCalculator()
calculator.Variables("LineValues") = m_LineSeries.DataPoints.GetDataSeriesVariant("Value")
' calculate the mean (average)
calculator.Formula = "AVG(LineValues)"
Dim mean As Double = CDbl(calculator.Evaluate())
m_MeanConstLine.Value = mean
' calculate median
calculator.Formula = "MEDIAN(LineValues)"
Dim median As Double = CDbl(calculator.Evaluate())
m_MedianConstLine.Value = median
' calculate standard deviation
calculator.Formula = "STDDEV(LineValues, false)"
Dim stdDev As Double = CDbl(calculator.Evaluate())
m_StandardDeviationStripe.Range = New NRange(mean - stdDev, mean + stdDev)
' update legend
m_Chart.Legend.Items.Clear()
CreateLegendItem(ENLegendMarkShape.Rectangle, "Mean: " & mean.ToString("G2"), MeanColor)
CreateLegendItem(ENLegendMarkShape.Rectangle, "Median: " & median.ToString("G2"), MedianColor)
CreateLegendItem(ENLegendMarkShape.Rectangle, "Standard Deviation: " & stdDev.ToString("G2"), StdDevColor)
End Sub
Private Sub CreateLegendItem(shape As ENLegendMarkShape, text As String, color As NColor)
Dim symbol As NWidget
If shape Is ENLegendMarkShape.Line Then
symbol = NLegend.CreateLegendSymbol(shape, New NSize(20, 20), New NMargins(2), Nothing, New NStroke(NColor.Black), New NStroke(color))
Else
symbol = NLegend.CreateLegendSymbol(shape, New NSize(20, 20), New NMargins(2), New NColorFill(color), New NStroke(NColor.Black), New NStroke(NColor.Black))
End If
Dim label As NLabel = New NLabel(text)
label.Margins = New NMargins(2)
m_Chart.Legend.Items.Add(New NPairBox(symbol, label))
End Sub
Private ReadOnly Property MeanColor As NColor
Get
Return NChartPalette.FreshPalette(0)
End Get
End Property
Private ReadOnly Property MedianColor As NColor
Get
Return NChartPalette.FreshPalette(1)
End Get
End Property
Private ReadOnly Property StdDevColor As NColor
Get
Return New NColor(NChartPalette.FreshPalette(2), 128)
End Get
End Property
Private ReadOnly Property LineColor As NColor
Get
Return NChartPalette.FreshPalette(3)
End Get
End Property
#End Region
#Region "Event Handlers"
#End Region
#Region "Fields"
Private m_LineSeries As NLineSeries
Private m_MedianConstLine As NAxisReferenceLine
Private m_MeanConstLine As NAxisReferenceLine
Private m_StandardDeviationStripe As NAxisStripe
Private m_Chart As NCartesianChart
#End Region
#Region "Static Methods"
#End Region
#Region "Constants"
#End Region
#Region "Nested Types"
#End Region
End Class