Accessing Graph Format with Theme Tree

 

 

Version Info

Minimum Origin Version Required: Origin 8 SR0

View Graph Format Tree

Following code show how to access graph format using Origin C code.

To customize graph format, you may use OriginObject::GetFormat to get the format tree of one Origin object(e.x. GraphPage, GraphLayer, DataPlot, Axis), save it to XML file or output it the learn more about it.

To view all graph layer relative format from tree. Compile, and then run this function in Commad window will output format tree.

void ViewGraphFormatTree()
{
        GraphLayer gl = Project.ActiveLayer();
        if(!gl)
                return;
        
        Tree trFormat;
        trFormat = gl.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);
        out_tree(trFormat); // output format tree
        
        string strSaveTo = GetAppPath(false) + "FormatTree.xml";
        if(trFormat.Save(strSaveTo))
                out_str("Format Tree xml file saved to "+strSaveTo);
}
// This function shows how to get Stack settings from a graph.
// Stack settings in the Theme are only available in Origin version 8.1 and later.
void GetGraphFormatStackSetting()
{
    GraphLayer gl = Project.ActiveLayer();
    if(!gl)
        return;
    
    Tree trFormat;
    trFormat = gl.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);

#if _OC_VER >= 0x0810
    switch( trFormat.Root.Stack.Offset.nVal )
    {
    case PLOTSTACK_NONE:
        break;
    case PLOTSTACK_CUMULATIVE:
                out_str("Cumulative offset setting.");
        bool bCumulative = true;
        break;
    case PLOTSTACK_CONST:
                out_str("Constant offset setting.");
        double dConst = trFormat.Root.Stack.Constant.nVal;
        break;
    case PLOTSTACK_AUTO:
                out_str("Auto offset setting.");
        double dGap = trFormat.Root.Stack.Gap.dVal;
        break;
    case PLOTSTACK_INDIVIDUAL:
        out_str("Individual offset setting.");
        bool bX = trFormat.Root.Stack.X.nVal;
        bool bY = trFormat.Root.Stack.Y.nVal;
        break;
    default:
        out_str("Unknown offset setting.");
        break;
    }
#else // !_OC_VER >= 0x0810
    out_str("Stack settings in Theme only available in Origin 8.1 and later.");
#endif // !_OC_VER >= 0x0810
}

Change Plot Label Justify

For now, theme tree not work on change label justify, we will support, can firstly use Labtalk command as below.

Run "change_label_justify 2 1", label display as center.

// nLabelPlotIndex, label plot index, offset from 1,
// nJustify, 0:Left; 1:Center; 2:Right.
void change_label_justify(int nLabelPlotIndex, int nJustify = 0) 
{
        string strLT;
        strLT.Format("range aa=%d;", nLabelPlotIndex); //this plot is label 
        
        string str;
        str.Format("set aa -tj %d;", nJustify);
        strLT += str;
        
        LT_execute(strLT);    
}

Turn Off Speed Mode

void TurnOffSpeedMode()
{
        GraphLayer gl = Project.ActiveLayer();
        if(gl)
        {
                Tree trFormat;
                trFormat = gl.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);
                
                TreeNode trSpeed = trFormat.Root.Speed.Matrix;
        // if the plot data is from worksheet, 
        // use the following line instead of the line above
        // TreeNode trSpeed = trFormat.Root.Speed.Worksheet;
        
                if( !trSpeed )
                        return;
                
                if( trSpeed.nVal )
                {
                        trSpeed.nVal = 0;
                        if( gl.ApplyFormat(trFormat, TRUE, TRUE) )
                                out_str("Turn off speed mode!");
                        
                }
                else
                {
                        out_str("Speed mode already be turn off!");
                }             
        }
}

Turn Off Speed Mode (Origin 7.5)

BOOL speed_mode_75(BOOL bOn = FALSE)
{
        BOOL bRes = FALSE;
        GraphLayer gl = Project.ActiveLayer();
        
        if( gl )
    {
                Tree trFormat;
                trFormat.Root.Page.Layers.All.Speed.Worksheet.nVal   = bOn;     // wks speed mode
                trFormat.Root.Page.Layers.All.Speed.Matrix.nVal              = bOn;     // matrix speed mode
                
                // Origin 7.5 does not have easy update ID mechanism, so we must use low level access
                // these IDs could be found comparing "Save Theme As" tree to OTH(xml) file generated by saving
                trFormat.Root.ID                                                                      = 0x00000001;      // root ID
                trFormat.Root.Page.ID                                                            = 0x00000004;      // page ID
                trFormat.Root.Page.Layers.ID                                                = 0x40000014;      // layer collection ID
                trFormat.Root.Page.Layers.All.ID                                       = 0x10000015;      // all layers ID
                trFormat.Root.Page.Layers.All.Speed.ID                            = 0x0000026F;      // speed branch ID
                trFormat.Root.Page.Layers.All.Speed.Worksheet.ID     = 0x00000270;      // worksheet speed mode ID
                trFormat.Root.Page.Layers.All.Speed.Matrix.ID                = 0x00000272;      // matrix speed mode ID
                
                bRes = gl.ApplyFormat(trFormat);
    }
    
    return bRes;
}

