This example demonstrates how to create a 3D fast bar series, that renders millions of data points in real-time.

| 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 - NFastBar3D";
// 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
///
/// Represents a fast point series
///
class NPulsePoint
{
public double X;
public double Y;
public double Intensity;
public double WaveLength;
public double Phase;
public double PhaseStep;
public double PhaseSin;
};
///
///
///
///
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 Bar 3D";
// configure the chart
NCartesianChart chart = (NCartesianChart)chartView.Surface.Charts[0];
chart.Enable3D = true;
chart.ModelWidth = 50;
chart.ModelHeight = 50;
chart.ModelDepth = 50;
chart.Projection.SetPredefinedProjection(ENPredefinedProjection.Perspective1);
chart.LightModel.SetPredefinedLightModel(ENPredefinedLightModel.SoftTopRight);
chart.Interactor = new NInteractor(new NTrackballTool());
chart.ModelWidth = 50;
chart.ModelHeight = 10;
chart.ModelDepth = 50;
// setup the fast bar series
m_FastBar = new NFastBarSeries();
chart.Series.Add(m_FastBar);
m_FastBar.Name = "FastBar Series";
m_FastBar.Data.HasXValues = true;
m_FastBar.Data.HasZValues = true;
m_FastBar.Data.HasColors = true;
m_FastBar.Fill = new NColorFill(NColor.Red);
m_FastBar.FillColorMode = ENColorMode.Custom;
// precalculate sinus table
for (int i = 0; i < 360; i++)
{
m_SinTable[i] = Math.Sin(i * Math.PI / 180);
}
chart.Axes[ENCartesianAxis.PrimaryY].SetFixedViewRange(new NRange(-2, 2));
ConfigureLinearScale(chart.Axes[ENCartesianAxis.PrimaryX]);
ConfigureLinearScale(chart.Axes[ENCartesianAxis.PrimaryY]);
ConfigureLinearScale(chart.Axes[ENCartesianAxis.Depth]);
return chartViewWithCommandBars;
}
///
///
///
///
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
NUniSizeBoxGroup group = new NUniSizeBoxGroup(stack);
m_BarGridSizeComboBox = new NComboBox();
m_BarGridSizeComboBox.Items.Add(new NComboBoxItem("100x100"));
m_BarGridSizeComboBox.Items.Add(new NComboBoxItem("500x500"));
m_BarGridSizeComboBox.Items.Add(new NComboBoxItem("1000x1000"));
m_BarGridSizeComboBox.SelectedIndex = 0;
stack.Add(NPairBox.Create("Point Count Per Channel:", m_BarGridSizeComboBox));
m_EnableShaderRenderingCheckBox = new NCheckBox("Enable Shader Rendering");
m_EnableShaderRenderingCheckBox.CheckedChanged += OnEnableShaderRenderingCheckBoxCheckedChanged;
m_EnableShaderRenderingCheckBox.Checked = m_FastBar.EnableShaderRendering;
stack.Add(m_EnableShaderRenderingCheckBox);
m_EnableLightingCheckBox = new NCheckBox("Enable Lighting");
m_EnableLightingCheckBox.Checked = true;
m_EnableLightingCheckBox.CheckedChanged += OnEnableLightingCheckBoxCheckedChanged;
stack.Add(m_EnableLightingCheckBox);
m_FillColorModeComboBox = new NComboBox();
m_FillColorModeComboBox.Items.FillFromEnum();
m_FillColorModeComboBox.SelectedIndexChanged += OnFillColorModeComboBoxSelectedIndexChanged;
stack.Add(NPairBox.Create("Use Custom Colors", m_FillColorModeComboBox));
m_FillColorModeComboBox.SelectedIndex = (int)ENColorMode.Custom;
m_StartStopTimerButton = new NButton("Stop Timer");
m_StartStopTimerButton.Click += OnStartStopTimerButtonClick;
stack.Add(m_StartStopTimerButton);
// make sure max point count is updated
m_BarGridSizeComboBox.SelectedIndexChanged += OnPointCountPerChannelComboBoxSelectedIndexChanged;
OnPointCountPerChannelComboBoxSelectedIndexChanged(null);
return group;
}
///
///
///
///
public string GetExampleDescription()
{
return @"This example demonstrates how to create a 3D fast bar series, that renders millions of data points in real-time.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Fast Bar Series", "https://www.nevron.com/docs/openvision/fast-bar-chart-series.html"),
("Fast Series Overview", "https://www.nevron.com/docs/openvision/fast-chart-series-overview.html"),
("3D Charts", "https://www.nevron.com/docs/openvision/3d-charts.html"),
("Cartesian Axes", "https://www.nevron.com/docs/openvision/cartesian-chart-axes.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);
scale.ViewRangeInflateMode = ENScaleViewRangeInflateMode.Logical;
axis.Scale = scale;
}
#endregion
#region Event Handlers
///
/// Timer tick
///
private void OnTimerTick()
{
for (int i = 0; i < m_MaxPulsePoints; i++)
{
NPulsePoint pulsePoint = m_PulsePoints[i];
pulsePoint.Phase += pulsePoint.PhaseStep;
pulsePoint.PhaseSin = Math.Sin(pulsePoint.Phase);
}
// spin the points
unsafe
{
fixed (byte* pData = &m_FastBar.Data.Data[0])
{
int pointCount = m_FastBar.Data.Count;
int dataItemSize = m_FastBar.Data.DataItemSize;
float* pYValues = (float*)m_FastBar.Data.GetDataChannelPointer(ENSeriesDataChannelName.YValueF, pData);
bool useColor = m_FastBar.Data.HasColors;
uint* pColor = null;
if (useColor)
{
pColor = (uint*)m_FastBar.Data.GetDataChannelPointer(ENSeriesDataChannelName.Color, pData);
}
NColor color1 = NColor.Blue;
NColor color2 = NColor.Red;
const float value1 = -1;
const float value2 = 1;
float valueDistance = value2 - value1;
int r1 = color1.R;
int g1 = color1.G;
int b1 = color1.B;
int rDiff = (color2.R - color1.R);
int gDiff = (color2.G - color1.G);
int bDiff = (color2.B - color1.B);
NPulsePoint[] pulsePoints = m_PulsePoints;
for (int i = 0; i < pointCount; i++)
{
int row = i / m_GridSize;
int col = i % m_GridSize;
double sum = 0;
float x = col;
float z = row;
for (int j = 0; j < pulsePoints.Length; j++)
{
NPulsePoint pulsePoint = pulsePoints[j];
double dx = pulsePoint.X - row;
double dy = pulsePoint.Y - col;
double distance = Math.Sqrt(dx * dx + dy * dy);
double waveFactor = (distance - (int)(distance / pulsePoint.WaveLength) * pulsePoint.WaveLength) / pulsePoint.WaveLength;
waveFactor = m_SinTable[(int)(waveFactor * 360)];
// waveFactor = 1.0f;
sum += (pulsePoint.Intensity) * waveFactor * pulsePoint.PhaseSin * (1 - Math.Min(1, distance / m_GridSize));
}
// create a color between coor1 and color 2
if (useColor)
{
float factor = Math.Min(1, Math.Max(0, (*pYValues - value1) / valueDistance));
uint r = (uint)(r1 + (int)(rDiff * factor));
uint g = (uint)(g1 + (int)(gDiff * factor));
uint b = (uint)(b1 + (int)(bDiff * factor));
*pColor = (uint)(0xFF000000 | (r << 16) | (g << 8) | b);
}
*pYValues = Math.Min(2, (float)sum);
pYValues += dataItemSize;
pColor += dataItemSize;
}
}
}
m_FastBar.Data.OnDataChanged();
}
///
/// Start / stop timer button
///
///
void OnStartStopTimerButtonClick(NEventArgs args)
{
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 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 OnEnableShaderRenderingCheckBoxCheckedChanged(NValueChangeEventArgs arg)
{
m_FastBar.EnableShaderRendering = (bool)arg.NewValue;
}
///
///
///
///
///
private void OnEnableLightingCheckBoxCheckedChanged(NValueChangeEventArgs arg)
{
NChartViewWithCommandBars chartView = (NChartViewWithCommandBars)ExampleContent;
NChart[] charts = chartView.View.Surface.Charts;
for (int i = 0; i < charts.Length; i++)
{
NCartesianChart chart = (NCartesianChart)charts[i];
chart.LightModel.EnableLighting = m_EnableLightingCheckBox.Checked;
}
}
///
///
///
///
private void OnFillColorModeComboBoxSelectedIndexChanged(NValueChangeEventArgs arg)
{
ENColorMode colorMode = (ENColorMode)arg.NewValue;
m_FastBar.FillColorMode = colorMode;
m_FastBar.Data.HasColors = colorMode == ENColorMode.Custom;
}
///
///
///
///
///
private void OnPointCountPerChannelComboBoxSelectedIndexChanged(NValueChangeEventArgs arg)
{
switch (m_BarGridSizeComboBox.SelectedIndex)
{
case 0:
m_GridSize = 100;
break;
case 1:
m_GridSize = 500;
break;
case 2:
m_GridSize = 1000;
break;
}
int pointCount = m_GridSize * m_GridSize;
// m_Title.Text = "Fast Bar 3D\nShowing Realtime Rendering of " + (m_GridSize * m_GridSize).ToString("#,##0", CultureInfo.InvariantCulture) + " Data Points";
m_FastBar.WidthSizeMode = ENFastBarSizeMode.Logical;
m_FastBar.LogicalWidth = 1.0;
m_FastBar.DepthSizeMode = ENFastBarSizeMode.Logical;
m_FastBar.LogicalDepth = 1.0;
m_FastBar.Data.HasXValues = true;
m_FastBar.Data.HasZValues = true;
m_FastBar.Data.SetCount(pointCount);
m_FastBar.EnableShaderRendering = true;
m_FastBar.Fill = new NColorFill(NColor.Red);
m_PulsePoints = new NPulsePoint[m_MaxPulsePoints];
for (int i = 0; i < m_MaxPulsePoints; i++)
{
NPulsePoint pulsePoint = new NPulsePoint();
pulsePoint.X = m_GridSize * m_Random.NextDouble();
pulsePoint.Y = m_GridSize * m_Random.NextDouble();
pulsePoint.Intensity = Math.Max(0.25, m_Random.NextDouble());
pulsePoint.WaveLength = m_Random.NextDouble() * m_GridSize / 4.0;
pulsePoint.Phase = m_Random.NextDouble() * Math.PI * 2;
pulsePoint.PhaseStep = Math.Max(0.2, m_Random.NextDouble()) * Math.PI / 10;
pulsePoint.PhaseSin = Math.Sin(pulsePoint.Phase);
m_PulsePoints[i] = pulsePoint;
}
unsafe
{
int count = m_FastBar.Data.Count;
int dataItemSize = m_FastBar.Data.DataItemSize;
fixed (byte* pData = &m_FastBar.Data.Data[0])
{
float* pXValues = (float*)m_FastBar.Data.GetDataChannelPointer(ENSeriesDataChannelName.XValueF, pData);
float* pZValues = (float*)m_FastBar.Data.GetDataChannelPointer(ENSeriesDataChannelName.ZValueF, pData);
for (int i = 0; i < pointCount; i++)
{
int row = i / m_GridSize;
int col = i % m_GridSize;
float x = col;
float z = row;
*pXValues = x;
*pZValues = z;
pXValues += dataItemSize;
pZValues += dataItemSize;
}
}
}
OnTimerTick();
}
#endregion
#region Fields
Random m_Random = new Random();
const int m_MaxPulsePoints = 5;
double[] m_SinTable = new double[360];
NFastBarSeries m_FastBar;
NLabel m_Title;
NTimer m_Timer;
int m_GridSize;
NPulsePoint[] m_PulsePoints;
NComboBox m_BarGridSizeComboBox;
NCheckBox m_EnableShaderRenderingCheckBox;
NCheckBox m_EnableLightingCheckBox;
NComboBox m_FillColorModeComboBox;
NButton m_StartStopTimerButton;
#endregion
#region Static Methods
#endregion
#region Constants
#endregion
#region Nested Types
#endregion
}
}