3.5 Graphing

This category list examples to demonstrate how to create a graph window, adding new layer, arrange layers in one graph, legend update and other graphing manipulation.


The following example shows how to plot multiple data to multiple layers in one graph, rearrange the layers, and then combine all legends from each layer into a single legend and put it on the first layer.

// Before running the following code, please new a worksheet and import \Samples\Curve Fitting\Multiple Gaussians.dat to worksheet.

void graph_ex()
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    	out_str("Error. Please active worksheet before running");
    GraphPage gp;
    // In worksheet, there should be at least 5 columns like XYYYY to create four xy range for plotting.
    const int nNumYs = 4;
    if ( wks.GetNumCols() < nNumYs + 1 )
        out_str("Please make an active worksheet with columns like XYYYY first!");
    // Plot each data to separate layers
    for ( int iData = 0, iLayer = 0; iData < nNumYs; iData++, iLayer++ )
        // Create one XY data range for plotting.
        XYRange dr;
        dr.Add(wks, 0, "X");
        dr.Add(wks, iData + 1, "Y");
        if ( gp.Layers.Count() <= iLayer )
            iLayer = gp.AddLayer();
        GraphLayer gl = gp.Layers(iLayer);
        // Plot data range on graph layer
        int nRet = gl.AddPlot(dr, IDM_PLOT_LINE);
        if ( nRet < 0 )
            printf("Fail to plot data range %d onto layer %d!\n", iData, iLayer);
    // Arrange layers to 2x2
    int nRows = 2;
    int nCols = 2;
    bool bRet = page_arrange_layers(gp, nRows, nCols); 
    if ( !bRet )
        printf("Fail to rearrange layers!\n");
    // Combine all legends to a single legend on the first layer.
    int iLegendLayer = 0; //the index of the layer on which to put the combined legend;
    bool bAscending = true; //sort the legends ascending before combine into one legend
    legend_combine(gp, iLegendLayer, bAscending);
This section covers the following topics: