DataRange::Create

Description

Create a valid DataRange object.

Syntax

int Create( LPCSTR lpcszRangeStr = NULL, int nXVtype = XVT_DATARANGE, BOOL bCreateTargets = false )

BOOL Create( TreeNode & tr, BOOL bOneData = TRUE )

Parameters

lpcszRangeStr
[input]the range string, like "[Book1]Sheet1!Col(A)"
nXVtype
[input]X variable type defined in oc_sys.h
bCreateTargets
[input]TRUE means new target is created. FALSE remains origin.

tr
[input]the tree containing the range data
bOneData
[input]TRUE means only one range is added to tree. False means add multiple ranges.

Return

zero if successful, otherwise erroe code.


TRUE if successful, otherwise FALSE.

Examples

EX1

//Assume there is an active worksheet with two columns(Col(A) and Col(B)).
//This example will create two new columns and use them to construct a datarange.
//Then fill the two columns with some data and draw a scatter graph.
void DataRange_Create_Ex1()
{
        Worksheet wks = Project.ActiveLayer();
        DataRange dr;
        //Creat Col(C) and Col(D) and use the two columns to construct a datarange.
        dr.Create("Col(C)", XVT_DATARANGE, true);//true mean create two new columns.
        dr.Create("Col(D)", XVT_XYDATARANGE, true);  
        wks.GetPage().Refresh(); //Refresh to show the new added columns.

        Column colC;
        colC.Attach(wks, 2);
        colC.SetFormula("5*(i-1)");
        colC.ExecuteFormula(); //Fill values in ColC with formula:5*(i-1) 
        
        Column colD;
        colD.Attach(wks, 3);
        // set Recalculate = Auto
        colD.SetFormula("sin(4*Col(C)*pi/180)", AU_AUTO);
        colD.ExecuteFormula();//Fill values in ColD with formula:sin(4*Col(C)*pi/180


        GraphPage gp;
    gp.Create("Origin", CREATE_HIDDEN);//Create a hidden blank graph page.
    if( gp )
    {
        GraphLayer gl = gp.Layers();
        if( gl )
        {
            gl.AddPlot(dr, IDM_PLOT_SCATTER);//Add a plot to the graph page with ColC and ColD.
            gl.Rescale();
            gp.SetShow();
        }
    }


}

EX2

//This example need active one or multiple selected xy subranges. Will
//print out the minimum and maximum X and Y values of active worksheet.
void DataRange_Create_Ex2(BOOL bOneData = FALSE)//TRUE means only one range is added to tree. FALSE means add multiple ranges. 
{
    Worksheet wks = Project.ActiveLayer();
    if( wks )
    {
 
        Tree trXYSelection;
        DWORD dwRules = DRR_GET_DEPENDENT | DRR_NO_FACTORS;
        init_input_data_branch_from_selection(trXYSelection, dwRules);
        out_tree(trXYSelection);
 
 
        DataRange dr,drSub;
        string strRange;
        dr.Create();
        dr.SetTree(trXYSelection, bOneData);
 
        DWORD dwPlotID; // This is to retrieve DataPlot UID if present
        vector vX, vY;
        double xmin, xmax, ymin, ymax;
        int nNumDatasets = dr.GetNumData(dwRules);
        for( int nIndex = 0; nIndex < nNumDatasets; nIndex++ )
        {
            // Copy data associated with nIndex of dr into vectors using DataRange::GetData
            dr.GetData(dwRules, nIndex, &dwPlotID, NULL, &vY, &vX);
 
            // Now we have made a copy of XY data into vectors vX and vY
            // so we can do analysis on the data...for example:
            vX.GetMinMax(xmin, xmax);
            vY.GetMinMax(ymin, ymax);
            dr.GetSubRange(drSub, dwRules, nIndex);
            strRange = drSub.GetDescription();
            printf("%s\nxmin = %g\nxmax = %g\nymin = %g\nymax = %g\n", strRange, xmin, xmax, ymin, ymax);
        }
    }
}


EX3

//This example need active a graph with selected plot. Will prints out the minimum and 
//maximum X and Y values in the selected subranges of the active graph.
void DataRange_Create_Ex3()
{
        GraphLayer graph = Project.ActiveLayer();
        if(graph)
        {
                Tree trXYSelection;
                DWORD dwRules = DRR_GET_DEPENDENT | DRR_NO_FACTORS;
                init_input_data_branch_from_selection(trXYSelection, dwRules);
                out_tree(trXYSelection);
 
                DataRange dr, drSub;
                dr.Create(trXYSelection, false);//false mean add multiple ranges to the tree.
 
                DWORD dwPlotID; // This is to retrieve DataPlot UID if present
                vector vX, vY;
                double xmin, xmax, ymin, ymax;
                string strRange;
                int nNumDatasets = dr.GetNumData(dwRules);
                for( int nIndex = 0; nIndex < nNumDatasets; nIndex++ )
                {
                        // Copy data associated with nIndex of dr into vectors using DataRange::GetData
                        dr.GetData( dwRules, nIndex, &dwPlotID, NULL, &vY, &vX);
 
                        // Now we have made a copy of XY data into vectors vX and vY
                        // so we can do analysis on the data...for example:
                        vX.GetMinMax(xmin, xmax);
                        vY.GetMinMax(ymin, ymax);
                        dr.GetSubRange(drSub, dwRules, nIndex);
                        strRange = drSub.GetDescription();
                        printf("%s\nxmin = %g\nxmax = %g\nymin = %g\nymax = %g\n", strRange, xmin, xmax, ymin, ymax);
                }
        }
}

EX4

//This example need to active a worksheet with xy columns data.
//Will create a graph according to the data is in the active worksheet.
void DataRange_Create_Ex4(string strX = "A", string strY = "B")
{
    Tree tr;
    Worksheet wks = Project.ActiveLayer();
 
    // Construct DataRange tree
    construct_one_data_range(tr, wks, strX, strY, NULL, NULL, true);
    out_tree(tr);
 
    DataRange dr;
    dr.Create(tr, true); // Default, only one range
 
    // Plot DataRange
    GraphPage gp;
    gp.Create("Origin", CREATE_HIDDEN);
    if( gp )
    {
        GraphLayer gl = gp.Layers();
        if( gl )
        {
            gl.AddPlot(dr, IDM_PLOT_SCATTER);
            gl.Rescale();
            gp.SetShow();
        }
    }
}

Remark

See Also

DataRange::Destroy

header to Include

origin.h