3.5.4 Adding Tags to Imported Data and Displaying on Plots


Version Info

Minimum Origin Version Required: Origin 8 SR0

Examples

In this example, we use LabTalk commands to import a sample data, call an Origin C function to add event tags to the X column in the form of meta data, then after plotting the XY data, we call another Origin C function to draw those event tags as vertical lines with associated labels.

Saving Event Tags to X Column

This OC function save an array of X values and associated labels to the specified column in the User tree so that you can also view them from Column Properties

int putEventsInStorage(string strPageName)
{
	Page pg = Project.Pages(strPageName);
	if( NULL == pg )
	{
		printf("Failed to get page named %s\n", strPageName);
		return -1;
	}

	Worksheet wks = pg.Layers();
	if( NULL == wks )
	{
		printf("Failed to get sheet from page named %s\n", strPageName);
		return -2;
	}

	Column xcol = wks.Columns(0); // assume data X column is first column
	if( NULL == xcol )
	{
		printf("Failed to get column\n");
		return -3;
	}

	Tree trEvents;
	trEvents.Event1.Tag.strVal = "Jump1";
	trEvents.Event1.X.dVal = 72.0;
	trEvents.Event2.Tag.strVal = "Jump2";
	trEvents.Event2.X.dVal = 301.0;
	trEvents.Event3.Tag.strVal = "Jump3";
	trEvents.Event3.X.dVal = 460.0;
	trEvents.Event4.Tag.strVal = "Jump4";
	trEvents.Event4.X.dVal = 760.0;

	if( FALSE == xcol.PutBinaryStorage("TREE", trEvents) ) // TREE is the name of the User Tree
	{
		printf("Failed to put binary storage into the column\n");
		return -4;
	}

	return 0; // Success
}

Adding Event Tags to Graph

This OC function retrive the X column from the first data plot and find the stored event tags and add them as lines and labels

int drawEventTags(string strGraphName, int nPlotIndex = 0)
{
	Page pg = Project.Pages(strGraphName);
	if( NULL == pg )
	{
		printf("Failed to get graph named %s\n", strGraphName);
		return -1;
	}

	GraphLayer gl = pg.Layers(); // active graph layer
	if( NULL == gl )
	{
		printf("Failed to get active layer from graph named %s\n", strGraphName);
		return -2;
	}

	Column xcol;
	DataPlot dp = gl.DataPlots(nPlotIndex);
	if( dp )
	{
		XYRange xy;
		if( FALSE == dp.GetDataRange(xy) || FALSE == xy.GetXColumn(xcol) )
		{
			printf("Failed to get X column from graph named %s\n", strGraphName);
			return -3;
		}
	}

	Tree trEvents;
	if( FALSE == xcol.GetBinaryStorage("TREE", trEvents) )
	{
		printf("Failed to get the binary storage from the X column\n");
		return -4;
	}

	int nEvent = 1;
	string str;
	vector vx(2), vy(2);
	GraphObject go;
	Tree trFormat;
	TreeNode tnEvent;

	while( true )
	{
		//---------- Get Event Node

		str.Format("Event%d", nEvent);
		tnEvent = trEvents.GetNode(str);
		if( NULL == tnEvent )
			break; // no more events, exit loop

		//---------- Add Vertical Line

		vx[0] = vx[1] = tnEvent.X.dVal;
		vy[0] = vy[1] = 0;
	
		go = gl.CreateGraphObject(GROT_LINE);
		go.SetName(str + "Line");
		go.Attach = 2; // 2 = attach to axes
	
		trFormat.Reset();// make sure tree is empty
		trFormat.Root.Direction.nVal = LN_VERTICAL;
		trFormat.Root.Span.nVal = 1; // turn span on
		trFormat.Root.Data.X.dVals = vx;
		trFormat.Root.Data.Y.dVals = vy;
		trFormat.Root.Color.nVal = SYSCOLOR_LTGRAY;
		trFormat.Root.BehindData.nVal = 1; // put line behind plot data
		trFormat.Root.States.nVal = GOC_NO_SELECT | GOC_NO_MOVE | GOC_NO_ROTATE | GOC_NO_SKEW | GOC_NO_EDIT | GOC_NO_BORDERSIZE | GOC_NO_IN_PLACE_EDIT;
		if( 0 == go.UpdateThemeIDs(trFormat.Root) )
			go.ApplyFormat(trFormat, true, true);

		//---------- Add Text Label

		go = gl.CreateGraphObject(GROT_TEXT);
		go.SetName(str + "Text");
		go.Attach = 2; // 2 = attach to axes
		go.Text = tnEvent.Tag.strVal;
		go.X = (go.DX / 2) + tnEvent.X.dVal;
		go.Y = (go.DY / 2) + gl.Y.To;

		trFormat.Reset();// make sure tree is emtpy
		trFormat.Root.Angle.dVal = 45;
		trFormat.Root.States.nVal = GOC_NO_SELECT | GOC_NO_MOVE | GOC_NO_ROTATE | GOC_NO_SKEW | GOC_NO_EDIT | GOC_NO_BORDERSIZE | GOC_NO_IN_PLACE_EDIT;
		if( 0 == go.UpdateThemeIDs(trFormat.Root) )
			go.ApplyFormat(trFormat, true, true);

		//---------- Next Event

		nEvent++;
	}

	return 0; // Success
}

LabTalk Script Using Above Origin C Functions

// Import a data file from Samples folder.
%Z = System.Path.Program$;
impASC fname:="%Zsamples\signal processing\step signal with random noise.dat";

// Put event data into binary storage.
putEventsInStorage(%H);

// Plot the imported data.
plotxy 2 plot:=200;

// Update plot with meta data info.
drawEventTags(%H);