Step 6 - Switches
Now that we've added
support for reading data, we want to create a different way of presenting
our data and to allow the user to switch between them.
In this step we will
- Create a View menu
with a Switch object for each plot type
- Write a callback
for the switches to create the appropriate plot
- Create a new plot
type and add it as a function.
So first we need to
create the switches:
create Menu gsharp_1.Menubar.View;
create Switch gsharp_1.Menubar.View.Method1
( XuNguiLabel = "Basic Graph",
XuNguiSwitchType = "radio",
XuNguiCallback = "ChangeGraphTypeCB",
XuNguiCallbackData = "'CreatePlot()'"
);
create Switch gsharp_1.Menubar.View.Method2
( XuNguiLabel = "Scatter Plot",
XuNguiSwitchType = "radio",
XuNguiCallback = "ChangeGraphTypeCB",
XuNguiCallbackData = "'CreateScatter()'"
);
GuiSwitchSetState("gsharp_1.Menubar.View.Method1", true, true);
|
The Switch object
is very similar to the Button object except it has a small toggle
next to it show if it is selected or not.
There are two
types of behaviour for Switches - radio or check. Only one radio
button can be selected at a time and selecting a new button, deselects
the current button. Check buttons are independent of each other
and can be selected and unselected freely. The resource XuNguiSwitchType
can be used to specify the appearance of the button, but Gsharp
leaves it to us to control the behaviour. We'll do this in our callback
function ChangeGraphTypeCB.
Notice that
we've used the callbackData resource to specify the name of the
function to create the required plot - either CreatePlot() or CreateScatter();
function ChangeGraphTypeCB(string o, string d, float r)
GuiRadioButtonCB(o, d, r);
if not exists(WORK.Lines) return;
reset;
eval(d);
endfunction
function GuiRadioButtonCB(string o, string d, float r)
string child, parent;
child = strrchr(o,".");
parent = substr(o,1,strlen(o)-strlen(child));
for child in parent+"."+childrenof($parent)
if (typeof($child)<>"Switch") continue;
if ($child.XuNguiSwitchType<>"radio") continue;
GuiSwitchSetState(child, o=child, false);
endfor
endfunction
|
When ChangeGraphTypeCB
is called we:
- Call GuiRadioButtonCB
to perform the radio behaviour - this is a standard function you can
use with any radio button.
- If we haven't read
any data yet - we return
- Otherwise we reset
the graphics and call the function passed in the callbackData.
Our second plot will
be a scatter plot of the line length against the number of spaces and
we'll label each point with the line number. We need to make an array
of labels as part of FileOpenCB:
LineNum = tostring(1:size(WORK.Lines));
Finally make your
scatter plot in Gsharp, generate the GSL and paste it into a function
called CreateScatter(). Of course we've done it for you if you prefer
...
function CreateScatter()
create Viewport page_1.viewport
( XuNfirstDiagonalPoint = (10,10) %,
XuNsecondDiagonalPoint = (88,82) %,
XuNframe = "inFront",
XuNframeWidth = 0.1 mm
);
create Title page_1.viewport.title
( XuNtitleText = "Scatter Plot"
);
create Domain page_1.viewport.domain
( XuN1st2DYAxisTicklines = "behind",
XuN1st2DYAxisTickmarks = false,
XuN1st2DXAxisTicklines = "behind",
XuN1st2DXAxisTickmarks = false
);
set page_1.viewport.domain.yaxis1
( XuNticklinesMajorColor = 31,
XuNticklinesMinor = true,
XuNticklinesMinorColor = 31,
XuNaxisTextText = "Number of Spaces",
XuNticklinesMinorStyle = "lineStyle1"
);
set page_1.viewport.domain.xaxis1
( XuNticklinesMajorColor = 31,
XuNticklinesMinor = true,
XuNticklinesMinorColor = 31,
XuNaxisTextText = "Line Lengths",
XuNticklinesMinorStyle = "lineStyle1"
);
create Graph page_1.viewport.domain.scatter
( XuNgraphType = "scatter",
XuNlabelLayoutLabels = "onWithoutBox",
XuNlabelLayoutTextLabelData = "LineNum",
XuNlabelStyleHeight = 4 %,
XuNmarkerHeight = 1e-006 %,
XuNmarkers = true,
XuNxData = "LineLengths",
XuNyData = "NumSpaces"
);
endfunction
|
Try
running your application again and try switching between your plot
types
Now continue to Step
7 - Dialogs
|