1.7.3 Customizing Data Plots


Adding Data Marker

Origin C supports the following methods for customizing data markers.

The following code shows how to add two data markers to the active graph window.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots();

// the indices of the data markers
vector<int> vnBegin = {0, 9};
vector<int> vnEnd = {4, 14};

// to add two data markers
int nRet = dp.AddDataMarkers(vnBegin, vnEnd);
if( 0 == nRet )
{
	out_str("Add data marker successfully.");
}

The code below shows how to change the position of the present data marker.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots();

// the indices of the data markers
vector<int> vnBegin = {11, 2};
vector<int> vnEnd = {19, 5};
vector<int> vnIndices = {1, 0};

// to add two data markers
int nRet = dp.SetDataMarkers(vnBegin, vnEnd, vnIndices);
if( 0 == nRet )
{
	out_str("Set data marker successfully.");
	gl.GetPage().Refresh();
}

Setting Color

The following code shows how to set the color of the data plot.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(0);

bool bRepaint = true;
dp.SetColor(SYSCOLOR_GREEN, bRepaint);

Getting Format Tree

OriginObject::GetFormat and OriginObject::ApplyFormat are used to get and set Origin object formats. The following getting, setting and copying format mechanisms can be used for all Origin objects whose classes derive from the OriginObject base class (see Reference: Class Hierarchy). For example, the Origin objects can be objects of the DataPlot class, Worksheet class, WorksheetPage class, MatrixLayer class, MatrixPage class, GraphLayer class, or GraphPage class.

The DataPlot class derives from the DataObjectBase class, and the DataObjectBase class derives from the OriginObject class, so we can call DataPlot::GetFormat to get the format tree structure.

There are two ways to see the format tree structure via the following code.

  • Set a break point on the GetFormat line in the following code, activate one data plot, run the code, press F10 (Step Over) to execute the GetFormat line, and see the details of the format tree in the Code Builder Local Variables Window tr variable. (press Alt+4 to open/hide the Local Variables window).
  • Use the last line, out_tree(tr);, to print out the format tree.
GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(-1); // Get the active data plot
 
// Different plot types(for example, Line, Box Chart...) have 
// different structure in the format tree.
Tree tr;
 
// Get the format tree to see details of the tree structure.
tr = dp.GetFormat(FPB_ALL, FOB_ALL, true, true);

out_tree(tr); // print out the format tree.

Setting Format on Line Plot

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(-1); // Get the active data plot
  
// Set format on a line plot
// Note: See the previous section to get the structure of format tree
Tree tr;
tr.Root.Line.Connect.nVal = 2; // 2 for 2 point segment
tr.Root.Line.Color.nVal = RGB2OCOLOR(RGB(100, 100, 220));
tr.Root.Line.Width.dVal = 1.5;
 
if( 0 == dp.UpdateThemeIDs(tr.Root) )
{
    bool bRet = dp.ApplyFormat(tr, true, true); 
}

Copying Format from One Data Plot to Another

Copying Format via Theme File

Getting and saving a format tree from a data plot into a theme file, then loading the theme file to a tree and applying the format tree to another data plot.

// Save plot settings from Graph1 to a theme file
GraphPage gpSource("Graph1");
GraphLayer glSource = gpSource.Layers(0);
DataPlot dpSource = glSource.DataPlots(0);

Tree tr;
tr = dpSource.GetFormat(FPB_ALL, FOB_ALL, true, true);
string strTheme = GetAppPath(false) + "plotsettings.XML";
tr.Save(strTheme);

// Load plot settings from a theme file to a tree, and apply format from 
// tree to data plot object.
GraphPage gpDest("Graph2");
GraphLayer glDest = gpDest.Layers(0);	
DataPlot dpDest = glDest.DataPlots(0);

Tree tr2;
tr2.Load(strTheme);
dpDest.ApplyFormat(tr2, true, true);

Copying Format via Tree

Getting plot settings from one data plot to a tree, then apply settings from this tree to another data plot object.

GraphPage gpSource("Graph1");
GraphLayer glSource = gpSource.Layers(0);
DataPlot dpSource = glSource.DataPlots(0);

GraphPage gpDest("Graph2");
GraphLayer glDest = gpDest.Layers(0);	
DataPlot dpDest = glDest.DataPlots(0);

// Get format from source data plot
Tree tr;
tr = dpSource.GetFormat(FPB_ALL, FOB_ALL, true, true);

// Apply format to another data plot
dpDest.ApplyFormat(tr, true, true);

Setting Format on Scatter Plot

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(-1); // Get the active data plot  

