Aggregate Functions Example for WinForms

Nevron NOV Chart for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms Chart examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Aggregate Functions Example for WinForms Description

This example demonstrates the most commonly used aggregate functions (SUM, MIN, MAX etc.).

Aggregate functions take as input an array of values, and produce a single resulting value.

In this example the Green bar series represent the values, and the Red constant line represents the aggregate value for the currently selected function


Aggregate Functions Example

C# Aggregate Functions Example Code for Nevron Chart for WinForms
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.Diagnostics;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NAggregateFunctionsExample";

			// 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 = "Aggregate Functions";

            // configure chart
            NCartesianChart chart = (NCartesianChart)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);

            // setup a bar series
            m_Bar = new NBarSeries();
            m_Bar.Name = "Bar Series";
            m_Bar.InflateMargins = true;
            m_Bar.UseXValues = false;
            m_Bar.Shadow = new NShadow(NColor.LightGray, 2, 2);

            // add some data to the bar series
            m_Bar.LegendView.Mode = ENSeriesLegendMode.DataPoints;
            m_Bar.DataPoints.Add(new NBarDataPoint(18, "C++"));
            m_Bar.DataPoints.Add(new NBarDataPoint(15, "Ruby"));
            m_Bar.DataPoints.Add(new NBarDataPoint(21, "Python"));
            m_Bar.DataPoints.Add(new NBarDataPoint(23, "Java"));
            m_Bar.DataPoints.Add(new NBarDataPoint(27, "Javascript"));
            m_Bar.DataPoints.Add(new NBarDataPoint(29, "C#"));
            m_Bar.DataPoints.Add(new NBarDataPoint(26, "PHP"));
            chart.Series.Add(m_Bar);

            // create const line to show the result of the function
            m_ConstLine = new NAxisReferenceLine();
            m_ConstLine.Stroke = new NStroke(2, NColor.Red);
            m_ConstLine.PaintAfterChartContent = true;
            m_ConstLine.IncludeValueInAxisRange = true;
            chart.Axes[ENCartesianAxis.PrimaryY].ReferenceLines.Add(m_ConstLine);

            return chartViewWithCommandBars;
        }
        public NWidget CreateExampleControls()
        {
            NStackPanel stack = new NStackPanel();
            NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);

            // function combo
            m_FunctionComboBox = new NComboBox();
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Average"));
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Median"));
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Min"));
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Max"));
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Sum"));
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Standard Deviation"));
            m_FunctionComboBox.Items.Add(new NComboBoxItem("Variance"));

            m_FunctionComboBox.SelectedIndex = 0;
            m_FunctionComboBox.SelectedIndexChanged += OnFunctionComboBoxSelectedIndexChanged;
            stack.Add(NPairBox.Create("Function: ", m_FunctionComboBox));

            // new positive data button
            NButton newPositiveDataButton = new NButton("New Positive Data");
            newPositiveDataButton.Click += (e) =>
            {
                // fill with random data
                RandomData(false);

                // calculate function
                CalculateFunction();
            };
            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);

                // calculate function
                CalculateFunction();
            };
            stack.Add(newPositiveAndNegativeDataButton);

            CalculateFunction();

            return boxGroup;
        }
        public string GetExampleDescription()
        {
            return @"

This example demonstrates the most commonly used aggregate functions (SUM, MIN, MAX etc.).

Aggregate functions take as input an array of values, and produce a single resulting value.

In this example the Green bar series represent the values, and the Red constant line represents the aggregate value for the currently selected function

"; } /// /// 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(m_Bar.DataPoints.Count, -100, 100); } else { randomData = CreateRandomData(m_Bar.DataPoints.Count, 0, 100); } for (int i = 0; i < m_Bar.DataPoints.Count; i++) { m_Bar.DataPoints[i].Value = randomData[i]; } } void CalculateFunction() { string formula; switch (m_FunctionComboBox.SelectedIndex) { case 0: // average formula = "AVG(BarValues)"; break; case 1: // median formula = "MEDIAN(BarValues)"; break; case 2: // min formula = "MIN(BarValues)"; break; case 3: // max formula = "MAX(BarValues)"; break; case 4: // sum formula = "SUM(BarValues)"; break; case 5: // Standard Deviation formula = "STDDEV(BarValues, true)"; break; case 6: // Variance formula = "VARIANCE(BarValues, true)"; break; default: return; } // create a chart formula calculator in which: // BarValues variable represents m_Bar Values NFormulaCalculator calculator = new NFormulaCalculator(); calculator.Formula = formula; calculator.Variables["BarValues"] = m_Bar.DataPoints.GetDataSeriesVariant("Value"); // calculate the formula. Note that the result of an aggregate function is always a single value variant NVariant result = calculator.Evaluate(); Debug.Assert(!result.IsArray); // display single values as a const line m_ConstLine.Value = result.ToDouble(null); } #endregion #region Event Handlers void OnFunctionComboBoxSelectedIndexChanged(NValueChangeEventArgs arg) { CalculateFunction(); } #endregion #region Fields NBarSeries m_Bar; NAxisReferenceLine m_ConstLine; NComboBox m_FunctionComboBox; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Aggregate Functions Example for WinForms Solution

VB.NET Aggregate Functions Example Code for Nevron Chart for WinForms
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.Forms
Imports System.Diagnostics

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NAggregateFunctionsExample"

        ' 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)
        host.Dock = DockStyle.Fill
        Controls.Add(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 = "Aggregate Functions"

            ' configure chart
            Dim chart = CType(chartView.Surface.Charts(0), NCartesianChart)

            ' add interlace stripe
            Dim linearScale As NLinearScale = TryCast(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_Bar = New NBarSeries()
            m_Bar.Name = "Bar Series"
            m_Bar.InflateMargins = True
            m_Bar.UseXValues = False
            m_Bar.Shadow = New NShadow(NColor.LightGray, 2, 2)

            ' add some data to the bar series
            m_Bar.LegendView.Mode = ENSeriesLegendMode.DataPoints
            m_Bar.DataPoints.Add(New NBarDataPoint(18, "C++"))
            m_Bar.DataPoints.Add(New NBarDataPoint(15, "Ruby"))
            m_Bar.DataPoints.Add(New NBarDataPoint(21, "Python"))
            m_Bar.DataPoints.Add(New NBarDataPoint(23, "Java"))
            m_Bar.DataPoints.Add(New NBarDataPoint(27, "Javascript"))
            m_Bar.DataPoints.Add(New NBarDataPoint(29, "C#"))
            m_Bar.DataPoints.Add(New NBarDataPoint(26, "PHP"))
            chart.Series.Add(m_Bar)

            ' create const line to show the result of the function
            m_ConstLine = New NAxisReferenceLine()
            m_ConstLine.Stroke = New NStroke(2, NColor.Red)
            m_ConstLine.PaintAfterChartContent = True
            m_ConstLine.IncludeValueInAxisRange = True
            chart.Axes(ENCartesianAxis.PrimaryY).ReferenceLines.Add(m_ConstLine)

            Return chartViewWithCommandBars
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            Dim boxGroup As NUniSizeBoxGroup = New NUniSizeBoxGroup(stack)

            ' function combo
            m_FunctionComboBox = New NComboBox()
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Average"))
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Median"))
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Min"))
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Max"))
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Sum"))
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Standard Deviation"))
            m_FunctionComboBox.Items.Add(New NComboBoxItem("Variance"))

            m_FunctionComboBox.SelectedIndex = 0
            Me.m_FunctionComboBox.SelectedIndexChanged += AddressOf OnFunctionComboBoxSelectedIndexChanged
            stack.Add(NPairBox.Create("Function: ", m_FunctionComboBox))

            ' new positive data button
            Dim newPositiveDataButton As NButton = New NButton("New Positive Data")
            newPositiveDataButton.Click += Sub(e)
                                               ' fill with random data
                                               RandomData(False)

                                               ' calculate function
                                               CalculateFunction()
                                           End Sub
            stack.Add(newPositiveDataButton)

            ' new positive & negative data button
            Dim newPositiveAndNegativeDataButton As NButton = New NButton("New Positive & Negative Data")
            newPositiveAndNegativeDataButton.Click += Sub(e)
                                                          ' fill with random data
                                                          RandomData(True)

                                                          ' calculate function
                                                          CalculateFunction()
                                                      End Sub
            stack.Add(newPositiveAndNegativeDataButton)

            CalculateFunction()

            Return boxGroup
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates the most commonly used aggregate functions (SUM, MIN, MAX etc.).

Aggregate functions take as input an array of values, and produce a single resulting value.

In this example the Green bar series represent the values, and the Red constant line represents the aggregate value for the currently selected function

" 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(m_Bar.DataPoints.Count, -100, 100) Else lRandomData = CreateRandomData(m_Bar.DataPoints.Count, 0, 100) End If For i = 0 To m_Bar.DataPoints.Count - 1 m_Bar.DataPoints(i).Value = lRandomData(i) Next End Sub Private Sub CalculateFunction() Dim formula As String Select Case m_FunctionComboBox.SelectedIndex Case 0 ' average formula = "AVG(BarValues)" Case 1 ' median formula = "MEDIAN(BarValues)" Case 2 ' min formula = "MIN(BarValues)" Case 3 ' max formula = "MAX(BarValues)" Case 4 ' sum formula = "SUM(BarValues)" Case 5 ' Standard Deviation formula = "STDDEV(BarValues, true)" Case 6 ' Variance formula = "VARIANCE(BarValues, true)" Case Else Return End Select ' create a chart formula calculator in which: ' BarValues variable represents m_Bar Values Dim calculator As NFormulaCalculator = New NFormulaCalculator() calculator.Formula = formula calculator.Variables("BarValues") = m_Bar.DataPoints.GetDataSeriesVariant("Value") ' calculate the formula. Note that the result of an aggregate function is always a single value variant Dim result As NVariant = calculator.Evaluate() Debug.Assert(Not result.IsArray) ' display single values as a const line m_ConstLine.Value = result.ToDouble(Nothing) End Sub #End Region #Region "Event Handlers" Private Sub OnFunctionComboBoxSelectedIndexChanged(arg As NValueChangeEventArgs) CalculateFunction() End Sub #End Region #Region "Fields" Private m_Bar As NBarSeries Private m_ConstLine As NAxisReferenceLine Private m_FunctionComboBox As NComboBox #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Aggregate Functions Example for WinForms Solution