3.4.5 Modify Graph Object Position



Version Info

Minimum Origin Version Required: Origin 8 SR0

Modify Text Position by Class

// modify Text object position by methods in GraphObject class
void    Set_Text_Pos_ex1(string strName = "MyText")
{
    GraphLayer		gl = Project.ActiveLayer();    
    if( gl )
    {
    	GraphObject 	grobj = gl.GraphObjects(strName);	// Get the graphic object by name
	    if( grobj )
	    {  
	    	grobj.Attach = 2; // attach to axes scales
	    	grobj.X = gl.X.From + grobj.DX / 2.0; 	// grobj.X is the X coordinate of the center of the object 
	    	grobj.Y = gl.Y.To - grobj.DY / 2.0;		// grobj.Y is the Y coordinate of the center of the object 
	    }
    }
}

Modify Text Position by Theme Tree

// modify Text object position by Theme Tree
void    Set_Text_Pos_ex2(string strName = "MyText")
{
    GraphLayer		gl = Project.ActiveLayer();    
    if( gl )
    {
    	GraphObject 	grobj = gl.GraphObjects(strName);
 	    if( grobj )
	    {
	    	Tree tr;
	    	tr.Root.Dimension.Units.nVal = 5;
	    	tr.Root.Dimension.Attachment.nVal = 2;	// attach to axes scales
	    	tr.Root.Dimension.Left.dVal = gl.X.From;
	    	tr.Root.Dimension.Top.dVal = gl.Y.To;
	    	if( 0 == grobj.UpdateThemeIDs(tr.Root) )
	    	{
	    		if( grobj.ApplyFormat(tr, true, true) )
	    			gl.GetPage().Refresh();
	    	}
	    }
    }
}

Modify Rectangle Position by Theme Tree

// Modify rectangle graph object position and size
void    Set_Rect_Pos_ex1()
{
	GraphPage gp;
	gp.Create("Origin");
	GraphLayer gl = gp.Layers();
	
	// create a graph object
	string 		strName = "MyRect";
	GraphObject goText = gl.CreateGraphObject(GROT_RECT);
	goText.SetName(strName);
	
    GraphObject	grobj = gl.GraphObjects(strName); // get this text by name
    if( grobj )
    {
    	// change position and size by theme tree
        Tree tr;  
        tr.Root.Dimension.Units.nVal = 5; //UNITS_INCH = 0, UNITS_CM = 1, UNITS_MM = 2, UNITS_PIXEL = 3, UNITS_POINT = 4, UNITS_SCALE = 5
        tr.Root.Dimension.Attachment.nVal = 2;  // attach to axes scales
        
        double left = gl.X.From;
        double right = (gl.X.To - gl.X.From) / 2;
        double bottom = gl.Y.To;
        double top = (gl.Y.To - gl.Y.From) / 2;
        tr.Root.Dimension.Left.dVal = left;
        tr.Root.Dimension.Top.dVal = top;
        tr.Root.Dimension.Width.dVal = right - left;
        tr.Root.Dimension.Height.dVal = top - bottom;
      
        if( 0 == grobj.UpdateThemeIDs(tr.Root) ) // updated IDs to treenodes
        {
            if( grobj.ApplyFormat(tr, true, true) )
            {
            	gl.GetPage().Refresh();
            }
        }
    }    
}

Modify Vertices of a Polygon

