1.7.1 Creating and Customizing Graph

Creating Graph Window

The Create method is used for creating new graphs.

GraphPage gp;
gp.Create("3D"); // create a graph using the 3D template

Getting Graph Page Format

GraphPage gp("Graph1");

Tree tr;
tr = gp.GetFormat(FPB_ALL, FOB_ALL, true, true);
out_tree(tr);

Setting Graph Page Format

The following example code shows how to set page background color as a gradient in two colors.

Tree tr;
tr.Root.Background.BaseColor.nVal = SYSCOLOR_RED;
tr.Root.Background.GradientControl.nVal = 1;
tr.Root.Background.GradientColor.nVal = SYSCOLOR_BLUE;

GraphPage gp("Graph1");
if(0 == gp.UpdateThemeIDs(tr.Root) )
	gp.ApplyFormat(tr, true, true);

Getting Graph Layer Format

GraphLayer gl = Project.ActiveLayer();

Tree tr;
tr = gl.GetFormat(FPB_ALL, FOB_ALL, true, true);
out_tree(tr);

Setting Graph Layer Format

The following example code shows how to set the background of a graph layer object to Black Line format.

GraphLayer gl = Project.ActiveLayer();

Tree tr;
tr.Root.Background.Border.Color.nVal = SYSCOLOR_BLACK;
tr.Root.Background.Border.Width.nVal = 1;
tr.Root.Background.Fill.Color.nVal = SYSCOLOR_WHITE;

if( 0 == gl.UpdateThemeIDs(tr.Root) )
	gl.ApplyFormat(tr, true, true);

Show Additional Lines

This example shows how to show additional lines, the Y=0/X=0 line, and the opposite line.

GraphLayer gl = Project.ActiveLayer();
Axis axesX = gl.XAxis;	

axesX.Additional.ZeroLine.nVal = 1; // Show Y = 0 line
axesX.Additional.OppositeLine.nVal = 1; // Show X Axes opposite line

Show Grid Lines

This example shows how to set gridlines to show, and how to color them.

Color values can be an index into Origin's internal color palette or an RGB value. See Color in the Data Types and Variables section for more information about working with color values.

GraphLayer gl = Project.ActiveLayer();
Axis axisY = gl.YAxis;
Tree tr;	

// Show major grid
TreeNode trProperty = tr.Root.Grids.HorizontalMajorGrids.AddNode("Show");
trProperty.nVal = 1;
tr.Root.Grids.HorizontalMajorGrids.Color.nVal = RGB2OCOLOR(RGB(100, 100, 220));
tr.Root.Grids.HorizontalMajorGrids.Style.nVal = 1; // Solid
tr.Root.Grids.HorizontalMajorGrids.Width.dVal = 1;

// Show minor grid
trProperty = tr.Root.Grids.HorizontalMinorGrids.AddNode("Show");
trProperty.nVal = 1;
tr.Root.Grids.HorizontalMinorGrids.Color.nVal = SYSCOLOR_GREEN; // Green
tr.Root.Grids.HorizontalMinorGrids.Style.nVal = 2; // Dot
tr.Root.Grids.HorizontalMinorGrids.Width.dVal = 0.3;

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

Setting Axis Scale

This example shows how to set scale parameters, increment, type and so on.

GraphLayer gl = Project.ActiveLayer();
Axis axesX = gl.XAxis;  
 
axesX.Scale.From.dVal = 0;
axesX.Scale.To.dVal = 1;
axesX.Scale.IncrementBy.nVal = 0; // 0=increment by value; 1=number of major ticks
axesX.Scale.Value.dVal = 0.2; // Increment value
axesX.Scale.Type.nVal = 0;// Linear 
axesX.Scale.Rescale.nVal = 0; // Rescake type
axesX.Scale.RescaleMargin.dVal = 8; // precent 8

This example shows how to set scale major ticks number for Y axis.

GraphLayer gl = Project.ActiveLayer();
Axis axesY = gl.YAxis; 

axesY.Scale.IncrementBy.nVal = 1; // 0: increment by value; 1: number of major ticks
axesY.Scale.MajorTicksCount.nVal = 5;

Getting Axis Format

GraphLayer gl = Project.ActiveLayer();
Axis axisX = gl.XAxis;

// Get all axis format settings to tree
Tree tr;
tr = axisX.GetFormat(FPB_ALL, FOB_ALL, true, true);
out_tree(tr);

Setting Axis Label

