T O P I C R E V I E W |
Clairekun |
Posted - 08/27/2020 : 2:10:31 PM Origin Ver. and Service Release (Select Help-->About Origin): 2018b Operating System: Windows 10
Hello,
Following https://my.originlab.com/forum/topic.asp?TOPIC_ID=46313 this topic from Origin Forum, where I was advised on how to delete extra lines in a legend-type label adding script to its Programming tab, I decided to write a script to apply to all my graph legends.
Issue no. 1: Loop not looping: stays on the first item
The main legend, common to all graphs, is named Legend. Then, graphs have a text object acting as a secondary legend, which has a specific name depending on the measurement. This script is inside Programming tab in each text label (including the main legend).
layer -c;
string gname$ = page.name$;
string LegText$;
string LegCol$;
string LegSuf$;
int n;
StringArray Legends = {"Legend", "Temp", "DSC", "ZnO"}; //Main legend is Legend; the other three are text objects
loop(ll,1,Legends.GetSize())
{
string Legs$ = Legends.GetAt(ll)$;
if (Legs$ == "Legend")
{
if (Left(gname$,1)$ == "G") {n = 7};
else {n = 6}
loop(ii,1,n)
{
LegText$ += "\l(1,$(ii)) %(1, @WT, A, ii)%(CRLF)";
}
}
else
{
if (Legs$ == "Temp") {LegCol$ = B; LegSuf$ ="°C"};
if (Legs$ == "DSC") {LegCol$ = C; LegSuf$ ="days"};
if (Legs$ =="ZnO") {LegCol$ = D; LegSuf$ ="% ZnO"};
loop(ii,2,count)
{
LegText$ += "\l($(ii)) %(ii, @WT, %(LegCol$), 1)%(LegSuf$)%(CRLF)";
}
}
%(Legs$).text$ = LegText$;
} This only works for the main legend, since it is the first item in the list. If I apply it to any other text object, it won't work. However, if I change the loop starting position, (i.e. loop(ll,2,Legends.GetSize()) for the text label called Temp, which is the 2nd item in the string array), it shows the right text.
Issue no. 2
When/if the script works, I save it to an .ogs file and replace the script from the text objects with a run command.
cd "Z:\Samples\Origin scripts";//Also tried "Z:\Samples\Origin scripts\"
run.file(legends.ogs);//Also tried "run.file(legends);" and simply "legends;" Whenever I apply the script to the text object, the script window -in case it was open- shows a message along the lines of "Current working directory: Z:\Samples\Origin scripts", but nothing else. The script is not applied, either.
Anything I might be missing?
Thank you. |
7 L A T E S T R E P L I E S (Newest First) |
Clairekun |
Posted - 07/08/2021 : 9:43:24 PM Sorry for reviving this post, I couldn't find the time to work on this again until now.
In case you don't want to reread the whole thread, I created a legend and a text object that would act as a secondary legend. The first legend has reference values from a specific workbook; the secondary legend has data values from another workbook. Depending on the graph, the secondary legend labels come from a different column.
For clarity: primary legend on the left, secondary on the right.

I tried to simplify the issue as much as possible. Since the part where problems arose was the secondary legend (which is a normal text object), I deleted whatever was unrelated to it and worked with only one variable. I tried to gradually escalate complexity. The tests I made were:
1. This worked:
layer -c;
string LegText$;
string LegCol$;
string LegSuf$;
LegCol$ = D;
LegSuf$ ="% ZnO";
loop(ii,2,count) //Starting from 2 because 1 belongs to the primary legend
{
LegText$ += "\l($(ii)) %(ii, @WT, %(LegCol$), 1)%(LegSuf$)%(CRLF)";
}
Text.text$ = LegText$;
2. This, too, worked:
layer -c;
string LegText$;
string LegCol$;
string LegSuf$;
Legs$ = "Text"; //Added this
LegCol$ = D;
LegSuf$ ="% ZnO";
loop(ii,2,count)
{
LegText$ += "\l($(ii)) %(ii, @WT, %(LegCol$), 1)%(LegSuf$)%(CRLF)";
}
%(Leg$).text$ = LegText$;//Modified this 3. This didn't work (nothing happens):
layer -c;
string gname$ = page.name$;
string LegText$;
string LegCol$;
string LegSuf$;
string Legs$;
if (Legs$ == "Text") {LegCol$ = D; LegSuf$ ="% ZnO"};
loop(ii,2,count)
{
LegText$ += "\l($(ii)) %(ii, @WT, %(LegCol$), 1)%(LegSuf$)%(CRLF)";
}
%(Legs$).text$ = LegText$; 4. Nor this (moving everything inside if command)
layer -c;
string LegText$;
string LegCol$;
string LegSuf$;
string Legs$;
if (Legs$ == "Text")
{
LegCol$ = D;
LegSuf$ ="% ZnO";
loop(ii,2,count)
{
LegText$ += "\l($(ii)) %(ii, @WT, %(LegCol$), 1)%(LegSuf$)%(CRLF)";
}
%(Legs$).text$ = LegText$;
} Why?
I need the if command because Leg$ will contain 3 variables, with their own values for LegCol$ and LegSuf$.
|
Clairekun |
Posted - 09/10/2020 : 05:28:02 AM Hi Nikolay,
I'm sorry but I don't quite understand your question. The command layer -c adds every plotted line in the layer to a variable named "count" by default, which is later referred to in the loop. Is this what you can't find? If not, please kindly explain.
I believe the variable "count" is not reaching the second loop, but I don't know what to do to solve it. |
nick_n |
Posted - 09/06/2020 : 02:56:11 AM Hi, I didn't find where you get "count" for loop(ii,2,count). BR,
Nikolay |
Clairekun |
Posted - 09/05/2020 : 1:58:59 PM Okay, I followed your advice.
I stripped the .opju file from unnecessary data/graphs and sent it via ftp under the name LegendsIssue.opju.
The code is not applied to the legends, please copy and paste from here. The example file has multiple graphs with two legends each: the main one, called Legend, and a secondary one, called ZnO (4th element in the loop).
Please check that the loop will not work unless loop(ll,1,Legends.GetSize()) is replaced by loop(ll,4,Legends.GetSize()) for the ZnO legend. Please note that you need to select "Run script after: All Events" beforehand.
Could you be so kind to try to find out where my mistake resides?
Thank you. |
lkb0221 |
Posted - 08/28/2020 : 09:31:42 AM Hi,
I tried to set up a fake graph and the script seems working fine. If it is the last loop as you said, then it must be the "count" variable got wrong, which is created from your first cmd and it requires a layer with some data objects in it.
Maybe you can send them an example project so the consultants there can actually see the error and debug for you.
Zheng |
Clairekun |
Posted - 08/27/2020 : 4:07:51 PM quote: Originally posted by cpyang
Can you check by doing this?
dlgfile g:=*.ogs;
run.file(%(fname$))
fname$ is the default output from any file related X-Function, dlgfile in this case.
CP
Hi,
That worked, thanks. In order to not have to choose it every single time, I tried string fname$ = "Z:\Samples\Origin scripts\legends.ogs";
run.file(%(fname$)) It worked, too. Any ideas why run command does nothing, though?
Now, the only thing that is acting weird is the loop.
Thank you. |
cpyang |
Posted - 08/27/2020 : 3:26:49 PM Can you check by doing this?
dlgfile g:=*.ogs;
run.file(%(fname$))
fname$ is the default output from any file related X-Function, dlgfile in this case.
CP
|
|
|