1.7.4 Managing Layers


Creating a Panel Plot

Creating a 6 Panel Graph

The following example will create a new graph window with 6 layers, arranged as 2 columns and 3 rows. This function can be run independent of what window is active.

GraphPage gp;
gp.Create("Origin");

while(gp.Layers.Count() < 6)
{
	gp.AddLayer();
}

graph_arrange_layers(gp, 3, 2);

Creating and Plotting into a 6 Panel Graph

The following example will import some data into a new workbook, create a new graph window with 6 layers, arranged as 2 columns and 3 rows, and loop through each layer (panel), plotting the imported data.

// Import data file to worksheet
ASCIMP  	ai;
Worksheet 	wks;
string	strDataFile = GetOpenBox(FDLOG_ASCII, GetAppPath(true));
if(AscImpReadFileStruct(strDataFile,&ai) == 0)
{		
	wks.Create("Origin");
	wks.ImportASCII(strDataFile, ai);
}

// Add XY data from worksheet to graph each layers
GraphPage gp("Graph1"); // the graph has the 3x2 panel layers created above
int index = 0;
foreach(GraphLayer gl in gp.Layers)
{
	DataRange dr;
	dr.Add(wks, 0, "X");
	dr.Add(wks, index+1, "Y");
	
	if( gl.AddPlot(dr, IDM_PLOT_LINE) >= 0 )
		gl.Rescale();
	
	index++;
}

Adding Layers to a Graph Window

The following example will add an independent right Y axis scale. A new layer is added, displaying only the right Y axis. It is linked in dimension and the X axis is linked to the current active layer at the time the layer is added. The new added layer becomes the active layer.

Before compiling the following codes, you need to add graph_utils.c to your current workspace. Run Labtalk command "Run.LoadOC(Originlab\graph_utils.c)" to do this.

#include <..\Originlab\graph_utils.h>// Needed for page_add_layer function

GraphLayer gl = Project.ActiveLayer();
GraphPage gp = gl.GetPage();

bool bBottom = false, bLeft = false, bTop = false, bRight = true;
int nLinkTo = gl.GetIndex(); // New added layer link to the active layer
bool bActivateNewLayer = true;

int	nLayerIndex = page_add_layer(gp, bBottom, bLeft, bTop, bRight, 
ADD_LAYER_INIT_SIZE_POS_SAME_AS_PREVIOUS, bActivateNewLayer, nLinkTo);

Hiding Layers Except Active One

GraphPage gp("Graph1");
if( gp )
{
	GraphLayer glActive = gp.Layers(-1); // -1 to get active layer
	
	foreach(GraphLayer gl in gp.Layers)
	{
		if( gl.GetIndex() != glActive.GetIndex() )
			gl.Show(false);
	}
}

Arranging the Layers

The following example will arrange the existing layers on the active graph into two rows by three columns. If the active graph does not already have 6 layers, it will not add any new layers. It arranges only the layers that exist.

GraphLayer gl = Project.ActiveLayer();
GraphPage gp = gl.GetPage();

int nRows = 3, nCols = 2;
graph_arrange_layers(gp, nRows, nCols);

Moving a Layer

The following example will left align all layers in the active graph window, setting their position to be 15% from the left-hand side of the page.

GraphLayer gl = Project.ActiveLayer();
GraphPage gp = gl.GetPage();

int nRows = gp.Layers.Count();
int nCols = 1;

stLayersGridFormat stFormat;
stFormat.nXGap = 0; // the X direction gap of layers
stFormat.nYGap = 5; // the Y direction gap of layers
stFormat.nLeftMg = 15; // left margin
stFormat.nRightMg = 10;
stFormat.nTopMg = 10;	
stFormat.nBottomMg = 10;

page_arrange_layers(gp, nRows, nCols, &stFormat);

Resizing a Layer

The following example will resize the current layer to reduce the width and height to half of the original size.

Before compiling the following codes, you need to add graph_utils.c to your current workspace. Run Labtalk command "Run.LoadOC(Originlab\graph_utils.c)" to do this.

#include <..\Originlab\graph_utils.h> // Needed for layer_set_size function
GraphLayer gl = Project.ActiveLayer();

// get the original size of graph layer
double dWidth, dHeight;
layer_get_size(gl, dWidth, dHeight);

// resize layer
dWidth /= 2;
dHeight /= 2;
layer_set_size(gl, dWidth, dHeight);

Swap two Layers

The following example will swap the position on the page of layers indexed 1 and 2.

Before compiling the following codes, you need to add graph_utils.c to your current workspace. Run Labtalk command "Run.LoadOC(Originlab\graph_utils.c)" to do this.

#include <..\Originlab\graph_utils.h> // Needed for layer_swap_position function
GraphPage gp("Graph1");
GraphLayer gl1 = gp.Layers(0);
GraphLayer gl2 = gp.Layers(1);

layer_swap_position(gl1, gl2);

The following example will swap the position on the page of layers named Layer1 and Layer2.

GraphPage gp("Graph1");
GraphLayer gl1 = gp.Layers("Layer1");
GraphLayer gl2 = gp.Layers("Layer2");

layer_swap_position(gl1, gl2);

Aligning Layers

The following example will bottom align layer 2 with layer 1 in the active graph window.

Before compiling the following codes, you need to add graph_utils.c to your current workspace. Run Labtalk command "Run.LoadOC(Originlab\graph_utils.c)" to do this.

#include <..\Originlab\graph_utils.h> // Needed for layer_aligns function
// Get the active graph page
GraphLayer gl = Project.ActiveLayer();
GraphPage gp = gl.GetPage();

GraphLayer gl1 = gp.Layers(0);
GraphLayer gl2 = gp.Layers(1);

// Bottom align layer 2 with layer 1
layer_aligns(gl1, gl2, POS_BOTTOM);

Linking Layers

The following example will link all X axes in all layers in the active graph to the X axis of layer 1. The Units will be set to a % of Linked Layer.

Before compiling the following codes, you need to add graph_utils.c to your current workspace. Run Labtalk command "Run.LoadOC(Originlab\graph_utils.c)" to do this.

#include <..\Originlab\graph_utils.h> // Needed for layer_set_link function
GraphLayer gl = Project.ActiveLayer();
GraphPage gp = gl.GetPage();
GraphLayer gl1 = gp.Layers(0); // Layer 1

foreach(GraphLayer glOne in gp.Layers)
{
	int nUnit = M_LINK; // Set layer unit as % of linked layer
	if( glOne != gl1 )
		layer_set_link(glOne, gl1.GetIndex(), LINK_STRAIGHT, LINK_NONE, &nUnit);
}

Setting Layer Unit

int nUnit = M_PIXEL;
GraphLayer gl = Project.ActiveLayer();

// Get the current position
double dPos[TOTAL_POS];
gl.GetPosition(dPos);

// Convert position to the specified unit
gl.UnitsConvert(nUnit, dPos);

// Set position with unit
gl.SetPosition(dPos, nUnit);