An axis label is an ordinary text object and is accessed in Origin C using the GraphObject class. On a default graph the X axis is named XB and the Y axis is named YL. The following code shows how to access the X and Y axis labels and assumes a default graph is the active page.

GraphLayer gl = Project.ActiveLayer();    // Get active graph layer

GraphObject grXL = gl.GraphObjects("XB"); // Get X axis label
GraphObject grYL = gl.GraphObjects("YL"); // Get Y axis label

Now that we have access to the axis labels we can change their values. The following code sets the X axis label directly and sets the Y axis label indirectly by linking it to a LabTalk string variable. Linking to a LabTalk variable requires the label's Programming Control option "Link to variables" to be turned on. This option is on by default.

grXL.Text = "My New X Asis Label";

LT_set_str("abc$", "My String Variable");
grYL.Text = "%(abc$)";

To make sure the label changes appear, it may be necessary to refresh the graph page. With our GraphLayer object we can refresh the page with the following code.

gl.GetPage().Refresh();

Show Top Axis

This example shows how to show X top axes.

// Show axes and ticks
Tree tr;	
TreeNode trProperty = tr.Root.Ticks.TopTicks.AddNode("Show");
trProperty.nVal = 1;

// Show tick labels
trProperty = tr.Root.Labels.TopLabels.AddNode("Show");
trProperty.nVal = 1;

GraphLayer gl = Project.ActiveLayer();
Axis axesX = gl.XAxis;
if(0 == axesX.UpdateThemeIDs(tr.Root) )
{
	bool bRet = axesX.ApplyFormat(tr, true, true);
}

Customizing Axis Ticks

This example shows how to set the format in the Axis dialog -> Title & Format tab.

GraphLayer gl = Project.ActiveLayer();
Axis axesX = gl.XAxis;	

Tree tr;
// Set ticks color as Auto, depend on the color of data plot
tr.Root.Ticks.BottomTicks.Color.nVal = INDEX_COLOR_AUTOMATIC;
tr.Root.Ticks.BottomTicks.Width.dVal = 3;
tr.Root.Ticks.BottomTicks.Major.nVal = 0; // 0: In and Out
tr.Root.Ticks.BottomTicks.Minor.nVal = 2; // 2: Out
tr.Root.Ticks.BottomTicks.Style.nVal = 0; // Solid

if(0 == axesX.UpdateThemeIDs(tr.Root) )
    bool bRet = axesX.ApplyFormat(tr, true, true);

Customizing Tick Labels

This example shows how to set tick labels with custom positions. It performs the same action as going in the Axis dialog Custom Tick Labels tab.

GraphLayer gl = Project.ActiveLayer();
Axis axesX = gl.XAxis;

Tree tr;
// Show axes begin and end as scale value
tr.Root.Labels.BottomLabels.Custom.Begin.Type.nVal = 2;	
tr.Root.Labels.BottomLabels.Custom.End.Type.nVal = 2;

// Set special point as Manual type with the special value and text.
tr.Root.Labels.BottomLabels.Custom.Special.Type.nVal = 3;
tr.Root.Labels.BottomLabels.Custom.Special.Label.strVal = "Mid";
tr.Root.Labels.BottomLabels.Custom.Special.Value.dVal = 12;

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

Change Scale Factor

This example shows how to scale the font size and line thickness when page size changed by changing scale factor.

void change_graph_and_font_size(double dNewWidth = 5)
{
	GraphPage gp = Project.ActiveLayer().GetPage();
	Tree tr1;tr1 = gp.GetFormat(FPB_ALL, FOB_ALL, true, true);
	double dOldWidth = tr1.Root.Dimension.Width.dVal;
	double factor = dOldWidth/tr1.Root.Dimension.Height.dVal;
	Tree tr2;
	tr2.Root.Dimension.Width.dVal = dNewWidth;
	tr2.Root.Dimension.Height.dVal = dNewWidth / factor;
	if(0 == gp.UpdateThemeIDs(tr2.Root))
	{
		gp.ApplyFormat(tr2, true, true);
		string strScript;
		//page -afu : Change Scale Factor
		//win -z0 : fit page to window size
		strScript.Format("page -AFU %f;win -z0", -dNewWidth/dOldWidth);
		gp.LT_execute(strScript);
	}
}
Note:

In this case, tr1 is only used to get current dimension value and does not apply new value.

It's because tr1 also has graph object position settings,which are outdated if you want to change graph dimension.
The correct way is to apply dimension setting only and graph object position will automatically update.