Real Time Bar Example for WPF

Nevron NOV Chart for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF 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.

Real Time Bar Example for WPF Description

This example demonstrates how to create a bar chart that updates in real time.


Real Time Bar Example

C# Real Time Bar Example Code for Nevron Chart for WPF
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 - NRealTimeBarExample";

			// 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);

			chartView.Registered += OnChartViewRegistered;
			chartView.Unregistered += OnChartViewUnregistered;

			// configure title
			chartView.Surface.Titles[0].Text = "Real Time Bar";

			// configure chart
			m_Chart = (NCartesianChart)chartView.Surface.Charts[0];

			m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear);

			NLinearScale scaleY = (NLinearScale)m_Chart.Axes[ENCartesianAxis.PrimaryX].Scale;
			scaleY.InflateViewRangeBegin = false;
			scaleY.InflateViewRangeEnd = false;

			// add interlaced stripe to the Y axis
			NScaleStrip strip = new NScaleStrip(new NColorFill(ENNamedColor.Beige), null, true, 0, 0, 1, 1);
			strip.Interlaced = true;
			m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale.Strips.Add(strip);

			m_Random = new Random();

			m_Bar = new NBarSeries();
			m_Bar.Name = "Line Series";
			m_Bar.InflateMargins = true;
			m_Bar.DataLabelStyle = new NDataLabelStyle(false);
			m_Bar.UseXValues = true;
			m_Bar.WidthSizeMode = ENBarSizeMode.Logical;
			m_Bar.LogicalWidth = 0.5;
			m_CurXValue = 0;

			m_Chart.Series.Add(m_Bar);

			chartView.Document.StyleSheets.ApplyTheme(new NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series));

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			NUniSizeBoxGroup group = new NUniSizeBoxGroup(stack);
			
			NButton toggleTimerButton = new NButton("Stop Timer");
			toggleTimerButton.Click += OnToggleTimerButtonClick;
			toggleTimerButton.Tag = 0;
			stack.Add(toggleTimerButton);

			NButton resetButton = new NButton("Reset Data");
			resetButton.Click += OnResetButtonClick;
			stack.Add(resetButton);

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

This example demonstrates how to create a bar chart that updates in real time.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Bar Series", "https://www.nevron.com/docs/openvision/bar-chart-series.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), ("Chart Data Series", "https://www.nevron.com/docs/openvision/chart-data-series.html"), ("Zooming and Scrolling", "https://www.nevron.com/docs/openvision/chart-interactivity-zooming-and-scrolling.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnChartViewRegistered(NEventArgs arg) { m_Timer = new NTimer(); m_Timer.Tick += OnTimerTick; m_Timer.Start(); } private void OnChartViewUnregistered(NEventArgs arg) { m_Timer.Stop(); m_Timer.Tick -= OnTimerTick; m_Timer = null; } private void OnTimerTick() { const int dataPointCount = 100000; if (m_Bar.DataPoints.Count < dataPointCount) { m_Bar.DataPoints.Add(new NBarDataPoint(m_CurXValue++, m_Random.Next(80) + 20)); } else { m_Bar.DataPoints[m_Bar.OriginIndex].X = m_CurXValue++; m_Bar.DataPoints[m_Bar.OriginIndex].Value = m_Random.Next(80) + 20; m_Bar.OriginIndex++; if (m_Bar.OriginIndex >= m_Bar.DataPoints.Count) { m_Bar.OriginIndex = 0; } } } private void OnResetButtonClick(NEventArgs arg) { m_Bar.DataPoints.Clear(); m_Bar.OriginIndex = 0; m_CurXValue = 0; } private void OnToggleTimerButtonClick(NEventArgs arg) { NButton button = (NButton)arg.TargetNode; if ((int)button.Tag == 0) { m_Timer.Stop(); button.Content = new NLabel("Start Timer"); button.Tag = 1; } else { m_Timer.Start(); button.Content = new NLabel("Stop Timer"); button.Tag = 0; } } #endregion #region Fields private NCartesianChart m_Chart; private NBarSeries m_Bar; private Random m_Random; private NTimer m_Timer; private int m_CurXValue; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Real Time Bar Example for WPF Solution