// Set symbol format
Tree tr;
tr.Root.Symbol.Size.nVal = 12; // Size of symbol
tr.Root.Symbol.Shape.nVal = 1; // Circle
tr.Root.Symbol.Interior.nVal = 1; // Interior type
tr.Root.Symbol.EdgeColor.nVal = SYSCOLOR_RED;
tr.Root.Symbol.FillColor.nVal = SYSCOLOR_BLUE;

// Show vertical droplines
tr.Root.DropLines.Vertical.nVal = 1;
tr.Root.DropLines.VerticalColor.nVal = SYSCOLOR_LTGRAY;
tr.Root.DropLines.VerticalStyle.nVal = 1;
tr.Root.DropLines.VerticalWidth.nVal = 1.5;

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

Setting Format on Grouped Line + Symbol Plots

Use Origin C to set the format for grouped plots. The same action can be completed by going into the Plot Details dialog, under the Group tab. The formats included Line Color, Symbol Type, Symbol Interior, and Line Style.

The following example shows how to set format on Line and Symbol plots. This group is assumed to contain 4 data plots.

GraphLayer gl = Project.ActiveLayer(); 
GroupPlot gplot = gl.Groups(0); // Get the first group in layer

// the Nester is an array of types of objects to do nested cycling in the group
// four types of setting to do nested cycling in the group
vector<int> vNester(3); 
vNester[0] = 0;  // cycling line color in the group
vNester[1] = 3;  // cycling symbol type in the group
vNester[2] = 8;  // cycling symbol interior in the group 
gplot.Increment.Nester.nVals = vNester;  // set Nester of the grouped plot 

// Put format settings to vector for 4 plots
vector<int> vLineColor = {SYSCOLOR_BLUE, SYSCOLOR_OLIVE, SYSCOLOR_RED, 
		SYSCOLOR_CYAN};
vector<int> vSymbolShape = {1, 3, 5, 8};
vector<int> vSymbolInterior = {1, 2, 5, 0};

Tree tr;
tr.Root.Increment.LineColor.nVals = vLineColor;  // set line color to theme tree
tr.Root.Increment.Shape.nVals = vSymbolShape;  // set symbol shape to theme tree
// set symbol interior to theme tree
tr.Root.Increment.SymbolInterior.nVals = vSymbolInterior;  

if(0 == gplot.UpdateThemeIDs(tr.Root) )    
{
	bool bb = gplot.ApplyFormat(tr, true, true);    // apply theme tree
}

Setting Colormap Settings

DataPlot class has two overloaded methods to set colormap.

  • DataPlot::SetColormap( const vector<double> & vz, BOOL bLogScale = FALSE ) is just used to set Z level and scale type (log type or not). The values in vz argument are Z values.
  • DataPlot::SetColormap( TreeNode& trColormap ) is used to set all colormap settings, for example, Z values, colors, line format and text label format.

This example shows how to set up colormap Z levels on a Contour graph.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(0);

// Get original colormap Z levels
vector vZs;
BOOL bLogScale = FALSE;
BOOL bRet = dp.GetColormap(vZs, bLogScale);
int nLevels = vZs.GetSize();    

// Decrease Z levels vector and set back to DataPlot
double min, max;
vZs.GetMinMax(min, max);
double dChangeVal = fabs(max - min) * 0.2;
bool bIncrease = true;
if( !bIncrease )
	dChangeVal = 0 - dChangeVal;

min = min - dChangeVal;
max = max - dChangeVal;
double inc = (max - min) / nLevels;    
vZs.Data(min, max, inc);

dp.SetColormap(vZs);

The following example shows how to set up colormap Z levels with log10 scale type.

