1.7.5 Creating and Accessing Graphical Objects

Creating Graphical Object

Add a Graphical Object, for example: text, or a rectangle or line.

The following example shows how to add a rectangle to the active graph. For other Graph object types see GROT_* (for example: GROT_TEXT, GROT_LINE, GROT_POLYGON) in the oc_const.h file.

GraphLayer gl = Project.ActiveLayer();
string strName = "MyRect";
GraphObject goRect = gl.CreateGraphObject(GROT_RECT, strName);

Add a text label on the current graph window:

GraphLayer gl = Project.ActiveLayer();
GraphObject go = gl.CreateGraphObject(GROT_TEXT, "MyText");		
go.Text = "This is a test";

The example below shows how to add an arrow to a graph. The object type of an arrow is GROT_LINE, the same type as a line. And for both lines and arrows, the number of data points required is 2.

GraphPage gp;
gp.Create();
GraphLayer gl = gp.Layers();

string strName = "MyArrow"; // the name of the graph object
GraphObject go = gl.CreateGraphObject(GROT_LINE, strName);

go.Attach = 2; // change attach mode to Layer and Scale

Tree tr;		
tr.Root.Dimension.Units.nVal = 5; // Set unit as Scale

// Set position by scale value
vector vx = {2, 6};
vector vy = {6, 2};
tr.Root.Data.X.dVals = vx;
tr.Root.Data.Y.dVals = vy;	

tr.Root.Arrow.Begin.Style.nVal = 0;
tr.Root.Arrow.End.Style.nVal = 1;

if( 0 == go.UpdateThemeIDs(tr.Root) )
{
	go.ApplyFormat(tr, true, true);
}

The example below shows how to add a curved arrow to a graph. For a curved arrow, the number of data points required is 4.

GraphPage gp;
gp.Create();
GraphLayer gl = gp.Layers();

string strName = "MyArrow"; // the name of the graph object
GraphObject go = gl.CreateGraphObject(GROT_LINE4, strName);

go.Attach = 2; // change attach mode to Layer and Scale

Tree tr;		
tr.Root.Dimension.Units.nVal = 5; // Set unit as Scale

// Set position by scale value
vector vx = {2, 4, 6, 5};
vector vy = {7, 6.9, 6.8, 2};
tr.Root.Data.X.dVals = vx;
tr.Root.Data.Y.dVals = vy;	

tr.Root.Arrow.Begin.Style.nVal = 0;
tr.Root.Arrow.End.Style.nVal = 1;

if( 0 == go.UpdateThemeIDs(tr.Root) )
{
	go.ApplyFormat(tr, true, true);
}

Setting Properties

Set Properties for a Graphical Object, for example, text font, color, line width.

// Set color and font for graph object	
GraphLayer gl = Project.ActiveLayer();
GraphObject goText = gl.GraphObjects("Text");
goText.Text = "This is a test";
goText.Attach = 2; // Attach to layer scale	

Tree tr;
tr.Root.Color.nVal = SYSCOLOR_RED; // the color of text
tr.Root.Font.Bold.nVal = 1; 
tr.Root.Font.Italic.nVal = 1;
tr.Root.Font.Underline.nVal = 1;
tr.Root.Font.Size.nVal = 30; // font size of text

if( 0 == goText.UpdateThemeIDs(tr.Root) )
{
	bool bRet = goText.ApplyFormat(tr, true, true);
}

Setting Position and Size

GraphLayer gl = Project.ActiveLayer();
GraphObject go = gl.GraphObjects("Rect");
go.Attach = 2; // Attach to layer scale	

// Move text object to the layer left top
Tree tr;
tr.Root.Dimension.Units.nVal = UNITS_SCALE;
tr.Root.Dimension.Left.dVal = gl.X.From; // Left
tr.Root.Dimension.Top.dVal = gl.Y.To/2; // Top
tr.Root.Dimension.Width.dVal = (gl.X.To - gl.X.From)/2; // Width
tr.Root.Dimension.Height.dVal = (gl.Y.To - gl.Y.From)/2; // Height

if( 0 == go.UpdateThemeIDs(tr.Root) )
{
	bool bRet = go.ApplyFormat(tr, true, true);
}

Updating Attach Property

The attach property has 3 choices, Page, Layer Frame, and Layer Scale.

// Attach graph object to the different object:
// 0 for layer, when move layer, graph object will be moved together;
// 1 for page, when move layer, not effect on graph object;
// 2 for layer scale, when change the scale, the position of graph object
// will be changed according.
go.Attach = 2;

Getting and Setting Disable Property

// To check disable properties, for example, movable, selectable.
Tree tr;
tr = go.GetFormat(FPB_OTHER, FOB_ALL, true, true);
DWORD 	dwStats = tr.Root.States.nVal;

// To check vertical and horizontal movement.
// More property bits, see GOC_* in oc_const.h file.
if( (dwStats & GOC_NO_VMOVE) && (dwStats & GOC_NO_HMOVE) )
{
	out_str("This graph object cannot be move");
}

Programming Control

// 1. Add a line
GraphLayer gl = Project.ActiveLayer();
GraphObject go = gl.CreateGraphObject(GROT_LINE);
go.Attach = 2; // Set attach mode to layer scale
go.X = 5; // Set init position to X = 5

// 2. Set line properties
Tree tr;
tr.Root.Direction.nVal = 2; // 1 for Horizontal, 2 for vertical
tr.Root.Span.nVal = 1; // Span to layer 
tr.Root.Color.nVal = SYSCOLOR_RED; // Line color

if( 0 == go.UpdateThemeIDs(tr.Root) )	
{
	go.ApplyFormat(tr, true, true);
}

// 3. Set event mode and LT script. 
// Move line will print out line position, x scale value.
Tree trEvent;
trEvent.Root.Event.nVal = GRCT_MOVE;// More other bits, see GRCT_* in oc_const.h
trEvent.Root.Script.strVal = "type -a $(this.X)";

if( 0 == go.UpdateThemeIDs(trEvent.Root) )	
{
	go.ApplyFormat(trEvent, true, true);
}

Updating Legend

A legend is a graphical object named "Legend" on a graph window. After adding/removing data plots, we can use the legend_update function to refresh the legend according to the current data plots.

 
// Simple usage here, just used to refresh legend. 
// Search this function in OriginC help to see the description of other arguments 
// for more usages.
legend_update(gl); // gl is a GraphLayer object

Adding Table Object on Graph

// 1. Create the worksheet with Table template
Worksheet wks;
wks.Create("Table", CREATE_HIDDEN); 
WorksheetPage wksPage = wks.GetPage();

// 2. Set table size and fill in text
wks.SetSize(3, 2);
wks.SetCell(0, 0, "1");
wks.SetCell(0, 1, "Layer 1");

wks.SetCell(1, 0, "2");
wks.SetCell(1, 1, "Layer 2");

wks.SetCell(2, 0, "3");
wks.SetCell(2, 1, "Layer 3");

//3. Add table as link to graph
GraphLayer gl = Project.ActiveLayer();	
GraphObject grTable = gl.CreateLinkTable(wksPage.GetName(), wks);