The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Going crazy with axis range
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

rikseventyseven

Switzerland
10 Posts

Posted - 09/10/2009 :  11:22:16 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Dear Developers,

I'm going crazy trying to set the axis range of a graph... NO WAY!

Here is my code. I tryed to use any possible combination of refresh and redraw but the axes stay always as origin drawn them from the beginning...

>> -----------------------------------------------------------------------
GraphPage gp; //create new graph page
gp.Create("origin");
gp.SetLongName(dsName + " - VI");
gp.Rename("VI"+dsName);

GraphLayer gl(gp.GetName(), 0);

int nIndex = 0;
int nPlotExp = gl.AddPlot(drExp, IDM_PLOT_LINESYMB); // Here we plot the Experimental data
int nPlotFit = gl.AddPlot(drFit, IDM_PLOT_LINE); // Here we plot the fitted data

DataPlot dpE = gl.DataPlots(nPlotExp); // Get first data plot in graph layer
dpE.SetColor(0, TRUE); // Change color to black
DataPlot dpF = gl.DataPlots(nPlotFit); // Get second data plot in graph layer
dpF.SetColor(tr.Misc.LineColor.nVal, TRUE); // Change color to what has been chosen by the user
gl.Rescale(); //refresh the layer.

//// Get data plot from collection in graph layer
DataPlot dp = gl.DataPlots(0);
if( !dp.IsValid() )
return FALSE;
////
////// Access plot properties using tree


Tree tr1;
//---------------------------------------------------------------
tr1.Root.Axes.X.Ticks.TopTicks.show.nVal = 1;
tr1.Root.Axes.Y.Ticks.RightTicks.show.nVal = 1;
//assign node IDs to all nodes in trFormat, return number of errors
int nErr = gl.UpdateThemeIDs( tr1.Root ) ;
if(0 != nErr)
out_str("Fail to Update Theme IDs, theme tree has wrong structure");
gl.ApplyFormat( tr1, true, true, true );

// then change settings can be done after needed axes are already turned on
// Set the major minor ticks to In format for all XY axes
tr1.Root.Axes.All.Ticks.All.Major.nVal = 1;
tr1.Root.Axes.All.Ticks.All.Minor.nVal = 1;
// Set the color of axes
tr1.Root.Axes.All.Ticks.All.Color.nVal = SYSCOLOR_BLACK;
nErr = gl.UpdateThemeIDs( tr1.Root ) ;
if(0 != nErr)
out_str("Fail to Update Theme IDs, theme tree has wrong structure");

double xRange = (vIclean[tr.Fit.optSkipPnts.nVal]-vIclean[vIclean.GetSize()-1]);
gl.XAxis.Scale.From.dVal = vIclean[vVclean.GetSize()-1] - 0.05*xRange; // Assign scale from value
gl.X.To = vIclean[tr.Fit.optSkipPnts.nVal] + 0.05*xRange; // assign scale to value
double yRange = (vVclean[tr.Fit.optSkipPnts.nVal]-vVclean[vVclean.GetSize()-1]);
gl.Y.From = vVclean[vVclean.GetSize()-1] - 0.05*yRange; // Assign scale from value
gl.YAxis.Scale.To.dVal = vVclean[tr.Fit.optSkipPnts.nVal] + 0.05*yRange; // assign scale to value

gl.ApplyFormat( tr1, true, true, true );




Origin Ver. 8 and Service Release SR5 (Select Help-->About Origin):
Operating System: XP

-------------------------------
"Stay hungry, stay foolish."

rlewis

Canada
253 Posts

Posted - 09/10/2009 :  1:38:25 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The following example works for me ..

bool Change_X_Range(double xBegin, double xEnd)
{
	GraphPage gp=Project.Pages(-1);
	if(gp.IsValid()==true)
	{
		GraphLayer gl(gp.Layers(0));
		if(gl.IsValid()==true)
		{
			Tree tr1;
			tr1=gl.GetFormat(FPB_ALL, FPB_ALL, TRUE, TRUE);
			tr1.Root.Axes.X.Scale.From.dVal=xBegin;
			tr1.Root.Axes.X.Scale.To.dVal=xEnd;
			if(gl.UpdateThemeIDs(tr1.Root)==0)
			{
				return gl.ApplyFormat(tr1, true, true,true);
			}
		}
	}
	return (false);
	
}
Go to Top of Page