bool plot_matrix(LPCSTR lpsczMatPage, LPCSTR lpcszGraphTemplate = "contour"
		, int nPlotID = IDM_PLOT_CONTOUR)
{
	// Get the active matrix object from the specific matrix page
	MatrixPage matPage = Project.MatrixPages(lpsczMatPage);
	if( !matPage )
	{
		out_str("Invalid matrix page");
		return false;
	}
	// get the active sheet in this matrix page
	MatrixLayer ml = matPage.Layers(-1); 
	// get the active matrix object in matrixsheet
	MatrixObject mobj = ml.MatrixObjects(-1); 	
	
	// Create hidden graph page with template and add plot
	// Create as hidden to avoid unneeded drawing            
	GraphPage gp;
	gp.Create(lpcszGraphTemplate, CREATE_HIDDEN);
	GraphLayer glay = gp.Layers();
	
	int nPlot = glay.AddPlot(mobj, nPlotID);
	if(nPlot < 0)
	{
		out_str("fail to add data plot to graph");
	    return false; 
	}
	glay.Rescale(); // rescale x y axes
	     
	
	// Construct Z levels vector
	int nNewLevels = 4;    
	double min = 0.1, max = 100000.;
	double step = (log10(max) - log10(min)) / (nNewLevels - 1);
	
	vector vLevels;
	vLevels.SetSize(nNewLevels);
	vLevels.Data(log10(min), log10(max), step);   
	vLevels = 10^vLevels;	
	
	// Setup z levels in percent, not real z values.
	// First value must be 0 and last value must be < 100
	vLevels = 100*(vLevels - min)/(max - min); 
	
	Tree tr;
	tr.ColorMap.Details.Levels.dVals = vLevels;
	tr.ColorMap.ScaleType.nVal = 1; // 1 for log10
	tr.ColorMap.Min.dVal = min;
	tr.ColorMap.Max.dVal = max;
	
	DataPlot dp = glay.DataPlots(nPlot);               
	bool bRet = dp.SetColormap(tr); 
	if( !bRet )
	{
		out_str("fail to set colormap");
		return false;
	}
	
	gp.Label = "Plot created using template: " + (string)lpcszGraphTemplate;
	gp.TitleShow = WIN_TITLE_SHOW_BOTH;
	gp.SetShow(); // show it when all it ready
	
	return true;
}

Call the above plot_matrix function with coutour template and IDM_PLOT_CONTOUR plot id to plot contour graph and then set colormap on it.

void plot_contour_ex(LPCSTR lpcszMatPage)
{ 
    plot_matrix(lpcszMatPage, "contour", IDM_PLOT_CONTOUR);
}

Call the above plot_matrix function with image template and IDM_PLOT_MATRIX_IMAGE plot id to plot image graph and then set colormap on it.

void plot_image_ex(LPCSTR lpcszMatPage)
{ 
    plot_matrix(lpcszMatPage, "image", IDM_PLOT_MATRIX_IMAGE);
}

The following example shows how to remove fill color, and set up line color, style, width and text labels on a Contour graph.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(0);
 
Tree tr;
dp.GetColormap(tr);
 
// Remove fill color
tr.ColorFillControl.nVal = 0;
 
// Set line color
vector<int> vnLineColors;
vnLineColors = tr.Details.LineColors.nVals;
int nLevels = vnLineColors.GetSize();
vnLineColors.Data(1, nLevels, 1);
tr.Details.LineColors.nVals = vnLineColors;
 
// Set line style as Dash for all lines
vector<int> vnLineStyles(vnLineColors.GetSize());
vnLineStyles = 1;
tr.Details.LineStyles.nVals = vnLineStyles;
 
// Set line width for all lines
vector vdLineWidths(vnLineColors.GetSize());
vdLineWidths = 3;
tr.Details.LineWidths.dVals = vdLineWidths;    
 
// Show/hide labels, show all except that the first two.
vector<int> vnLabels(vnLineColors.GetSize());
vnLabels = 1;    
vnLabels[0] = 0;
vnLabels[1] = 0;
tr.Details.Labels.nVals = vnLabels;    
 
// Set back settings to graph
dp.SetColormap(tr);

This example shows how to set the format(i.e. color, size, bold, italic) of the text labels on a Contour graph.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(0);

// Get all properties of the related objects of the colormap data plot
Tree tr;
tr = dp.GetFormat(FPB_ALL, FOB_ALL, true, true);

// Show all labels
vector<int> vnLabels;
vnLabels = tr.Root.ColorMap.Details.Labels.nVals;
vnLabels = 1;// 0 to hide, 1 to show
tr.Root.ColorMap.Details.Labels.nVals = vnLabels;

// Set the numeric format for labels
tr.Root.NumericFormats.Format.nVal = 0; // Decimal
tr.Root.NumericFormats.DigitsControl.nVal = 0;
tr.Root.NumericFormats.SignificantDigits.nVal = 5;//DecimalPlaces
tr.Root.NumericFormats.Prefix.strVal = "_";
tr.Root.NumericFormats.Suffix.strVal = "Label";
tr.Root.NumericFormats.MinArea.nVal = 5; // Labeling Criteria - Min Area(%)

// Set text format for labels
tr.Root.Labels.Color.nVal = SYSCOLOR_BLUE;
//FontFaceIndex_to_DWORD is used to convert font from GUI index to DWORD real value 
tr.Root.Labels.Face.nVal = FontFaceIndex_to_DWORD(2);// choose the 3rd font in GUI
tr.Root.Labels.Size.nVal = 20;
tr.Root.Labels.WhiteOut.nVal = 1;
tr.Root.Labels.Bold.nVal = 1;
tr.Root.Labels.Italic.nVal = 1;
tr.Root.Labels.Underline.nVal = 1;

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