>
|
|
>
|
|
>
|
|
Executing the Plot command produces a function call.
| (1) |
By using commands from the Layout Constructors package a nested function call can be produced which represents a worksheet.
>
|
|
That XML representation of a worksheet can be inserted directly.
>
|
|
>
|
|
>
|
|
The previous example's call to the InsertContent command inserted a component with identity "Plot0", which still exists in this worksheet. Inserting additional content whose input contains another component with that same identity "Plot0" incurs a substitution of the input identity in order to avoid a conflict with the identity of the existing component.
The return value of the following call to InsertContent is a table which can be used to reference the substituted identity of the inserted component.
>
|
|
| (2) |
>
|
|
The next example makes use of action code for a Plot and a Button component.
>
|
|
>
|
|
The action code passed with the clickaction option of the Plot component constructor calls a procedure update, and the action code of the Button component calls a procedure clear. These procedures are defined below. The update procedure uses the clickx and clicky values taken on when the mouse pointer is left clicked in the plot.
>
|
update := proc(comp)
global L;
local cx, cy, v;
uses DocumentTools, plots, CurveFitting;
(cx,cy) := Do(comp(clickx)), Do(comp(clicky));
if type(L,listlist) then
L := [L[], [cx, cy]];
else
L := [[cx, cy]];
end if;
Do(comp = display(plot(L, style=point, symbol=solidcircle,
symbolsize=15, color=blue, axes=normal),
plot(LeastSquares(L, v), v=0..10),
view=[0..10, 0..10]));
end proc:
|
>
|
clear := proc(comp)
DocumentTools:-Do(comp = plot([[0,0]], color=white,
axes=normal, view=[0..10,0..10]));
end proc:
|
Now we insert the components. Following insertion every single left click in the plot augments L with a new data point, and the plot is updated with a point plot and a least squares linear fit of the points in L.
>
|
|
The Plot component in the previous example is constructed by supplying identity="Plot0" as an option. But the first example on this page had already inserted content with a component with that identity. Hence the identity of the Plot component in the last example is replaced at insertion time. Note that literal instances of "Plot0" and Plot0 are also replaced in all component action code of the inserted content. The consistent replacement allows the action code to function as intended. Such replacement can be confirmed selecting the inserted Plot and Button components and viewing the component names in the Context Panel and then clicking the Edit Code buttons to view the Code Editor for these components.
The fillcolor can be specified to match that of a parent Table Cell.
>
|
|
>
|
|
The following example programmatically inserts a PlotComponent with a scaled 3D animation which can be played using SetProperty.
>
|
|
>
|
|
>
|
|
>
|
|