GLE Library: graphutil.gle


! Subroutines for drawing graphs

sub graph_del wd sca
   ! Function returning the offset of the drawing area of a graph
   ! wd = width of the graph
   ! sca = hscale parameter of the graph
   return (wd-wd*sca)/2
end sub

sub graph_select wd hi hsc vsc xmn xmx ymn ymx
   ! Initialize graph variables and functions such as xg() and yg()
   ! wd = width of graph
   ! hi = height of graph
   ! hsc = hscale parameter of graph
   ! vsc = vscale parameter of graph
   ! xmn, xmx = minimum and maximum xaxis value
   ! ymn, ymx = minimum and maximum yaxis value
   begin graph
      size wd hi
      nobox
      hscale hsc
      vscale vsc
      xaxis min xmn max xmx off
      yaxis min ymn max ymx off
   end graph
   graphx = xpos()
   graphy = ypos()
end sub

sub graph_line x1 y1 x2 y2
   ! Function to draw a line on a graph
   ! x1, y2 = start point
   ! x2, y2 = end point
   amove xg(x1) yg(y1)
   aline xg(x2) yg(y2)
end sub

sub graph_hgrid nb color$
   ! Function to draw an horizontal equidistant grid on a graph
   ! (use 'under graph_hgrid 5 "red"' in your graph block)
   ! nb = number of lines in grid
   ! color$ = color for grid lines
   gsave
   local i
   set color color$
   for i = 1 to nb
      local yp = yg(ygmin+(ygmax-ygmin)/(nb+1)*i)
      amove xg(xgmin) yp
      aline xg(xgmax) yp
   next i
   grestore
end sub

sub graph_vgrid nb color$
   ! Function to draw a vertical equidistant grid on a graph
   ! (use 'under graph_vgrid 5 "red"' in your graph block)
   ! nb = number of lines in grid
   ! color$ = color for grid lines
   gsave
   local i
   set color color$
   for i = 1 to nb
      local xp = xg(xgmin+(xgmax-xgmin)/(nb+1)*i)
      amove xp yg(ygmin)
      aline xp yg(ygmax)
   next i
   grestore
end sub

sub graph_grid nbh nbv color$
   ! Function to draw an equidistant grid on a graph
   ! (use 'under graph_grid 5 "red"' in your graph block)
   ! nb = number of lines in grid
   ! color$ = color for grid lines
   graph_hgrid nbh color$
   graph_vgrid nbv color$
end sub

sub graph_origin
   ! Go to the origin of a graph selected with selectgraph
   amove graphx graphy
end sub

sub graph_zeroaxis style color$
   gsave
   tickw = 0.3*0.3+0.1
   set lstyle style color color$
   amove xg(xgmin)+tickw yg(0)
   aline xg(xgmax)-tickw yg(0)
   amove xg(0) yg(ygmin)+tickw
   aline xg(0) yg(ygmax)-tickw
   grestore
end sub

sub graph_textbox xp yp dx dy str$
   amove xg(xp)+dx+0.1 yg(yp)+dy+0.1
   begin box add 0.1 fill white
      write str$
   end box
end sub

sub graph_text xp yp dx dy str$
   amove xg(xp)+dx yg(yp)+dy
   write str$
end sub

sub invxg x
   local s = (xg(xgmax)-xg(xgmin))/(xgmax - xgmin)
   return (x - xg(xgmin) + s*xgmin)/s
end sub

sub invyg y
   local s = (yg(ygmax)-yg(ygmin))/(ygmax - ygmin)
   return (y - yg(ygmin) + s*ygmin)/s
end sub

sub graph_print_inv x y
   print "xg("+format$(invxg(x),"fix 2")+") yg("+format$(invyg(y),"fix 2")+")"
end sub