Chart for .NET / User's Guide / Chart Types / Working with Data Points

In This Topic
    Working with Data Points
    In This Topic
    The series types derived from NSeries implement the INDataPointCollection interface, which enables you to work with the series as if it is a collection of data points. This interface provides the following functionality:
     Adding Data Points

    Data points can be added to a series with the help of the AddDataPoint method. It accepts a NDataPoint object that contains the data point values. The following code adds several data points to a line series:

    C#
    Copy Code
    NChart chart = chartControl.Charts[0];
    NLineSeries line = (NLineSeries)chart.Series.Add(SeriesType.Line);
    line.AddDataPoint(new NDataPoint(34.6));
    line.AddDataPoint(new NDataPoint(14.4));
    line.AddDataPoint(new NDataPoint(19.6));
    
    Visual Basic
    Copy Code
    Dim chart As NChart = chartControl.Charts(0)
    Dim line As NLineSeries = chart.Series.Add(SeriesType.Line)
    line.AddDataPoint(New NDataPoint(34.6))
    line.AddDataPoint(New NDataPoint(14.4))
    line.AddDataPoint(New NDataPoint(19.6))
    

    A little bit longer, but more efficient way to add data points is to create just one NDataPoint object and reuse it for all the data points that you add. The following code demonstrates this approach:

    C#
    Copy Code
    NDataPoint dp = new NDataPoint();
    dp[DataPointValue.Value] = 34.6;
    line.AddDataPoint(dp);
    dp[DataPointValue.Value] = 14.4;
    line.AddDataPoint(dp);
    dp[DataPointValue.Value] = 19.6;
    line.AddDataPoint(dp);
    
    Visual Basic
    Copy Code
    Dim dp As NDataPoint = New NDataPoint
    dp(DataPointValue.Value) = 34.6
    line.AddDataPoint(dp)
    dp(DataPointValue.Value) = 14.4
    line.AddDataPoint(dp)
    dp(DataPointValue.Value) = 19.6
    line.AddDataPoint(dp)
    

    The examples above are valid not only for line series, but for all types of series that require one value per data point - Area, Bar, Pie, Radar, Point, SmoothLine, etc. The various constructors of the NDataPoint class let you initialize new data points with different combinations of x, y, z values, labels, fill styles and stroke styles. For series that require more values per data point, there are specialized data point classes that make the initialization easier. For example a Floating Bar series requires Begin and End value. The NFloatBarDataPoint provides several constructors that accept begin / end values. The following code demonstrates how to use the NFloatBarDataPoint object:

    C#
    Copy Code
    NFloatBarSeries floatbar = (NFloatBarSeries)chart.Series.Add(SeriesType.FloatBar);
    floatbar.AddDataPoint(new NFloatBarDataPoint(15, 26));
    
    Visual Basic
    Copy Code
    Dim floatbar As NFloatBarSeries = chart.Series.Add(SeriesType.FloatBar)
    floatbar.AddDataPoint(New NFloatBarDataPoint(15, 26))
    

    If you prefer to use the NDataPoint class you can add a floatbar data point with the following code:

    C#
    Copy Code
    NDataPoint dp = new NDataPoint();
    dp[DataPointValue.Begin] = 15;
    dp[DataPointValue.End] = 26;
    floatbar.AddDataPoint(dp);
    
    Visual Basic
    Copy Code
    Dim dp As NDataPoint = New NDataPoint
    dp(DataPointValue.Begin) = 15
    dp(DataPointValue.End) = 26
    floatbar.AddDataPoint(dp)
    

    Besides the AddDataPoint method you can use the StoreDataPoint and InsertDataPointAt methods to pass data to the series. The StoreDataPoint method sets a data point at a given index, replacing the current values. The InsertDataPointAt method inserts a data point at a given index. Both methods can accept indexes that are greater than the current size of the data point collection.

     Removing Data Points

    The data point collection interface provides the ClearDataPoints method to remove all data points from a series. This method deletes all the items from all data series in a series. It is recommended to use ClearDataPoints to clean up the series data even in cases when you add data directly to the data series.

    If you need to remove a specific data point you can use the RemoveDataPointAt method.

     Other Operations

    If you need to get the data point at a specific index you can use the ComposeDataPoint method. It creates a new NDataPoint object and fills it with the values at the specified index. This method can be useful if you want to change a specific data point value - you can get all the data point values, change some of them and then set the data point back to its place with the StoreDataPoint method.

    You can use the GetDataPointCount to obtain the number of data points in the series. This is useful when you have to iterate through the series data points.

    See Also