Setup Data Plot Property

Setup Property for Special Data Points

// set property for the special points on data plot.
void set_specialpoints()
{
   GraphLayer gl = Project.ActiveLayer();
   DataPlot dp = gl.DataPlots(0);
   
   Tree tr;
   tr.Root.Points.Point1.Index.nVal=0; // the index of the current editing point is 0(offset is 0)
   tr.Root.Points.Point1.Symbol.Size.dVal=20;        // pts
   tr.Root.Points.Point1.Symbol.Type.nVal=0;        // geometric
   tr.Root.Points.Point1.Symbol.Shape.nVal=1;        // square
   tr.Root.Points.Point1.Symbol.Interior.nVal=0;    // solid
   tr.Root.Points.Point1.Symbol.EdgeColor.nVal=2;   // green color
   
   tr.Root.Points.Point2.Index.nVal=2; // the index of the current editing point is 2(offset is 0)
   tr.Root.Points.Point2.Symbol.Size.dVal=20;        // pts
   tr.Root.Points.Point2.Symbol.Type.nVal=0;        // geometric
   tr.Root.Points.Point2.Symbol.Shape.nVal=1;        // square
   tr.Root.Points.Point2.Symbol.Interior.nVal=0;    // solid
   tr.Root.Points.Point2.Symbol.EdgeColor.nVal=3;   // blue color
   
   int iRet = dp.UpdateThemeIDs(tr.Root, "Error", "Unknown tag");
   bool bRet = dp.ApplyFormat(tr, true, true);     
}

Set Modifier

//use SetModifier to set plot label
void set_label_by_SetModifier()
{
        //prepare data
        Worksheet wks;
        wks.Create("origin");
        if(!wks)
                return;
        wks.AddCol();
        
        vector vn = {1,2,3,4,5};
        for(int ii = 0; ii < 3; ii++)
        {
                Dataset ds(wks, ii);
                ds = vn;
        }
        
        //plot
        DataRange dr;   
        dr.Add(wks, 0, "X");
        dr.Add(wks, 1, "Y");
        
        GraphPage gp;
        gp.Create("Origin");
        GraphLayer gl = gp.Layers();        
        gl.AddPlot(dr, IDM_PLOT_SCATTER);
        gl.Rescale();
        
        //set label
        Column col(wks, 2);
        DataPlot dp = gl.DataPlots(0); 
        if(!dp)
                return;
        dp.SetModifier(COLDESIG_PLOTLABEL_FORM, col);//see PlotDesignationEx for more modifier types.
        
        vector<int> vnDesigs;
        vector<string> saNames;
        dp.GetModifiers(vnDesigs, saNames);
        
        vector<uint> vecIndex;
        if(vnDesigs.Find(vecIndex, COLDESIG_PLOTLABEL_FORM) > 0) 
        {
                printf("label form = %s\n", saNames[ vecIndex[0] ]);
        }
}

//use ApplyFormat to set plot label
void set_label_by_applyformat()
{
        //prepare data
        Worksheet wks;
        wks.Create("origin");
        if(!wks)
                return;
        wks.AddCol();
        
        vector vn = {1,2,3,4,5};
        for(int ii = 0; ii < 3; ii++)
        {
                Dataset ds(wks, ii);
                ds = vn;
        }
        
        //plot
        DataRange dr;   
        dr.Add(wks, 0, "X");
        dr.Add(wks, 1, "Y");
        
        GraphPage gp;
        gp.Create("Origin");
        GraphLayer gl = gp.Layers();        
        gl.AddPlot(dr, IDM_PLOT_SCATTER);
        gl.Rescale();
        
        //set label
        DataPlot dp = gl.DataPlots(0);
        if(dp)
        {             
                Tree trFmt;
                TreeNode trEnable = trFmt.Root.Label.AddNode("Enable");
                trEnable.nVal = 1;
                
                OQCOLOR nLab;
                int ycol = 1, labelcol = 2;
                //convert the offset(modifier column to y column) to theme internal value
                okutil_get_ocolor_by_col(&nLab, labelcol, ycol, SYMB_COLOR_COLTYPE_INDEX);
                
                trFmt.Root.Label.Form.nVal = nLab;
                if ( 0 == dp.UpdateThemeIDs(trFmt.Root) )
                        dp.ApplyFormat(trFmt, true, true);
        }     
}