// this example shows how to modify the vertices of an existing polygon object
#include <..\Originlab\grobj_utils.h>
void graph_object_modify_polygon_ex()
{
    // Assume graph layer is active and polgon object named "polygon" exists
    GraphLayer gl = Project.ActiveLayer();     
    GraphObject go;
    go = gl.GraphObjects("polygon");
    if( go )
    {
    	go.Attach = ATTACH_TO_SCALE;
    	
        // Get format to tree
        Tree tr;
        tr = go.GetFormat(FPB_DATA, FOB_ALL, true, true); // FPB_DATA bit to control the accessing of vertices of graph object
 
        // Get current vertices
        vector vx, vy;
        vx = tr.Root.Data.X.dVals;
        vy = tr.Root.Data.Y.dVals;
 
        // Make some chagnes to vertex values
        // Note: It is assumed here that the polygon object is attached to Layer and Scale, otherwise changing the vertices
        // by scaling may place the object outside of page
        vx += 2; // Add some offset to x
        vy /= 2; // Scale the y vertices
 
        // Set vectors back to tree
        tr.Root.Data.X.dVals = vx;
        tr.Root.Data.Y.dVals = vy;
        
        // Set format to apply changes to object
        int nErr = go.UpdateThemeIDs(tr.Root);
        if(nErr == 0)
        {
            go.ApplyFormat(tr, true, true);
        }
        else
        {
            out_int("err = ", nErr);
        }
    }    
}

Mofidy Graph Object Position on Newly Created Graph Window

Please notice the LT command doc -UWF in following example function. This command used to force to update window to draw newly created window, however, the position of XB(X axis title), YL(Y axis title) and so on will be wrong. Once you want to get the real position of build-in graph object in newly created window, like XB, YL, Legend, need to call this command before getting position.

void Set_Line_Pos_ex1()
{   
   // create page and get first layer
   GraphPage oPage;
   oPage.Create("Origin");
   GraphLayer oLayer = oPage.Layers(0);

   LT_execute("doc -UWF;"); // force update window to draw however the position of XB would be wrong
   
   // get the Y position of the x axis title
   GraphObject goXB = oLayer.GraphObjects("XB");    
   double dY = goXB.Y;
   printf("Y-position of XB (%f) \n", dY);

   // add line graph object 
   GraphObject goLine = oLayer.CreateGraphObject(GROT_LINE);
   
   // format line
   Tree tr;
   tr.Root.Dimension.Attachment.nVal = 2; // 2: layer and scale
   tr.Root.Direction.nVal = 1;
   tr.Root.Span.nVal = 1;
   if(0 == goLine.UpdateThemeIDs(tr.Root) )
		goLine.ApplyFormat(tr, true, true);
   
   // to set the position of the line to the Y position of the X-axis label
   goLine.Y = dY;
}

Get Real World (Axes) Coordinates of Graph Object

Since object.x, object.y etc are not in real world coordinates but converted from pixel positions, and as a result, they do not have the precision to reflect the real values that the object has been set with. From LabTalk, you can access the real axes coordinates array by object.x# and object.y#, where x# and y# are the real position of the #th point of an object. Straight lines and arrows have 2 points, rectangles and circles have 4 points and other objects may have many points. In the following example, the same axes positions can be gotten by Origin C code as these LabTalk commands read.

Line

To run this example, a graph window must exist in the project and the active layer in the graph should contain a vertical line named "VLine". To get these requirements, run the following LabTalk script first.

win -n plot;
draw -n VLine -d 0 -l -v 5;
void Get_Graph_Object_Axes_Pos_ex1()
{
    // get the active layer
    GraphLayer gl = Project.ActiveLayer();
    if(gl)  // if the active layer exist
    {
        // get the line "VLine"
        GraphObject goLine = gl.GraphObjects("VLine");
        if(goLine)    // if the line exist
        {
            Tree trFormat;    // define a Tree variable
            
            // FPB_DATA is not part of FPB_ALL, as getting data is slower so must explicitly ask
            trFormat = goLine.GetFormat(FPB_DATA, FOB_OBJECT, true, true);
 
            vector vX, vY;  // define two vector to hold positions of X and Y
            vX = trFormat.Root.Data.X.dVals;   // get X positions
            vY = trFormat.Root.Data.Y.dVals;   // get Y positions
            
            for(int i=0; i<vX.GetSize(); i++)  // loop to print X and Y positions
            {
                printf("\nx[%d]=%f,  y[%d]=%f", i, vX[i], i, vY[i]);
            }
        }
        else   // if the line not exist
        {
            out_str("No graph object named \"VLine\" exists!");
        }
    }
    else   // if active graph layer not exist
    {
        out_str("No graph layer is active!");
    } 
}