188.8.131.52 From Graphical Objects
Graphic Objects (text, lines and shapes) can be tied to events and contain script that runs on those events or by using the run method for those objects. Since graphical objects are attached to a page, they are saved in Templates, Window files and Project files.
Some of your scripts may be used so often that you would like to automate their execution by assigning one or more of them to a button on the Origin project's graphical-user interface (GUI). To do so, follow the steps below:
From a new open Origin project:
- Select the Text tool from the Tools toolbar to the left side of the project window.
- Click on an open space on your graph or worksheet (on a worksheet, click in the gray area to the right of the last column). This will put you into edit mode. Type "Hello" in the text object and click outside the object. You have now created a label for the button.
- Hold down the ALT key while double-clicking on the text object that you just created. The object dialog opens to the Programming tab.
- Note that there is an object Name in the upper left of the dialog. Change this to Greeting.
- In the lower text box, enter this script:
type -b "Hello World";
- From the Script Run After drop-down list, select Button Up, and click OK.
- You have now created a button that, when pressed, executes your script and prints "Hello World" in a pop-up window.
Unlike a text script that exists only in the Script Window, this button and the script that it runs will be saved when you save your Origin project.
The script behind any graphic object can be run using its name and the run method. Open the Script Window (Window: Script Window) and type:
and press Enter.
Here is a script that creates a vertical line on a graph that can be moved to report the interpolated value of your data at the X position represented by the line:
// Create a vertical line on our graph
draw -n MyCursor -l -v $(x1+(x2-x1)/2);
MyCursor.HMOVE = 1; // Allow horizontal movement
MyCursor.color = color(orange); // Change color to orange
MyCursor.linewidth = 3; // Make line thicker
// Add a label to the graph
label -sl -a $(MyCursor.x) $(Y2+0.05*(Y2-Y1)) -n MyLabel $(%C(MyCursor.x));
// Assign a script to the line ..
MyCursor.script$="MyLabel.x = MyCursor.x;
MyLabel.y = Y2 + MyLabel.dy;
// .. and make the script run after the line is moved
MyCursor.Script = 2;
Any Graphical Object (text, lines and shapes) can have an attached script that runs when an event occurs.
In this example, a rectangle (named RECT) on a graph is set to have a script run when the rectangle is either Moved or Sized.
- Use the Rectangle tool on the Tools toolbar to draw a rectangle on a graph.
- Use the Back(data) tool on the Object Edit toolbar to push the rectangle behind the data.
- Hold down the Alt key and double-click on the rectangle to open Programming Control. Note that the objects name is Rect.
- Enter the following script:
%B = %C;
%A = xof(%B);
dsRect = ((%A >= rect.x1) && (%A <= rect.x2) &&
(%B >= rect.y3) && (%B <= rect.y1))?%B:0/0;
type -a Mean of $(stats.mean);
- Choose the Moved or Sized event in the Script, Run After drop down list.
- Click OK.
When you Move or Resize this rectangle, the script calculates the mean of all the points within the rectangle and types the result to the Script Window. You can also define a Graphic Object range and run the script like:
GObject gobj = [Graph1]1!rect;