3.4.7 Legend Update
This page shows how to update graph legend by Origin C code.
Version Info
Minimum Origin Version Required: Origin 8 SR0
Refresh Legend
This example shows how to refresh legend for active graph layer. Please follow the following steps to try.
- Copy the following function to one c file in Code Builder, add to current workspace and compile it.
- New a worksheet and import \Samples\Curve Fitting\Linear Fit.dat.
- Highlight column B and plot as Line. Right click Layer Icon and choose Layer Contents, select linearfit_c to right list and click OK button. You will see Column C has been added to graph but still only B on legend.
- Run the function below legend_update in Command Window, C will be added to legend as a new line.
void legend_update()
{
GraphLayer gl = Project.ActiveLayer();
if(!gl)
{
out_str("Please activate one graph layer");
return;
}
// no need to specified any option arguments since here just refresh legend on data plotting
legend_update(gl);
}
Combine Multiple Legends to One
This example shows how to combine the legend from multiple layers in one page to one legend. Please follow the following steps to try.
- Copy the following function to one c file in Code Builder, add to current workspace and compile it.
- New a worksheet and import \Samples\Curve Fitting\Exponential Decay.dat.
- Highlight all columns and plot by menu Plot -> Multi-Curve -> 4 Panel. There are 3 legends in this graph window, one legend for one data plot.
- Run combile_page_legends in Command Window, you will see legends in each layer have been combined to one legend on the first layer.
void combile_page_legends()
{
GraphLayer gl = Project.ActiveLayer();
if(!gl)
{
out_str("Please activate one graph window");
return;
}
GraphPage gp = gl.GetPage();
int nMode = -1; // ignore if <0 otherwise to set the corresponding graph page's Auto Legend mode
bool bCreate = true;
bool bReconstruct = true;
bool bOneForAll = true; // if true only create one legend for all layers
bool bAscending = true;
foreach (gl in gp.Layers)
{
legend_update(gl, nMode, bCreate, bReconstruct, NULL, bOneForAll, bAscending);
}
bool bCombine = true;
if ( bReconstruct && bCombine)
{
legend_combine(gp, 0, bAscending);
}
}
Change Legend Mode
This example functions show how to change legend mode for one graph page, not for layer. Please copy the functions below to one c file, add file to Code Builder Workspace and compile it.
There is a way to set the legend mode of one graph on GUI:
- Right click graph window gray area and choose Properties to bring up Plot Details dialog.
- Choose Legends/Titles Tab, in Auto Legend group, the option of Translation mode of %(1), %(2) is the legend mode.
Set Page Legend Mode
This example shows how to set legend mode to Data Range for all legends on the page of the active graph layer.
Activate one graph and then run set_page_legend_mode in Command Window, legend should display full data range of each plotting.
void set_page_legend_mode()
{
GraphLayer gl = Project.ActiveLayer();
if(!gl)
{
out_str("Please activate one graph window");
return;
}
legend_update(gl, ALM_RANGE);
}
Set Page Custom Legend Mode
- New a worksheet and import \Samples\Curve Fitting\Enzyme.dat to worksheet.
- Highlight all columns and make a Line plot. At this time show Column Long Name on legend.
- Run set_page_legend_custom_mode in Command Window, will show Column Units label on legend.
void set_page_legend_custom_mode()
{
GraphLayer gl = Project.ActiveLayer();
if(!gl)
{
out_str("Please activate one graph window");
return;
}
int nMode = ALM_CUSTOM;
bool bCreate = true; // true, if no legend will create one
bool bReconstruct = false; // true, if always delete the original legend and create a new one; false, not delete old one just update
string strCustomMode = "@U"; // use Column Units label as legend
if( legend_update(gl, nMode, bCreate, bReconstruct, strCustomMode) )
{
out_str("Set layer mode successfully");
}
else
{
out_str("Fail to set layer mode");
}
}
|