rikseventyseven

Switzerland
10 Posts

Posted - 09/11/2009 :  05:45:46 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Unfortunately it does not work.
I suspect that there is something with the gp.Create("origin") function. The "origin" layout may impose an automatic rescaling to the data.

Indeed when I take that graph and I embed it into a Report sheet, the axes look scaled as I wanted... Very weird...

Thanks anyhow. R

-------------------------------
"Stay hungry, stay foolish."
Go to Top of Page

rlewis

Canada
253 Posts

Posted - 09/13/2009 :  11:50:57 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ..
The problem is not related to the Graphpage Create function. It actually stems from the fact that the all of the axis properties are not accessable through the Axis tree .. Some are accessible through the Axis tree, others are accessable through the AxisObject properties etc. etc ....

I wrote a Utility class derived from Axis Class and was able do all of the manipulations that you wanted to do. The code and Test Program is listed below. Its a bit roundabout way of doing things but I find it a lot easier to use and approach like this ....
Hopefully, one day, OriginLab will incorporate consolidate access to Axis Object into Axis Member Functions such as these ...
Hope this helps ...


#ifndef _RNAH_GRAPH_AXIS_CLASS_
#define _RNAH_GRAPH_AXIS_CLASS_

#include <rnahl\Library\rnah_constants.h> // Contains the required enumerations ... 

class rnGraphAxis : public Axis
{
public:
	rnGraphAxis() : Axis()
	{
	}
	
