Enable sampling for the line series chart

Applies to: Nevron Chart for .NET

How to enable sampling for the line series chart?

Nevron Chart Sampling feature allows plotting of line charts with hundreds of thousands of data points. When using sampling the line will automatically resample the data storage depending on the size of the chart on the screen. This allows for the visualization of massive amount of data. Enabling the line series sampling will also significantly improve your performance.


Figure 1: Sampled Line Series Chart

The following example produces 4 line series, each one with 100k data points:

[C#]
using System;
using System.Drawing;
using System.Windows.Forms;
using Nevron.Chart;
using Nevron.GraphicsCore;
...
private void Form1_Load(object sender, EventArgs e)
{
    // no legend
    nChartControl1.Legends.Clear();
 
    // configure the chart
    NChart Chart = nChartControl1.Charts[0];
     
    Chart.Dock = DockStyle.Fill;
    Chart.DockMargins = new NMarginsL(5, 20, 5, 20);
    Chart.BoundsMode = BoundsMode.Stretch;
 
    // add interlaced stripe to the Y axis
    NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.FromArgb(155, 221, 221, 221)), null, true, 0, 0, 1, 1);
    stripStyle.ShowAtWalls = new ChartWallType[] { ChartWallType.Back };
    stripStyle.Interlaced = true;
    ((NStandardScaleConfigurator)Chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator).StripStyles.Add(stripStyle);
 
    // add a line series1
    NLineSeries Line1 = (NLineSeries)Chart.Series.Add(SeriesType.Line);
    Line1.Name = "Line Series1";
    Line1.BorderStyle.Color = Color.Red;
    Line1.InflateMargins = true;
    Line1.DataLabelStyle.Visible = false;
    Line1.MarkerStyle.Visible = false;
    Line1.SamplingMode = SeriesSamplingMode.Enabled;
     
    // add a line series2
    NLineSeries Line2 = (NLineSeries)Chart.Series.Add(SeriesType.Line);
    Line2.Name = "Line Series2";
    Line2.BorderStyle.Color = Color.Green;
    Line2.InflateMargins = true;
    Line2.DataLabelStyle.Visible = false;
    Line2.MarkerStyle.Visible = false;
    Line2.SamplingMode = SeriesSamplingMode.Enabled;
 
    // add a line series3
    NLineSeries Line3 = (NLineSeries)Chart.Series.Add(SeriesType.Line);
    Line3.Name = "Line Series3";
    Line3.BorderStyle.Color = Color.Blue;
    Line3.InflateMargins = true;
    Line3.DataLabelStyle.Visible = false;
    Line3.MarkerStyle.Visible = false;
    Line3.SamplingMode = SeriesSamplingMode.Enabled;
 
    // add a line series4
    NLineSeries Line4 = (NLineSeries)Chart.Series.Add(SeriesType.Line);
    Line4.Name = "Line Series4";
    Line4.BorderStyle.Color = Color.Orange;
    Line4.InflateMargins = true;
    Line4.DataLabelStyle.Visible = false;
    Line4.MarkerStyle.Visible = false;
    Line4.SamplingMode = SeriesSamplingMode.Enabled;
 
    // add data to the line series
    Random rand = new Random();
 
    AddNewData(Line1, 100000);
    AddNewData(Line2, 100000);
    AddNewData(Line3, 100000);
    AddNewData(Line4, 100000);
}
 
private void AddNewData(NLineSeries line, int count)
{
    Random rand = new Random();
 
    double prevYVal = 0;
    double prevXVal = 0;
 
    double[] yValues = new double[count];
 
    double magnitude = 0.01 + rand.NextDouble() * 5;
 
    // continuous
    double angle = 0;
    double phase = (Math.PI * 2 * rand.NextDouble()) / count + 0.0001;
 
    for (int i = 0; i < count; i++)
    {
        double yStep = Math.Sin(angle) * magnitude;
        double xStep = 0.01 + rand.NextDouble() * magnitude;
 
        if (xStep < 0)
        {
            xStep = 0;
        }
 
        angle += phase;
        prevXVal += xStep;
 
        yValues[i] = prevYVal + yStep;
    }
 
    line.Values.AddRange(yValues);
}

