3.4.1 Add Graph Object



Version Info

Minimum Origin Version Required: Origin 8 SR0

Remark

We can create Graph Object like line, text, retangle on graph, worksheet or matrix window. More details about GraphObject class please reference to GraphObject and GraphLayer and PolyPolylineGraphObject

Add Text

void AddText()
{
    GraphPage gp;
    gp.Create("Origin");
    GraphLayer gl = gp.Layers(0); // get the first layer
    if(gl)
    {
    	// add a Text named MyText
        GraphObject    	go = gl.CreateGraphObject(GROT_TEXT);
        go.SetName("MyText");
        if( go )
        {
        	go.Text = "New Label";
        }        
        gl.GetPage().Refresh();
    }
}

Add Line

Keep a graph window ative and run below AddLine function to add one vertical line and one horizontal line.

void AddLine()
{
	GraphLayer gl = Project.ActiveLayer();
	if( gl )
	{		
		// add vertical line and set position
		GraphObject goVLine = gl.CreateGraphObject(GROT_LINE);
		goVLine.SetName("vline");
		goVLine.X = gl.X.From + (gl.X.To - gl.X.From) / 2; // set the init position of vertical line
		
		// set direction, script
		string strScript = "type -a " + " \"X = $(vline.X)\" "; // set this script, moving vertical line will print out X pos to Command window.
		SetLineFormat(goVLine, LN_VERTICAL, strScript, GRCT_MOVE);		
		
		
		// add horizontal line and set position
		GraphObject goHLine = gl.CreateGraphObject(GROT_LINE);
		goHLine.SetName("hline");			
		goHLine.Y = gl.Y.From + (gl.Y.To - gl.Y.From) / 2;	// set the init position of horizontal line
		
		// set direction, script
		strScript = "type -a " + " \"Y = $(hline.Y)\" "; // set this script, moving horizontal line will print out Y pos to Command window.
		SetLineFormat(goHLine, LN_HORIZONTAL, strScript, GRCT_MOVE);
	}
}

// set line object properties, script, format, color, width, attach mode, direction...
void SetLineFormat(GraphObject& goLine, int nDirection, string strScript, int nExecMode)
{
	goLine.Attach = 2;// 2, layer and scale
	
	Tree tr;		
	tr.Root.Span.nVal = 1; // 1 to span over the whole layer
	tr.Root.Direction.nVal = nDirection; // vertical or horizontal	
	tr.Root.Color.nVal = SYSCOLOR_RED;
	tr.Root.Width.dVal = 3;
	tr.Root.Event.nVal = nExecMode;
	tr.Root.Script.strVal = strScript; // set script, run it after the Event specified by nExecMode
	
	if( 0 == goLine.UpdateThemeIDs(tr.Root) ) // 0 means no error
	{
		bool bRet = goLine.ApplyFormat(tr, true, true); // return true if applied format successfully
	}
}

Add Rectangle

void AddRectangle()
{
	GraphPage gp;
	gp.Create("Origin");
	
	GraphLayer gl = gp.Layers(0);	
	GraphObject goRect = gl.CreateGraphObject(GROT_RECT);	
}

Add Curved Arrow Tool

This example shows how to add a curved arrow line on graph window, and how to change vertices of curve arrow line. In the following function, if replace GROBJ_TN_LINE4 to GROT_POLYLINE, the graph object will be Polyline.

