2D Chart for WinForms | ComponentOne
Charting Data / Entering and Modifying Chart Data / Loading and Extracting Chart Data
In This Topic
    Loading and Extracting Chart Data
    In This Topic

    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:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    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
    

    To write code in C#

    C#
    Copy Code
    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);
    

    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.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim xpo As Object
    Dim cds As ChartDataSeries
    cds = C1Chart1.ChartGroups.ChartGroupsCollection(0).ChartData.SeriesList(0)
    xpo = cds.X.CopyDataOut()
    

    To write code in C#

    C#
    Copy Code
    object xpo;
    ChartDataSeries cds;
    cds = c1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];
    xpo = cds.X.CopyDataOut();
    

    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:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    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
    

    To write code in C#

    C#
    Copy Code
    ChartDataSeries cds;        
    cds = c1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0];                 
    float[] xpo = cds.X.CopyDataOut(typeof(float[]));        
    float[] ypo = cds.Y.CopyDataOut(typeof(float[]));
    
    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.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim xpo() As Single
    xpo = CType(C1Chart1.ChartGroups.ChartGroupsCollection(0), Single())_
    .ChartData.SeriesList(0).X.CopyDataOut(GetType(Single()))
    

    To write code in C#

    C#
    Copy Code
    float[] xpo;
    xpo = (float[])c1Chart1.ChartGroups.ChartGroupsCollection[0].ChartData       
    .SeriesList[0].X.CopyDataOut(typeof(float[]));
    
    See Also