[VB.NET]
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Nevron.Chart
Imports Nevron.GraphicsCore
...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' no legend
    NChartControl1.Legends.Clear()
 
    ' configure the chart
    Dim Chart As NChart = NChartControl1.Charts(0)
 
    Chart.Dock = DockStyle.Fill
    Chart.DockMargins = New NMarginsL(5, 20, 5, 20)
    Chart.BoundsMode = BoundsMode.Stretch
 
    ' add interlaced stripe to the Y axis
    Dim stripStyle As New NScaleStripStyle(New NColorFillStyle(Color.FromArgb(155, 221, 221, 221)), Nothing, True, 0, 0, 1, 1)
    stripStyle.ShowAtWalls = New ChartWallType() {ChartWallType.Back}
    stripStyle.Interlaced = True
    DirectCast(Chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator, NStandardScaleConfigurator).StripStyles.Add(stripStyle)
 
    ' add a line series1
    Dim Line1 As NLineSeries = DirectCast(Chart.Series.Add(SeriesType.Line), NLineSeries)
    Line1.Name = "Line Series1"
    Line1.BorderStyle.Color = Color.Red
    Line1.InflateMargins = True
    Line1.DataLabelStyle.Visible = False
    Line1.MarkerStyle.Visible = False
    Line1.SamplingMode = SeriesSamplingMode.Enabled
 
    ' add a line series2
    Dim Line2 As NLineSeries = DirectCast(Chart.Series.Add(SeriesType.Line), NLineSeries)
    Line2.Name = "Line Series2"
    Line2.BorderStyle.Color = Color.Green
    Line2.InflateMargins = True
    Line2.DataLabelStyle.Visible = False
    Line2.MarkerStyle.Visible = False
    Line2.SamplingMode = SeriesSamplingMode.Enabled
 
    ' add a line series3
    Dim Line3 As NLineSeries = DirectCast(Chart.Series.Add(SeriesType.Line), NLineSeries)
    Line3.Name = "Line Series3"
    Line3.BorderStyle.Color = Color.Blue
    Line3.InflateMargins = True
    Line3.DataLabelStyle.Visible = False
    Line3.MarkerStyle.Visible = False
    Line3.SamplingMode = SeriesSamplingMode.Enabled
 
    ' add a line series4
    Dim Line4 As NLineSeries = DirectCast(Chart.Series.Add(SeriesType.Line), NLineSeries)
    Line4.Name = "Line Series4"
    Line4.BorderStyle.Color = Color.Orange
    Line4.InflateMargins = True
    Line4.DataLabelStyle.Visible = False
    Line4.MarkerStyle.Visible = False
    Line4.SamplingMode = SeriesSamplingMode.Enabled
 
    ' add data to the line series
    Dim rand As New Random()
 
    AddNewData(Line1, 100000)
    AddNewData(Line2, 100000)
    AddNewData(Line3, 100000)
    AddNewData(Line4, 100000)
End Sub
 
Private Sub AddNewData(ByVal line As NLineSeries, ByVal count As Integer)
    Dim rand As New Random()
 
    Dim prevYVal As Double = 0
    Dim prevXVal As Double = 0
 
    Dim yValues As Double() = New Double(count - 1) {}
 
    Dim magnitude As Double = 0.01 + rand.NextDouble() * 5
 
    ' continuous
    Dim angle As Double = 0
    Dim phase As Double = (Math.PI * 2 * rand.NextDouble()) / count + 0.0001
 
    For i As Integer = 0 To count - 1
        Dim yStep As Double = Math.Sin(angle) * magnitude
        Dim xStep As Double = 0.01 + rand.NextDouble() * magnitude
 
        If xStep < 0 Then
            xStep = 0
        End If
 
        angle += phase
        prevXVal += xStep
 
        yValues(i) = prevYVal + yStep
    Next
 
    line.Values.AddRange(yValues)
End Sub

Article ID: 138, Created On: 11/23/2010, Modified: 12/1/2010