ComponentOne Chart for .NET (2.0) Search HelpCentral 

Loading and Extracting Chart Data

CopyDataIn Method

In C1Chart, the X, Y, Y1, Y2, and Y3 data array objects accept Object data types. Therefore, a variety of .NET type arrays may be entered into the ChartDataArray objects (except for PointData). The CopyDataIn method of the ChartDataArray takes an Object data type (which can be an array of various types) and loads it into the data array.

A good implementation would be to maintain a set of arrays for the data series and before the chart is to be drawn, update the C1Chart’s data array objects with the arrays of values. This allows full control over the data and provides control over batching the data values. The following code shows a brief example of how the arrays can be built and set to the chart’s data array objects:

·      Visual Basic

Dim xp(9) As Single

Dim yp(9) As Single

Dim i As Integer

 

For i = 0 To 9

    xp(i) = i

    yp(i) = i * i

Next i

 

With C1Chart1.ChartGroups.ChartGroupsCollection(0).ChartData.SeriesList(0)

    .X.CopyDataIn(xp)

    .Y.CopyDataIn(yp)

End With

·      C#

float xp(10);

float yp(10);

int i;

 

for( i=0; i < 10; i++)

{

   xp[i] = i;

   yp[i] = i * i;

}

 

ChartDataSeries cds =

   C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];

 

cds.X.CopyDataIn(xp);

cds.Y.CopyDataIn(yp);

·      Delphi

var

  xp:  array[0..9] of Single;

  yp:  array[0..9] of Single;

  i:   Integer;

  cds: ChartDataSeries;

 

begin

  for i := 0 to 9 do

  begin

    xp[i] := i;

    yp[i] := i * i;

  end

 

cds := C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];

cds.X.CopyDataIn(xp);

cds.X.CopyDataIn(yp);

 

end;

CopyDataOut Method

While the CopyDataIn method loads the array data into the ChartDataArray objects, the CopyDataOut method extracts data from the array objects. This method returns an Object data type, which contains the data array.

·      Visual Basic

Dim xpo As Object

Dim cds As ChartDataSeries

 

cds = C1Chart1.ChartGroups.ChartGroupsCollection(0).ChartData.SeriesList(0)

xpo = cds.X.CopyDataOut()

·      C#

object xpo;

ChartDataSeries cds;

 

cds = C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];

xpo = cds.X.CopyDataOut();

·      Delphi

var

  xpo: Object;

  cds: ChartDataSeries;

begin

  cds = C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];

  xpo = cds.X.CopyDataOut();

end;

To prevent having to then convert this Object to an array of the correct data type, the CopyDataOut method can also accept a parameter of System.Type, which causes it to then produce an array of the specified type. Because the actual data type is required for the parameter, the code must also include the GetType() function (typeof() in C#) to deliver the correct type. In the following example, the arrays are returned from the chart’s ChartDataArray objects with the CopyDataOut method, but as data arrays of type Single:

·      Visual Basic

Dim xpo As Single()

Dim ypo As Single()

 

With C1Chart1.ChartGroups.ChartGroupsCollection(0).ChartData.SeriesList(0)

xpo = .X.CopyDataOut(GetType(Single()))

ypo = .Y.CopyDataOut(GetType(Single()))

End With

·      C#

ChartDataSeries cds;

cds = C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];

 

float[] xpo = cds.X.CopyDataOut(typeof(float[]));

float[] ypo = cds.Y.CopyDataOut(typeof(float[]));

·      Delphi

var

  cds: ChartDataSeries;

  xpo: array of Single;

  ypo: array of Single;

 

begin

  cds := C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];

  xpo := cds.X.CopyDataOut(TypeOf(array of Single));

  ypo := cds.Y.CopyDataOut(TypeOf(array of Single));

end;

Note: For C# users, the CopyDataOut implementation is slightly different. In C# array data types cannot be set to variables of type Object. The array can be set to the Object if the Object is explicitly cast to the correct data type.

·      Visual Basic

Dim xpo() As Single

 

xpo = CType(C1Chart1.ChartGroups.ChartGroupsCollection(0), Single())_

.ChartData.SeriesList(0).X.CopyDataOut(GetType(Single()))

·      C#

float[] xpo;

xpo = (float[])C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData
       .SeriesList[0].X.CopyDataOut(typeof(float[]));

·      Delphi

var

  xpo: array of Single;

 

begin 

  xpo = (array of Single)(C1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData

    .SeriesList[0].X.CopyDataOut(TypeOf(array of Single)));

end;


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.