	rnGraphAxis(const Axis &axis)  : Axis(axis)
	{
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    

	int GetType()
	{
/*
		rnGraphAxis::GetType
		On Success ... 	Returns a code for the Type of Axis
			0 .... X Axis
			1 .... Y Axis
			2 .... Z Axis

		On Failure ... 		Returns -1;
		
	Function assumes that the following enumeration is declared
	enum
	{
		AXIS_CODE_X=0,
		AXIS_CODE_Y,
		AXIS_CODE_Z,
	};
*/		
		if(IsValid()==true)
		{
			GraphLayer glParent;
			GetParent(glParent);
			if(glParent.IsValid()==true)
			{
				Axis gAxis;
				Axis ThisAxis=(*this);
				gAxis=glParent.XAxis;
				if(gAxis==ThisAxis)
				{
					return (AXIS_CODE_X);
				}
				gAxis=glParent.YAxis;
				if(gAxis==ThisAxis)
				{
					return (AXIS_CODE_Y);
				}
				if(is_3D_graph(glParent)==true)
				{
					gAxis=glParent.ZAxis;
					if(gAxis==ThisAxis)
					{
						return (AXIS_CODE_Z);
					}
				}
			}
		}
		return (-1);
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	bool Display(bool ShowState, bool Primary=true)
	{
/*
	rnGraphAxis::Display

	Shows/Hides the a GraphLayer Axis
	Primary = true(default)		.... Operates on the primary axis	XAxis(Bottom)	YAxis(Left)	ZAxis(Front)
	Primary = false				.... Operates on the secondary axis	XAxis(Top)	YAxis(Right)	ZAxis(Back)
	
*/		
		if(IsValid()==true)
		{
			AxisObject aO;
			if(Primary==true)
			{
				AxisObject aO=AxisObjects(AXISOBJPOS_AXIS_FIRST);
			}
			else
			{
				aO=AxisObjects(AXISOBJPOS_AXIS_SECOND);
			}
			if(aO.IsValid()==true)
			{
				aO.Show=ShowState;
				return (true);
			}
		}
		return (false);
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	bool SetTicks(uint TickCode, int ShowState, bool Primary=true)
	{
/*
	rnGraphAxis::SetTicks		...	
	Sets the Tick Status of the Axis Object as defined by the parameters TickCode and ShowState and Primary
	
	Primary = true 	(default)	.... Operates on the primary axis	XAxis(Bottom)	YAxis(Left)	ZAxis(Front)
	Primary = false 			.... Operates on the secondary axis	XAxis(Top)	YAxis(Right)	ZAxis(Back)
	
	Legal Values for TickCode
	AXIS_TICKS_TYPE_NONE = -1
	AXIS_TICKS_TYPE_BOTH = 0
	AXIS_TICKS_TYPE_MAJOR = 1
	AXIS_TICKS_TYPE_MINOR = 2
	
	Legal Values for ShowState
	AXIS_TICKS_POSITION_NONE =-1
	AXIS_TICKS_POSITION_IN_AND_OUT = 0
	AXIS_TICKS_POSITION_IN	=1
	AXIS_TICKS_POSITION_OUT	=2
	
	
*/
		if(IsValid()==true)
		{
			AxisObject aO;
			if(Primary==true)
			{
				aO=AxisObjects(AXISOBJPOS_AXIS_FIRST);
			}
			else
			{
				aO=AxisObjects(AXISOBJPOS_AXIS_SECOND);
			}
			if(aO.IsValid()==true)
			{
				if(ModifyAxisTicks(aO,TickCode,ShowState)==true)
				return (true);
			}
		}
		return (false);
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	bool SetRange(double axisFrom, double axisTo)
	{
/*
			rnGraphAxis::SetRange              
			Sets the range of an axis object ...
*/		
		if(IsValid()==true && axisFrom!=axisTo)
		{
			Tree tr;
			tr=GetFormat(FPB_ALL, FPB_ALL, TRUE, TRUE);
			tr.Root.Scale.From.dVal=axisFrom;
			tr.Root.Scale.To.dVal=axisTo;
			if(UpdateThemeIDs(tr.Root)==0)
			{
				return (ApplyFormat(tr, true, true,true));
			}
		}
		return (false);
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	bool SetColor(int nColor, bool Primary=true)
	{
/*
			rnGraphAxis::SetColor              
			Sets the Color of an axis object ...
			Primary = true(default)		.... Operates on the primary axis	XAxis(Bottom)	YAxis(Left)	ZAxis(Front)
			Primary = false				.... Operates on the secondary axis	XAxis(Top)	YAxis(Right)	ZAxis(Back)
*/		
		if(IsValid()==true)
		{
			AxisObject aO;
			if(Primary==true)
			{
				aO=AxisObjects(AXISOBJPOS_AXIS_FIRST);
			}
			else
			{
				aO=AxisObjects(AXISOBJPOS_AXIS_SECOND);
			}
			int AxisType=GetType();
			GraphLayer glParent;
			GetParent(glParent);
			if(glParent.IsValid()==true)
			{
				switch (AxisType)
				{
					case AXIS_CODE_X:
						if(Primary==true)
						{
							glParent.XAxis.AxisObjects(AXISOBJPOS_AXIS_FIRST).BottomTicks.Color.nVal = nColor;
						}
						else
						{
							glParent.XAxis.AxisObjects(AXISOBJPOS_AXIS_SECOND).TopTicks.Color.nVal = nColor;
						}
					break;
					case AXIS_CODE_Y:
						if(Primary==true)
						{
							glParent.YAxis.AxisObjects(AXISOBJPOS_AXIS_FIRST).LeftTicks.Color.nVal = nColor;
						}
						else
						{
							glParent.YAxis.AxisObjects(AXISOBJPOS_AXIS_SECOND).RightTicks.Color.nVal = nColor;
						}				
					break;				
					case AXIS_CODE_Z:
						if(Primary==true)
						{
							glParent.ZAxis.AxisObjects(AXISOBJPOS_AXIS_FIRST).FrontTicks.Color.nVal = nColor;
						}
						else
						{
							glParent.ZAxis.AxisObjects(AXISOBJPOS_AXIS_SECOND).BackTicks.Color.nVal = nColor;
						}								
					break;				
				}
				return (true);
			}
		}
		return (false);
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
private:

	bool ModifyAxisTicks(AxisObject &aO, uint TickCode, int ShowState)
	{
/*
	This function assumes that the following enumerations have benn declared
	enum
	{
		AXIS_TICKS_POSITION_NONE=-1,
		AXIS_TICKS_POSITION_IN_AND_OUT,
		AXIS_TICKS_POSITION_IN,
		AXIS_TICKS_POSITION_OUT
	};

	enum
	{
		AXIS_TICKS_TYPE_NONE=-1,
		AXIS_TICKS_TYPE_BOTH,
		AXIS_TICKS_TYPE_MAJOR,
		AXIS_TICKS_TYPE_MINOR
	};
*/		
		if(aO.IsValid()==true)
		{
			Tree tr;
			tr=aO.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);
			if(ShowState>2 || ShowState<0)
			{
				ShowState=-1;
			}
			switch (TickCode)
			{
				case AXIS_TICKS_TYPE_BOTH:
					tr.Root.Major.dVal=ShowState;
					tr.Root.Minor.dVal=ShowState;
				break;
				case AXIS_TICKS_TYPE_MAJOR:
					tr.Root.Major.dVal=ShowState;
				break;
				case AXIS_TICKS_TYPE_MINOR:
					tr.Root.Minor.dVal=ShowState;
				break;
				case AXIS_TICKS_TYPE_NONE:
				default:
					tr.Root.Minor.dVal=-1;
					tr.Root.Major.dVal=-1;
				break;
			}
			aO.ApplyFormat(tr, true, true,true);
			return (true);
		}
		return (false);
	}
	
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#endif // .... _RNAH_GRAPH_AXIS_CLASS_

void MyTestProg()
{
	GraphPage gp; //create new graph page 
	gp.Create("origin");
	if(gp.IsValid()==false)return;
	string dsName="MyGraph";
	gp.SetLongName(dsName + " - VI");
	gp.Rename("VI"+dsName);
	GraphLayer gl(gp.GetName(), 0);
	if(gl.IsValid()==false) return;
	int nIndex = 0;

//  Generate data for the "Experimental" Dataset
	Worksheet wks;
    wks.Create("origin");
    if(wks.IsValid()==false) return;
    Dataset ds1(wks,0);
    Dataset ds2(wks,1);
    ds1.Data(1,20,1);
    ds2.Normal(20);  

//  Generate data for the "Fitted" Dataset
    Worksheet wks1;
    wks1.Create("origin");
    if(wks1.IsValid()==false) return;
    Dataset ds3(wks1,0);
    Dataset ds4(wks1,1);
    ds3.Data(1,20,1);
    ds4.Normal(20);

	int nPlotExp = gl.AddPlot(wks, IDM_PLOT_SCATTER); // Plot the "Experimental" data
	int nPlotFit = gl.AddPlot(wks1, IDM_PLOT_LINE); //   Plot the "Fitted" data

	DataPlot dpE = gl.DataPlots(nPlotExp); // Get first data plot in graph layer
	dpE.SetColor(0, TRUE); // Change color to black
	DataPlot dpF = gl.DataPlots(nPlotFit); // Get second data plot in graph layer
	dpF.SetColor(1,true); // Set color to red

	rnGraphAxis rnX(gl.XAxis);
	if(rnX.IsValid()==false) return;
	rnX.Display(true,false); // Display the Top x-Axis
	rnX.SetTicks(AXIS_TICKS_TYPE_BOTH, AXIS_TICKS_POSITION_IN);  // Set Bottom X-Ticks to  ticks to IN
	rnX.SetTicks(AXIS_TICKS_TYPE_BOTH, AXIS_TICKS_POSITION_IN,false); // Set Top X-Ticks to  ticks to IN
	rnX.SetColor(3,true); // Set Botton X-Azis to Blue
	rnX.SetColor(3,false);// Set Top X-Azis to Blue
	
	rnGraphAxis rnY(gl.YAxis);
	if(rnY.IsValid()==false) return;
	rnY.Display(true,false); // Display the Right y-axis
	rnY.SetTicks(AXIS_TICKS_TYPE_BOTH, AXIS_TICKS_POSITION_IN); // Set Left Y-Ticks to  ticks to IN	
	rnY.SetTicks(AXIS_TICKS_TYPE_BOTH, AXIS_TICKS_POSITION_IN,false); // Set Right Y-Ticks to  ticks to IN
	rnY.SetColor(3,true); // Set Left X-Azis to Blue
	rnY.SetColor(3,false);// Set Right X-Azis to Blue
	
	rnX.SetRange(-5.0, 30.0); // Reset the X-Axis Range
	rnY.SetRange(-5.0,5.0); // Reset the Y-Axis Range
	gp.Refresh(true); // Refresh the Graphpage
}

Go to Top of Page

rikseventyseven

Switzerland
10 Posts

Posted - 09/15/2009 :  09:37:19 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hey thanks a lot for your answer! That's a lot of code!
You saved me from typing a lot of lines and from a lot of headaches...

-R

-------------------------------
"Stay hungry, stay foolish."
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000