3.3.4 Plot All Worksheets in One Graph


Version Info

Minimum Origin Version Required: Origin 8 SR0

Example

In Origin C, customer can use GraphLayer::AddPlot to plot all Y columns existing on a Worksheet. e.g. If there are four columns ABCD with designation as XYYY on sheet Sheet1 of book Book1, then this method will plot three dataplots on a graphlayer with names like B, C, D.

The following example show how to plot all worksheets in current project on one graph, and group all the dataplots on the graph to apply a color palette to it. To run it, please new an Orgin C file in Code Builder and paste the following code in it, build all files in workspace, type Plot_all_worksheets_in_project_ex in LabTalk Console in Code Builder and press enter, you will see in active folder a newly created graph with grouped plots on it.

void Plot_all_worksheets_in_project_ex()
{
	Folder rootFdr = Project.RootFolder;
	
	//Create Graph for plotting
	GraphPage gp;
	gp.Create("Origin");
	if ( gp && gp.Layers() )
	{
		bool bRecursive = true; //check whether to loop all subfolders
		GraphLayer gl = gp.Layers(0); //get the first layer to plot data on it
		
		//call auxiliary recursively to plot all worksheets in this project.
		Plot_all_worksheets_in_folder(rootFdr, gl, bRecursive); //the function is defined at the end of this example
		
		//group all plots
		gl.GroupPlots(0, -1); //from first to end
		
		//refresh the graphlayer to make it more visible
		gl.Rescale();
		legend_update(gl, ALM_RANGE); //set legend to show page name, layer name, column name and row range
	}
}

void Plot_all_worksheets_in_folder(Folder& fdr, GraphLayer& gl, bool bRecursive = false)
{
	if ( !fdr || !gl )
		return;
	
	//loop all pages and plot data on worksheets
	foreach(PageBase pg in fdr.Pages)
	{
		if ( pg.GetType() == EXIST_WKS ) //check whether is WorksheetPage
		{
			WorksheetPage wksPage(pg);
			foreach(Layer wkslyr in wksPage.Layers)
			{
				Worksheet wks(wkslyr);
				gl.AddPlot(wks, IDM_PLOT_LINE); //plot all curves from wks to gl, as line
			}
		}
	}
	
	//if need to loop all subfolders
	if ( bRecursive )
	{
		foreach(Folder SubFdr in fdr.Subfolders)
		{
			Plot_all_worksheets_in_folder(SubFdr, gl, bRecursive);
		}
	}
}