Contour Plot from an XYZ Worksheet

Plot XYZ Contour from Worksheet and Custom Lines

Minimum Origin Version Required: Origin 8 SR0

This example to show how to plot XYZ contour and custom line colors, line styles, line thicknesses.

void plot_xyz_contour_ex1()
{
        int npts = 30;
        Worksheet wks;
        wks.Create();        
        wks.SetSize(-1,3);
        wks.SetColDesignations("XYZ");
        
        // fill wks with some XYZ data
        Dataset dsX(wks, 0);
        Dataset dsY(wks, 1);
        Dataset dsZ(wks, 2);
        dsX.Data(1, npts);
        dsX *= 0.05;
        dsY.Normal(npts);
        dsY += 1.2;
        dsZ.Normal(npts);
        
        // then we construct the XYZ data range
        DataRange dr;
        dr.Add(wks, 0, "X");
        dr.Add(wks, 1, "Y");
        dr.Add(wks, 2, "Z");
        
        // in this example, we will create a new graph, but you can use any exisitng graph layer
        // as long as the layer is a 2D layer
        GraphPage gp;
        // You must use a 3D scatter/line template, you can first make a 3d scatter plot, 
        // make needed modifications, then save your own
        gp.Create("TriContour");
        if( gp )
        {
                GraphLayer gl = gp.Layers();
                if( gl )
                {
                        int nPlot = gl.AddPlot(dr, IDM_PLOT_TRI_CONTOUR);
                        DataPlot dp = gl.DataPlots(nPlot);
                        gl.Rescale();                    
                        
                        set_contour_lines(dp);
                        gp.Refresh();
                }
        }
}


void set_contour_lines(DataPlot& dp)
{
        if(!dp)
                return;    

        // now we will set the colormap, we have to assume here that the color spectrum gr object is already in the 
        // layer to show the colormap
        double dMin = -1.5, dMax = 1.5;
        int nLevels = 20;
        double dInc = ( dMax - dMin ) / nLevels;
        vector vLevels;
        vLevels.Data(dMin, dMax, dInc);
        
        if( !dp.SetColormap(vLevels) )
                return;
        
        // line color
        int                        nNumColors = nLevels;
        vector<uint>    vnColors;
        int nColorFrom = SYSCOLOR_YELLOW;
        int nColorTo = nColorFrom + nNumColors - 1;            
        vnColors.Data(nColorFrom, nColorTo, 1);             
        
        // line styles
        vector<uint> vnStyles(vnColors.GetSize());
        vnStyles = 2; // Dot
        
        // line thickness
        vector vThickness(vnColors.GetSize()); 
        vThickness = 2.0; 
        
        // set all lines are show
        vector<int> vLines(vnColors.GetSize());
        vLines = 1;

        Tree tr; 
        tr.ColorMap.Details.ShowLines.nVals = vLines; // to show all lines
        tr.ColorMap.ColorFillControl.nVal = 0; // to remove fill color
        tr.ColorMap.Details.LineColors.nVals = vnColors;
        tr.ColorMap.Details.LineStyles.nVals = vnStyles;       
        tr.ColorMap.Details.LineWidths.dVals = vThickness;             

        if(!dp.SetColormap(tr))
                return;    
        
}

Plot XYZ Contour from Worksheet and Custom Labels

Minimum Origin Version Required: Origin 8.1 SR2

This example to show how to plot XYZ contour and custom labels color and font.

To run this example, create a worksheet and import the following data first: <Origin Installation Directory>\Samples\Matrix Conversion and Gridding\XYZ Random Gaussian.dat.

void plot_xyz_contour_ex2()
{
   Worksheet wks = Project.ActiveLayer();  // get active worksheet
   if( !wks )
       return;
   
    // construct the XYZ data range
    DataRange dr;
    dr.Add(wks, 0, "X");
    dr.Add(wks, 1, "Y");
    dr.Add(wks, 2, "Z");
    
    GraphPage gp;
    gp.Create("TriContour");  // create contour graph
    if( gp )
    {
        GraphLayer gl = gp.Layers();
        if( gl )
        {
            int nPlot = gl.AddPlot(dr, IDM_PLOT_TRI_CONTOUR);  // add contour plot to layer
            DataPlot dp = gl.DataPlots(nPlot);  // get the added plot
            gl.Rescale();     // rescale layer
            
           // set_contour_lines(dp);  
           set_label_format(dp);  // set label format
                
            gp.Refresh();  // refresh graph
        }
    }
}

void set_label_format(DataPlot& dp)
{
        if(!dp)
                return;
        
        vector vLevels;
        BOOL bLogScale;
        if(!dp.GetColormap(vLevels, bLogScale))  // get colormap of plot
                return;
        
        int nNumLabels = vLevels.GetSize() - 1;  // number of labels
        vector<int> vnLabelShow(nNumLabels);
        vnLabelShow = 1;  // show labels
        
        Tree trFormat;
        trFormat.Root.ColorMap.Details.Labels.nVals = vnLabelShow;  // show labels
        trFormat.Root.Labels.Color.nVal = SYSCOLOR_MAGENTA;  // color=magenta
        trFormat.Root.Labels.Size.dVal = 30;  // size=30
        trFormat.Root.Labels.Bold.nVal = true; 
        trFormat.Root.Labels.Italic.nVal = true;
        
        if(0 == dp.UpdateThemeIDs(trFormat.Root))
                if(!dp.ApplyFormat(trFormat, true, true))   // apply format
                        out_str("Failed to apply format!");
}