Real Time Line Example for Blazor

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

Real Time Line Example for Blazor Description

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


Real Time Line Example

C# Real Time Line Example Code for Nevron Chart for Blazor
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 - NRealTimeLineExample";

			// 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 Line";

			// 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_Line = new NLineSeries();
			m_Line.Name = "Line Series";
			m_Line.InflateMargins = true;
			m_Line.DataLabelStyle = new NDataLabelStyle(false);
			m_Line.MarkerStyle = new NMarkerStyle(new NSize(4, 4));
			m_Line.UseXValues = true;
			m_CurXValue = 0;

			m_Chart.Series.Add(m_Line);

			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 line chart that updates in real time.

"; } /// /// 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"), ("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 = 40; if (m_Line.DataPoints.Count < dataPointCount) { m_Line.DataPoints.Add(new NLineDataPoint(m_CurXValue++, m_Random.Next(80) + 20)); } else { m_Line.DataPoints[m_Line.OriginIndex].X = m_CurXValue++; m_Line.DataPoints[m_Line.OriginIndex].Value = m_Random.Next(80) + 20; m_Line.OriginIndex++; if (m_Line.OriginIndex >= m_Line.DataPoints.Count) { m_Line.OriginIndex = 0; } } } private void OnResetButtonClick(NEventArgs arg) { m_Line.DataPoints.Clear(); m_Line.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 NCartesianChart m_Chart; NLineSeries m_Line; Random m_Random; NTimer m_Timer; int m_CurXValue; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Real Time Line Example for Blazor Solution