Plot/Class

From J Wiki
Jump to navigation Jump to search
Plot | Verbs | Class Commands Data Options Outputs Types | Colors Fonts Keys Text YAxes | Function Multi Shape

Plot is defined in class jzplot.

Public verbs are pd and plot.

Public nouns that specify the Windows form and isigraph control id:

Name Description Default
PForm form id plot
PFormhwnd form handle defined when the form is created
PId isigraph control id gs

Example: create two plots

The following draws two independent plots:

load 'jzplot'          NB. load plot class
a=: conew 'jzplot'     NB. create plot object a
b=: conew 'jzplot'     NB. create plot object b
plot__a */~ i:15       NB. draw plot in a
plot__b +./~ i:15      NB. draw plot in b

Example: add plot to a form

Prior to J8.02, you needed to define the public nouns PForm, PFormhwnd and PId. These override the default values.

require 'gtkwd jzplot' NB. load plot class (gtkwd only required by J7)

MYPLOT=: 0 : 0
pc myplot;
xywh 0 0 225 200;cc g0 isigraph ws_border rightscale bottommove;
xywh 231 3 40 12;cc close button leftmove rightmove;cn "Close";
pas 2 0;pcenter;
rem form end;
)

myplot_run=: 3 : 0
wd MYPLOT
loc=: conew 'jzplot'             NB. create plot object
PForm__loc=: 'myplot'            NB. define PForm in loc
PFormhwnd__loc=: wd 'qhwndp'     NB. define PFormhwnd in loc
PId__loc=: 'g0'                  NB. define PId in loc
'density' plot__loc 7|i.25 25    NB. draw plot on the form
wd 'pshow'
)

myplot_g0_paint=: 3 : 0
pd__loc 'show'                   NB. call show in paint handler
)

myplot_cancel=: myplot_close_button=: 3 : 0
destroy__loc''
wd 'pclose'
)

myplot_run''

In J8.02 plotting to an isigraph control has been somewhat simplified. The code below should be functionally equivalent to the example above.

require 'jzplot'

MYPLOT=: 0 : 0
pc myplot;
minwh 225 200;
cc g0 isigraph flush;
cc close button;cn "Close";
pas 0 0;
)

myplot_run=: 3 : 0
wd MYPLOT
loc=: ('myplot';'g0') conew 'jzplot'  NB. create plot object
'density' plot__loc 7|i.25 25         NB. draw plot on the form
wd 'pshow'
)

myplot_close=: myplot_close_button=: 3 : 0
wd 'pclose'
destroy__loc''
)

myplot_run''

Example: update a plot in a form

Continuing the previous (pre J8.02) example, suppose you need to update the plot, perhaps in response to a user interaction. You redraw the plot and then use glpaint '' to make the new plot visible:

myplot_g0_update =: 3 : 0
'density' plot__loc 5|i.25 25    NB. draw updated plot on the form
myplot_g0_paint ''               NB. call paint routine to draw pixels
glpaint_jgl2_ ''                 NB. copy updated pixels to the screen
)

The following extends the J8.02 example above, showing how to update the plot based on user interaction

require 'jzplot'

MYPLOT=: 0 : 0
pc myplot;
minwh 225 200;
cc g0 isigraph flush;
cc update button;cn "Update";
cc close button;cn "Close";
pas 0 0;
)

myplot_run=: 3 : 0
wd MYPLOT
Base0=: 7
loc=: ('myplot';'g0') conew 'jzplot'  NB. create plot object
'density' plot__loc Base0|i.25 25     NB. draw plot on the form
wd 'pshow'
)

myplot_close=: myplot_close_button=: 3 : 0
wd 'pclose'
destroy__loc''
)

myplot_update_button =: 3 : 0
Base0=: 3 + 9|Base0-1
'density' plot__loc Base0|i.25 25  NB. draw updated plot on the form
glpaint__loc ''                    NB. copy updated pixels to the screen
)

myplot_run''