VB.NET Real Time Bar Example Code for Nevron Chart for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System

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 - NRealTimeBarExample"

		' 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)

            chartView.Registered += AddressOf OnChartViewRegistered
            chartView.Unregistered += AddressOf OnChartViewUnregistered

            ' configure title
            chartView.Surface.Titles(0).Text = "Real Time Bar"

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

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear)

            Dim scaleY As NLinearScale = m_Chart.Axes(ENCartesianAxis.PrimaryX).Scale
            scaleY.InflateViewRangeBegin = False
            scaleY.InflateViewRangeEnd = False

            ' add interlaced stripe to the Y axis
            Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(ENNamedColor.Beige), Nothing, True, 0, 0, 1, 1)
            strip.Interlaced = True
            m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale.Strips.Add(strip)

            m_Random = New Random()

            m_Bar = New NBarSeries()
            m_Bar.Name = "Line Series"
            m_Bar.InflateMargins = True
            m_Bar.DataLabelStyle = New NDataLabelStyle(False)
            m_Bar.UseXValues = True
            m_Bar.WidthSizeMode = ENBarSizeMode.Logical
            m_Bar.LogicalWidth = 0.5
            m_CurXValue = 0

            m_Chart.Series.Add(m_Bar)

            chartView.Document.StyleSheets.ApplyTheme(New NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series))

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

            Dim toggleTimerButton As NButton = New NButton("Stop Timer")
            toggleTimerButton.Click += AddressOf OnToggleTimerButtonClick
            toggleTimerButton.Tag = 0
            stack.Add(toggleTimerButton)

            Dim resetButton As NButton = New NButton("Reset Data")
            resetButton.Click += AddressOf OnResetButtonClick
            stack.Add(resetButton)

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

This example demonstrates how to create a bar chart that updates in real time.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Bar Series", "https://www.nevron.com/docs/openvision/bar-chart-series.html"), ("Common Series Functionality", "https://www.nevron.com/docs/openvision/chart-common-series-functionality.html"), ("Chart Data Series", "https://www.nevron.com/docs/openvision/chart-data-series.html"), ("Zooming and Scrolling", "https://www.nevron.com/docs/openvision/chart-interactivity-zooming-and-scrolling.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnChartViewRegistered(arg As NEventArgs) m_Timer = New NTimer() Me.m_Timer.Tick += AddressOf OnTimerTick m_Timer.Start() End Sub Private Sub OnChartViewUnregistered(arg As NEventArgs) m_Timer.Stop() Me.m_Timer.Tick -= AddressOf OnTimerTick m_Timer = Nothing End Sub Private Sub OnTimerTick() Const dataPointCount = 100000 If m_Bar.DataPoints.Count < dataPointCount Then m_Bar.DataPoints.Add(New NBarDataPoint(Math.Min(System.Threading.Interlocked.Increment(m_CurXValue), m_CurXValue - 1), m_Random.Next(80) + 20)) Else m_Bar.DataPoints(m_Bar.OriginIndex).X = Math.Min(System.Threading.Interlocked.Increment(m_CurXValue), m_CurXValue - 1) m_Bar.DataPoints(m_Bar.OriginIndex).Value = m_Random.Next(80) + 20 m_Bar.OriginIndex += 1 If m_Bar.OriginIndex >= m_Bar.DataPoints.Count Then m_Bar.OriginIndex = 0 End If End If End Sub Private Sub OnResetButtonClick(arg As NEventArgs) m_Bar.DataPoints.Clear() m_Bar.OriginIndex = 0 m_CurXValue = 0 End Sub Private Sub OnToggleTimerButtonClick(arg As NEventArgs) Dim button = CType(arg.TargetNode, NButton) If CInt(button.Tag) = 0 Then m_Timer.Stop() button.Content = New NLabel("Start Timer") button.Tag = 1 Else m_Timer.Start() button.Content = New NLabel("Stop Timer") button.Tag = 0 End If End Sub #End Region #Region "Fields" Private m_Chart As NCartesianChart Private m_Bar As NBarSeries Private m_Random As Random Private m_Timer As NTimer Private m_CurXValue As Integer #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) Real Time Bar Example for WPF Solution