This example demonstrates how to create a XY scatter point chart rendering millions of data points in real-time using the NFastPointSeries.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Chart.Tools;
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 - NFastPoint2D";
// 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;
m_Title = chartView.Surface.Titles[0];
m_Title.Text = "Fast Point 2D";
NCartesianChart chart = (NCartesianChart)chartView.Surface.Charts[0];
ConfigureLinearScale(chart.Axes[ENCartesianAxis.PrimaryX]);
ConfigureLinearScale(chart.Axes[ENCartesianAxis.PrimaryY]);
// setup the fast point series
m_FastPointSeries = new NFastPointSeries();
chart.Series.Add(m_FastPointSeries);
m_FastPointSeries.FillColorMode = ENColorMode.Custom;
m_FastPointSeries.Name = "FastPoint Series";
// enable zooming and scrolling
NInteractor interactor = new NInteractor();
interactor.Add(new NRectangleZoomTool());
NDataPanTool dataPanTool = new NDataPanTool();
dataPanTool.StartMouseButtonEvent = ENMouseButtonEvent.RightButtonDown;
dataPanTool.EndMouseButtonEvent = ENMouseButtonEvent.RightButtonUp;
interactor.Add(dataPanTool);
chart.Interactor = interactor;
return chartViewWithCommandBars;
}
///
///
///
///
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
NUniSizeBoxGroup group = new NUniSizeBoxGroup(stack);
m_PointCountComboBox = new NComboBox();
m_PointCountComboBox.Items.Fill(new string[] { "100K", "500K", "1M", "2M", "10M"});
m_PointCountComboBox.SelectedIndexChanged += OnPointCountComboBoxSelectedIndexChanged;
m_PointCountComboBox.SelectedIndex = 2;
stack.Add(NPairBox.Create("Point Count:", m_PointCountComboBox));
m_PointSizeNumericUpDown = new NNumericUpDown();
m_PointSizeNumericUpDown.Value = 2;
m_PointSizeNumericUpDown.Minimum = 1;
m_PointSizeNumericUpDown.Maximum = 20;
m_PointSizeNumericUpDown.ValueChanged += OnPointSizeNumericUpDownValueChanged;
stack.Add(NPairBox.Create("Point Size:", m_PointSizeNumericUpDown));
m_EnableShaderRenderingCheckBox = new NCheckBox();
m_EnableShaderRenderingCheckBox.CheckedChanged += OnEnableShaderRenderingCheckBoxChanged;
m_EnableShaderRenderingCheckBox.Checked = m_FastPointSeries.EnableShaderRendering;
stack.Add(NPairBox.Create("Enable Shader Rendering:", m_EnableShaderRenderingCheckBox));
m_StartStopTimerButton = new NButton("Stop Timer");
m_StartStopTimerButton.Click += OnStartStopTimerButtonClick;
stack.Add(m_StartStopTimerButton);
AddData();
return group;
}
///
///
///
///
public string GetExampleDescription()
{
return @"This example demonstrates how to create a XY scatter point chart rendering millions of data points in real-time using the NFastPointSeries.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Fast Point Series", "https://www.nevron.com/docs/openvision/fast-point-chart-series.html"),
("Fast Series Overview", "https://www.nevron.com/docs/openvision/fast-chart-series-overview.html"),
("Chart Data Series", "https://www.nevron.com/docs/openvision/chart-data-series.html"),
};
}
#endregion
#region Implementation
///
/// Applies a linear scale and an interlaced stripe at the specified axis
///
///
private void ConfigureLinearScale(NCartesianAxis axis)
{
// add interlaced stripe to the Y axis
NLinearScale scale = new NLinearScale();
NScaleStrip stripStyle = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
stripStyle.SetShowAtWall(ENChartWall.Left, true);
stripStyle.SetShowAtWall(ENChartWall.Back, true);
stripStyle.SetShowAtWall(ENChartWall.Right, true);
stripStyle.SetShowAtWall(ENChartWall.Front, true);
stripStyle.SetShowAtWall(ENChartWall.Bottom, true);
stripStyle.SetShowAtWall(ENChartWall.Top, true);
stripStyle.Interlaced = true;
scale.Strips.Add(stripStyle);
axis.Scale = scale;
axis.Visible = true;
}
#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;
}
///
///
///
///
void OnStartStopTimerButtonClick(NEventArgs arg)
{
AddData();
if (m_Timer.IsStarted)
{
m_Timer.Stop();
m_StartStopTimerButton.Content = new NLabel("Start Timer");
}
else
{
m_Timer.Start();
m_StartStopTimerButton.Content = new NLabel("Stop Timer");
}
}
///
///
///
private void OnTimerTick()
{
// spin the points
unsafe
{
fixed (byte* pData = &m_FastPointSeries.Data.Data[0])
{
int count = m_FastPointSeries.Data.Count;
int dataItemSize = m_FastPointSeries.Data.DataItemSize;
float* pXValues = (float*)m_FastPointSeries.Data.GetDataChannelPointer(ENSeriesDataChannelName.XValueF, pData);
float* pYValues = (float*)m_FastPointSeries.Data.GetDataChannelPointer(ENSeriesDataChannelName.YValueF, pData);
NVector2DF[] driftSpeeds = m_DriftSpeeds;
// modify the data
for (int i = 0; i < count; i++)
{
NVector2DF v;
v.X = *pXValues + driftSpeeds[i].X;
v.Y = *pYValues + driftSpeeds[i].Y;
if ((v.X * v.X + v.Y * v.Y) > 1)
{
// When outside the sphere, change to a random velocity,
// and multiply the point by 0.9997 to move it back
// towards the inside of the sphere. Using a value
// close to 1 allows the sphere to get a little
// fuzzy.
m_DriftSpeeds[i] = GetRandomVelocity();
v.X *= 0.9997f;
v.Y *= 0.9997f;
}
else if (m_Random.Next() < 0.001)
{
// change to a new random velocity, with a small probability
m_DriftSpeeds[i] = GetRandomVelocity();
}
*pXValues = v.X;
*pYValues = v.Y;
pXValues += dataItemSize;
pYValues += dataItemSize;
}
m_FastPointSeries.Data.OnDataChanged();
}
}
}
///
///
///
///
private void OnPointSizeNumericUpDownValueChanged(NValueChangeEventArgs arg)
{
m_FastPointSeries.Size = (float)m_PointSizeNumericUpDown.Value;
}
///
///
///
///
private void OnPointCountComboBoxSelectedIndexChanged(NValueChangeEventArgs arg)
{
AddData();
}
///
///
///
///
private void OnEnableShaderRenderingCheckBoxChanged(NValueChangeEventArgs arg)
{
m_FastPointSeries.EnableShaderRendering = m_EnableShaderRenderingCheckBox.Checked;
}
#endregion
#region Fields
NFastPointSeries m_FastPointSeries;
NLabel m_Title;
NTimer m_Timer;
NComboBox m_PointCountComboBox;
NNumericUpDown m_PointSizeNumericUpDown;
NCheckBox m_EnableShaderRenderingCheckBox;
NButton m_StartStopTimerButton;
Random m_Random = new Random();
NVector2DF[] m_DriftSpeeds;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
}