#include <..\Originlab\grobj_utils.h>
void graph_object_line4_ex()
{
	GraphPage gp;
	gp.Create("origin");
	GraphLayer gl = gp.Layers();	

	GraphObject go;
	go = gl.CreateGraphObject(GROBJ_TN_LINE4);
	
	go.Attach = ATTACH_TO_SCALE;
	
	Tree tr;
	tr = go.GetFormat(FPB_DATA, FOB_ALL, true, true);
	
	// to take a look on default position
	vector vx0, vy0;
	vx0 = tr.Root.Data.X.dVals;
	vy0 = tr.Root.Data.Y.dVals;
	
	// to set the proper position for four points
	vector vx = {2.2, 4.7, 6.58, 7.16};
	vector vy ={5.7, 4.22, 5.12, 7.35};
	tr.Root.Data.X.dVals = vx;
	tr.Root.Data.Y.dVals = vy;
    tr.Root.Arrow.End.Style.nVal = 1; // add arrow to the end
	
	int nErr = go.UpdateThemeIDs(tr.Root);
	if(nErr == 0)
	{
		go.ApplyFormat(tr, true, true);
	}
	else
	{
		out_int("err = ", nErr);
	}
}

Add Polygon Tool

#include <..\Originlab\grobj_utils.h>
void graph_object_create_polygon_ex()
{
    GraphPage gp;
    gp.Create("origin");
    GraphLayer gl = gp.Layers();    
 
    GraphObject go;
    go = gl.CreateGraphObject(GROT_POLYGON); // create polygon by type id
 
    go.Attach = ATTACH_TO_SCALE;
 
    Tree tr;
    tr = go.GetFormat(FPB_DATA, FOB_ALL, true, true);// FPB_DATA bit to control the accessing of vertices of graph object
 
    // When polygon is just created, there are no vertices, so need to set them
    // The size of vx and vy vectors below will determine the number of points of the graph object
    Tree trNew;
    vector vx = {2.4, 4.6, 7.3, 6.8};
    vector vy ={4.8, 7.9, 7.2, 1.6};
    trNew.Root.Data.X.dVals = vx;
    trNew.Root.Data.Y.dVals = vy;
 
    int nErr = go.UpdateThemeIDs(trNew.Root);
    if(nErr == 0)
    {
        go.ApplyFormat(trNew, true, true);
    }
    else
    {
        out_int("err = ", nErr);
    }
}

Add Polypolygon

static void make_circle(vector& vx, vector& vy, int nSize, double x0, double y0, double rr)
{
	vx.SetSize(nSize);
	vy.SetSize(nSize);

	double inc = 2.* pi/nSize;
	double ang=0;
	for(int ii = 0; ii <nSize; ii++, ang+= inc)
	{
		vx[ii] = x0 + rr* sin(ang);
		vy[ii] = y0 + rr* cos(ang);
	}
}

void many_circles(int nRegNum = 100)
{
	GraphLayer gl = Project.ActiveLayer();
	double x1, x2, y1, y2, x0, y0, rr;
	x1 = gl.X.From, x2 = gl.X.To;
	y1 = gl.Y.From, y2 = gl.Y.To;

	PolyPolylineGraphObject go;
	go = gl.CreateGraphObject(GROT_POLYPOLYGON, "PP");
	go.Attach = ATTACH_TO_SCALE;
	// Set Label for polypolygon
	go.SetDrawOption(POLYPOLYLINE_DRAW_OPTION_NEED_TEXT);

	vector<double> vx, vy; 
	for(int ii = 0; ii < nRegNum ; ii ++)
	{
		x0 = x1 + (x2-x1) * rnd();
		y0 = y1 + (y2-y1) *rnd();
		rr = (x2-x1)/50 + rnd() * (x2-x1)/100;
		make_circle(vx, vy, 40, x0, y0, rr);
		go.AddPolyline(vx, vy);
	}
	
	Tree tr;
	tr.Root.Fill.Color.nVal = INDEX_COLOR_TRANSPARENT;
	tr.Root.Border.Color.nVal = SYSCOLOR_YELLOW;
	tr.Root.Border.Width.dVal = 0.2;
	
	tr.Root.States.nVal = GOC_NO_SELECT;
	
	int nRet = go.UpdateThemeIDs(tr.Root);
	go.ApplyFormat(tr, true, true);

	gl.GetPage().Refresh();
	out_str("done");
}