3.10.1 Output to Report Table


Version Info

Minimum Origin Version Required: Origin 8 SR5

Example

This example will show you how to perform statistics analysis on data, and then put the results to the report table. The example will use the curve_stats function. Also, please refer to Creating Reports for more information about how to creating reports.

Before running the Origin C code, please run the following LabTalk script first to generate the required data.

newbook;  // create a name workbook
fname$ = system.path.program$ + "Samples\Statistics\Descriptive Statistics on columns.txt";
impASC;  // import the file to the workbook
void output_report_table()
{
	Worksheet wks = Project.ActiveLayer();  // get the active worksheet
	if(!wks)
		return;
	
	Column colX = wks.Columns(0);  // get column A as X
	Column colY = wks.Columns(3);  // get column D as Y
	
	vector vX = colX.GetDataObject();  // X vector
	vector vY = colY.GetDataObject();  // Y vector
	
	Tree trResult;
	if(!curve_stats(vX, vY, trResult))  // perform statistics on column D, results to trResult
		return;
	
	int nID = 100; // node ID
	int nTableFormat = GETNBRANCH_HIDE_COL_HEADINGS | GETNBRANCH_HIDE_ROW_HEADINGS
						| GETNBRANCH_FIT_COL_WIDTH | GETNBRANCH_FIT_ROW_HEIGHT;  // table format
	
	// report tree 1
	Tree trReport1;
	trReport1.Report.ID = nID++;  // report id
	trReport1.Report.SetAttribute(STR_LABEL_ATTRIB, "Descriptive Stats Report");  // report title
	trReport1.Report.SetAttribute(TREE_Table, nTableFormat | GETNBRANCH_OPEN);  // open this node
	
	trReport1.Report.Table1.ID = nID++;  // table 1: show input data
	trReport1.Report.Table1.SetAttribute(STR_LABEL_ATTRIB, "Input Data");  // table title
	trReport1.Report.Table1.SetAttribute(TREE_Table, nTableFormat | GETNBRANCH_TRANSPOSE);  // transpose this table
	
	trReport1.Report.Table1.C1.ID = nID++;  // column 1
	trReport1.Report.Table1.C1.SetAttribute(STR_LABEL_ATTRIB, "X(" + colX.GetName() + ")");  // column label
	trReport1.Report.Table1.C1.dVals = vX;  // values
	
	trReport1.Report.Table1.C2.ID = nID++;  // column 2
	trReport1.Report.Table1.C2.SetAttribute(STR_LABEL_ATTRIB, "Y(" + colY.GetName() + ")");
	trReport1.Report.Table1.C2.dVals = vY;  // values
	
	trReport1.Report.Table2.ID = nID++;  // table 2: show output results
	trReport1.Report.Table2.SetAttribute(STR_LABEL_ATTRIB, "Stats Results");  // table title
	trReport1.Report.Table2.SetAttribute(TREE_Table, nTableFormat | GETNBRANCH_OPEN);  // open this node
	
	trReport1.Report.Table2.R1.ID = nID++;  // row 1
	trReport1.Report.Table2.R1.SetAttribute(STR_LABEL_ATTRIB, "Results");  // row label

	int nColIDBase = 10000;  // base column id
	trReport1.Report.Table2.R1.C1.ID = nColIDBase + 1;  // row 1 column 1
	trReport1.Report.Table2.R1.C1.SetAttribute(STR_LABEL_ATTRIB, "N Total"); // column label
	trReport1.Report.Table2.R1.C1.dVal = trResult.N.dVal;  // set value
	
	trReport1.Report.Table2.R1.C2.ID = nColIDBase + 2;  // row 1 column 2
	trReport1.Report.Table2.R1.C2.SetAttribute(STR_LABEL_ATTRIB, "Sum"); // column label
	trReport1.Report.Table2.R1.C2.dVal = trResult.sum.dVal;  // set value
	
	trReport1.Report.Table2.R1.C3.ID = nColIDBase + 3;  // row 1 column 3
	trReport1.Report.Table2.R1.C3.SetAttribute(STR_LABEL_ATTRIB, "Minimum"); // column label
	trReport1.Report.Table2.R1.C3.dVal = trResult.min.dVal;  // set value
	
	trReport1.Report.Table2.R1.C4.ID = nColIDBase + 4;  // row 1 column 4
	trReport1.Report.Table2.R1.C4.SetAttribute(STR_LABEL_ATTRIB, "Maximum"); // column label
	trReport1.Report.Table2.R1.C4.dVal = trResult.max.dVal;  // set value
	
	trReport1.Report.Table2.R1.C5.ID = nColIDBase + 5;  // row 1 column 5
	trReport1.Report.Table2.R1.C5.SetAttribute(STR_LABEL_ATTRIB, "Mean"); // column label
	trReport1.Report.Table2.R1.C5.dVal = trResult.mean.dVal;  // set value
	
	trReport1.Report.Table2.R1.C6.ID = nColIDBase + 6;  // row 1 column 6
	trReport1.Report.Table2.R1.C6.SetAttribute(STR_LABEL_ATTRIB, "Median"); // column label
	trReport1.Report.Table2.R1.C6.dVal = trResult.median.dVal;  // set value
	
	trReport1.Report.Table2.R1.C7.ID = nColIDBase + 7;  // row 1 column 7
	trReport1.Report.Table2.R1.C7.SetAttribute(STR_LABEL_ATTRIB, "Standard Deviation"); // column label
	trReport1.Report.Table2.R1.C7.dVal = trResult.sd.dVal;  // set value

	GraphPage gp;
	gp.Create();  // create a graph page
	if(!gp)
		return;
	
	GraphLayer gl = gp.Layers(0);  // get the graph layer in the graph page
	if(!gl)
		return;
	
	DataRange dr;  // data range with column A and column D
	dr.Add("X", wks, 0, 0, -1, 0);
	dr.Add("Y", wks, 0, 3, -1, 3);
	int nn = gl.AddPlot(dr, IDM_PLOT_LINE);  // add plot to graph layer
	if(nn == -1)
		return;
	gl.Rescale();  // rescale graph layer
	
	trReport1.Report.Table3.ID = nID++;  // table 3: show graph
	trReport1.Report.Table3.SetAttribute(STR_LABEL_ATTRIB, "Graphs");  // table title
	
	// open this node and transpose this table
	trReport1.Report.Table3.SetAttribute(TREE_Table, nTableFormat | GETNBRANCH_OPEN | GETNBRANCH_TRANSPOSE);  
	
	trReport1.Report.Table3.C1.ID = nID++;  // column 1
	trReport1.Report.Table3.C1.strVal = gp.GetUID(true);  // set value with the unique ID of the graph page
	trReport1.Report.Table3.C1.SetAttribute(STR_LABEL_ATTRIB, gp.GetName());  // column label
	trReport1.Report.Table3.C1.SetAttribute(TREE_Control, ONODETYPE_EMBED_GRAPH);  // embedded graph
	
	WorksheetPage wp = wks.GetPage();  // get the worksheet page of the active worksheet
	if(!wp)
		return;
	
	DWORD 	dwOptions = WP_SHEET_HIERARCHY | CREATE_NO_DEFAULT_TEMPLATE;
	int n1 = wp.AddLayer("Hierarchical Sheet Report", dwOptions);  // add a new worksheet
	if(n1<0)
		return;
	
	Worksheet wksReport = wp.Layers(n1);  // get the added new worksheet
	
	if(wksReport.SetReportTree(trReport1.Report)<0)  // set report tree to the worksheet
	{
		out_str("Failed to set report tree.\n");
		return;
	}
	wksReport.AutoSize();
	
	// report tree 2
	Tree trReport2;
	trReport2.Report.ID = nID++;  // report id
	trReport2.Report.SetAttribute(STR_LABEL_ATTRIB, "Descriptive Stats Report");  // report title
	trReport2.Report.SetAttribute(TREE_Table, GETNBRANCH_FIT_COL_WIDTH | GETNBRANCH_FIT_ROW_HEIGHT);  // fit column width and row height
	
	trReport2.Report.Table.ID = nID++;  // table
	trReport2.Report.Table.SetAttribute(STR_LABEL_ATTRIB, "Stats Results");  // table title
	trReport2.Report.Table.SetAttribute(TREE_Table, GETNBRANCH_FIT_COL_WIDTH | GETNBRANCH_FIT_ROW_HEIGHT | GETNBRANCH_TRANSPOSE);
	
	trReport2.Report.Table.C1.ID = nID++;  // column 1
	trReport2.Report.Table.C1.SetAttribute(STR_LABEL_ATTRIB, "Input X");
	trReport2.Report.Table.C1.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_X);  // set designation
	
	trReport2.Report.Table.C2.ID = nID++;  // column 2
	trReport2.Report.Table.C2.SetAttribute(STR_LABEL_ATTRIB, "Input Y");
	trReport2.Report.Table.C2.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C3.ID = nID++;  // column 3
	trReport2.Report.Table.C3.SetAttribute(STR_LABEL_ATTRIB, "N Total");
	trReport2.Report.Table.C3.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C4.ID = nID++;  // column 4
	trReport2.Report.Table.C4.SetAttribute(STR_LABEL_ATTRIB, "Sum");
	trReport2.Report.Table.C4.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C5.ID = nID++;  // column 5
	trReport2.Report.Table.C5.SetAttribute(STR_LABEL_ATTRIB, "Minimum");
	trReport2.Report.Table.C5.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C6.ID = nID++;  // column 6
	trReport2.Report.Table.C6.SetAttribute(STR_LABEL_ATTRIB, "Maximum");
	trReport2.Report.Table.C6.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C7.ID = nID++;  // column 7
	trReport2.Report.Table.C7.SetAttribute(STR_LABEL_ATTRIB, "Mean");
	trReport2.Report.Table.C7.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C8.ID = nID++;  // column 8
	trReport2.Report.Table.C8.SetAttribute(STR_LABEL_ATTRIB, "Median");
	trReport2.Report.Table.C8.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	trReport2.Report.Table.C9.ID = nID++;  // column 9
	trReport2.Report.Table.C9.SetAttribute(STR_LABEL_ATTRIB, "Standard Deviation");
	trReport2.Report.Table.C9.SetAttribute(STR_COL_DESIGNATION_ATTRIB, OKDATAOBJ_DESIGNATION_Y);
	
	// set values to those columns
	trReport2.Report.Table.C1.dVals = vX;
	trReport2.Report.Table.C2.dVals = vY;
	trReport2.Report.Table.C3.dVal = trResult.N.dVal;
	trReport2.Report.Table.C4.dVal = trResult.sum.dVal;
	trReport2.Report.Table.C5.dVal = trResult.min.dVal;
	trReport2.Report.Table.C6.dVal = trResult.max.dVal;
	trReport2.Report.Table.C7.dVal = trResult.mean.dVal;
	trReport2.Report.Table.C8.dVal = trResult.median.dVal;
	trReport2.Report.Table.C9.dVal = trResult.sd.dVal;
	
	int n2 = wp.AddLayer("Flat Sheet Report");  // add a new worksheet
	if(n2<0)
		return;
	
	Worksheet wksReport2 = wp.Layers(n2);  // get the added new worksheet
	
	if(wksReport2.SetReportTree(trReport2.Report)<0)  // set report tree to worksheet
	{
		out_str("Failed to set report tree.\n");
		return;
	}
	wksReport2.AutoSize();
}