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.