WMSimpleGraphs.Mod 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. MODULE WMSimpleGraphs; (** AUTHOR "Patrick Hunziker"; PURPOSE "Minimum-overhead graph drawing and storing"; *)
  2. (** right-click on navigator thumbnail allows window storage as image file *)
  3. (*ToDo: ticks for Graph. labels on axes *)
  4. (*ToDo: catch NaN and Inf in data and other strategies to avoid erratic window sizes*)
  5. IMPORT Strings, WMGraphics, WMRectangles, Modules, Reals, WM:=WMWindowManager, MathL;
  6. CONST Colors=[WMGraphics.Red,WMGraphics.Blue,WMGraphics.Green,WMGraphics.Yellow, WMGraphics.Magenta, WMGraphics.Cyan, WMGraphics.Gray];
  7. TYPE
  8. Regressor=PROCEDURE{DELEGATE}(CONST data: ARRAY [*,*] OF LONGREAL; VAR slope,intercept: LONGREAL);
  9. TYPE
  10. Window=OBJECT(WM.Window);
  11. END Window;
  12. Histogram* = OBJECT (Window);
  13. VAR
  14. data:ARRAY [*] OF LONGREAL;
  15. width,height:LONGINT;
  16. PROCEDURE &New*(CONST data: ARRAY [*] OF LONGREAL; CONST title: ARRAY OF CHAR);
  17. VAR max:LONGREAL;
  18. BEGIN
  19. SELF.data:=data;
  20. max:=MAX(data);
  21. width:=LEN(data,0); height:=ENTIER(max)+1;
  22. Init(10*width, 10*height, FALSE);
  23. WM.GetDefaultManager().Add(PosX, PosY, SELF, {WM.FlagFrame,WM.FlagClose});
  24. NewWindowPos(GetWidth());
  25. SetTitle(Strings.NewString(title));
  26. SetPointerInfo(manager.pointerCrosshair);
  27. END New;
  28. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  29. VAR i:LONGINT;
  30. BEGIN
  31. canvas.Fill(WMRectangles.MakeRect(0, 0, w, h), WMGraphics.White, WMGraphics.ModeCopy);
  32. FOR i:=0 TO LEN(data,0)-1 DO
  33. canvas.Fill(WMRectangles.MakeRect( i*w DIV width , h-ENTIER(data[i]*h / height),
  34. (i+1)*w DIV width , h), WMGraphics.Black, WMGraphics.ModeCopy);
  35. END;
  36. INC(timestamp);
  37. END Draw;
  38. END Histogram;
  39. (** display matrix values in checkerboard like fashion. positive values are in black/grey/white, negative values in red*)
  40. Matrix* = OBJECT (Window);
  41. VAR
  42. data:ARRAY [*,*] OF LONGREAL;
  43. width,height:LONGINT;
  44. max,min, offset, gain:LONGREAL;
  45. PROCEDURE &New*(CONST data: ARRAY [*,*] OF LONGREAL; CONST title: ARRAY OF CHAR);
  46. BEGIN
  47. SELF.data:=data;
  48. min:=MIN(data); max:=MAX(data);
  49. max:=MAX(ABS(min), ABS(max));
  50. min:=MIN(0, min);
  51. IF max=0 THEN max:=1 END;
  52. width:=MAX(1,LEN(data,0));
  53. height:=MAX(1,LEN(data,1));
  54. Init(width, height, TRUE);
  55. offset:=0; gain:=255/max;
  56. IF( width<10) OR (height<10) THEN
  57. bounds := WMGraphics.MakeRectangle(0, 0, 10*width, 10*height);(* grow small images *)
  58. END;
  59. WM.GetDefaultManager().Add(PosX, PosY, SELF, {WM.FlagFrame,WM.FlagClose});
  60. NewWindowPos(GetWidth());
  61. SetTitle(Strings.NewString(title));
  62. SetPointerInfo(manager.pointerCrosshair);
  63. END New;
  64. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  65. VAR col: WMGraphics.Color; x,y:LONGINT; val:LONGREAL; valI:LONGINT;
  66. BEGIN
  67. FOR y:=0 TO LEN(data,0)-1 DO
  68. FOR x:=0 TO LEN(data,1)-1 DO
  69. val:=data[y,x]; IF Reals.IsNaNL(val) THEN val:=0 END;
  70. valI:=ENTIER(offset+gain*val);
  71. valI:=MAX(-255, MIN( 255, valI));
  72. IF valI>=0 THEN col:=WMGraphics.RGBAToColor(valI,valI,valI,255);
  73. ELSE col:=WMGraphics.RGBAToColor(-valI,0,0,255);
  74. END;
  75. canvas.Fill(WMRectangles.MakeRect(x*w DIV width, h-ENTIER(0.5+(y+1)*h/height),
  76. (x+1)*w DIV width, h-ENTIER(0.5+y*h/height)),
  77. col, WMGraphics.ModeCopy);
  78. END;
  79. END;
  80. INC(timestamp);
  81. END Draw;
  82. END Matrix;
  83. Graph* = OBJECT (Window);
  84. CONST border=5;
  85. VAR
  86. data:ARRAY [*] OF LONGREAL;
  87. width,height:LONGINT;
  88. max,min:LONGREAL;
  89. PROCEDURE &New*(CONST data: ARRAY [*] OF LONGREAL; CONST title: ARRAY OF CHAR);
  90. BEGIN
  91. SELF.data:=data;
  92. max:=MAX(data);
  93. min:=MIN(0, MIN(data));
  94. width:=LEN(data,0); height:=ENTIER(max-min)+1;
  95. Init(10*width, 10*height, FALSE);
  96. WM.GetDefaultManager().Add(PosX, PosY, SELF, {WM.FlagFrame,WM.FlagClose});
  97. NewWindowPos(GetWidth());
  98. SetTitle(Strings.NewString(title));
  99. SetPointerInfo(manager.pointerCrosshair);
  100. END New;
  101. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  102. VAR i:LONGINT; mn,mx:LONGINT;
  103. BEGIN
  104. canvas.Fill(WMRectangles.MakeRect(0, 0, w, h), WMGraphics.White, WMGraphics.ModeCopy);
  105. mn:=-border+ENTIER(0.5+min*h / height);
  106. mx:=ENTIER(0.5+max*h / height);
  107. FOR i:=0 TO LEN(data,0)-2 DO
  108. canvas.Line(border+i*w DIV width, h+mn-ENTIER(0.5+data[i]*h / height),
  109. border+(i+1)*w DIV width, h+mn-ENTIER(0.5+data[i+1]*h / height),
  110. WMGraphics.Black, WMGraphics.ModeCopy);
  111. END;
  112. IF mn#0 THEN canvas.Line(0, h+mn, w, h+mn, WMGraphics.Black, WMGraphics.ModeCopy); END;
  113. INC(timestamp);
  114. END Draw;
  115. END Graph;
  116. Graphs* = OBJECT (Window);
  117. CONST border=5;
  118. VAR
  119. data:ARRAY [*,*] OF LONGREAL;
  120. width,height:LONGINT;
  121. max,min:LONGREAL;
  122. PROCEDURE &New*(CONST data: ARRAY [*,*] OF LONGREAL; CONST title: ARRAY OF CHAR);
  123. BEGIN
  124. SELF.data:=data;
  125. max:=MAX(data);
  126. min:=MIN(0, MIN(data));
  127. width:=LEN(data,1); height:=ENTIER(max-min)+1;
  128. Init(10*width, 10*height, FALSE);
  129. WM.GetDefaultManager().Add(PosX, PosY, SELF, {WM.FlagFrame,WM.FlagClose});
  130. NewWindowPos(GetWidth());
  131. SetTitle(Strings.NewString(title));
  132. SetPointerInfo(manager.pointerCrosshair);
  133. END New;
  134. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  135. VAR i,j:LONGINT; mn,mx:LONGINT;
  136. BEGIN
  137. canvas.Fill(WMRectangles.MakeRect(0, 0, w, h), WMGraphics.White, WMGraphics.ModeCopy);
  138. mn:=-border+ENTIER(0.5+min*h / height);
  139. mx:=ENTIER(0.5+max*h / height);
  140. FOR j:=0 TO LEN(data,0)-1 DO
  141. FOR i:=0 TO LEN(data,1)-2 DO
  142. canvas.Line(border+i*w DIV width, h+mn-ENTIER(0.5+data[j,i]*h / height),
  143. border+(i+1)*w DIV width, h+mn-ENTIER(0.5+data[j,i+1]*h / height),
  144. Colors[j MOD LEN(Colors,0)], WMGraphics.ModeCopy);
  145. END;
  146. END;
  147. IF mn#0 THEN canvas.Line(0, h+mn, w, h+mn, WMGraphics.Black, WMGraphics.ModeCopy); END;
  148. INC(timestamp);
  149. END Draw;
  150. END Graphs;
  151. GraphXY* = OBJECT (Window);
  152. CONST border=5;
  153. VAR
  154. data:ARRAY [*,*] OF LONGREAL;
  155. width,height:LONGINT;
  156. minx,miny,maxx,maxy:LONGREAL;
  157. ticks: ARRAY [*,*] OF LONGREAL;
  158. PROCEDURE &New*(CONST data: ARRAY [*,*] OF LONGREAL; CONST title: ARRAY OF CHAR);
  159. BEGIN
  160. SELF.data:=data;
  161. maxx:=MAX(0,MAX(data[0]));maxy:=MAX(0,MAX(data[1]));
  162. minx:=MIN(0, MIN(data[0])); miny:=MIN(0, MIN(data[1]));
  163. width:=ENTIER(maxx-minx)+1; height:=ENTIER(maxy-miny)+1;
  164. Init(10*width, 10*height, FALSE);
  165. WM.GetDefaultManager().Add(PosX, PosY, SELF, {WM.FlagFrame,WM.FlagClose});
  166. NewWindowPos(GetWidth());
  167. SetTitle(Strings.NewString(title));
  168. SetPointerInfo(manager.pointerCrosshair);
  169. ticks:=GetTicks(data);
  170. END New;
  171. PROCEDURE Axes(canvas: WMGraphics.Canvas; w,h,mnw,mnh:LONGINT; scalex,scaley:REAL);
  172. VAR i:LONGINT;
  173. BEGIN
  174. IF mnh#0 THEN canvas.Line(0, h+mnh, w, h+mnh, WMGraphics.Black, WMGraphics.ModeCopy) END;
  175. IF mnw#0 THEN canvas.Line(-mnw, 0, -mnw, h, WMGraphics.Black, WMGraphics.ModeCopy) END;
  176. FOR i:=0 TO LEN(ticks,1)-1 DO
  177. canvas.Line(-mnw+ENTIER(0.5+ticks[0,i]*scalex), h+mnh-1, -mnw+ENTIER(0.5+ticks[0,i]*scalex), h+mnh+1, WMGraphics.Black, WMGraphics.ModeCopy); (* ticks are at multiples of largest decimal unit *)
  178. canvas.Line(-mnw-1, h+mnh-ENTIER(0.5+ticks[1,i]*scaley), -mnw+1, h+mnh-ENTIER(0.5+ticks[1,i]*scaley), WMGraphics.Black, WMGraphics.ModeCopy)
  179. END;
  180. END Axes;
  181. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  182. VAR i:LONGINT; mnw,mnh,mxw,mxh:LONGINT; scalex,scaley:REAL;
  183. BEGIN
  184. canvas.Fill(WMRectangles.MakeRect(0, 0, w, h), WMGraphics.White, WMGraphics.ModeCopy);
  185. scalex:=w/width; scaley:=h/height;
  186. mnw:=-border+ENTIER(0.5+minx* scalex); mxw:=ENTIER(0.5+maxx* scalex);
  187. mnh:=-border+ENTIER(0.5+miny* scaley); mxh:=ENTIER(0.5+maxy* scaley);
  188. FOR i:=0 TO LEN(data,1)-2 DO
  189. canvas.Line(-mnw+ENTIER(0.5+data[0,i]*scalex), h+mnh-ENTIER(0.5+data[1,i]*scaley),
  190. -mnw+ENTIER(0.5+data[0,i+1]*scalex), h+mnh-ENTIER(0.5+data[1,i+1]*scaley),
  191. WMGraphics.Blue, WMGraphics.ModeCopy);
  192. END;
  193. Axes(canvas, w,h,mnw,mnh,scalex,scaley);
  194. INC(timestamp);
  195. END Draw;
  196. END GraphXY;
  197. (** scatter plot with optional error bars.
  198. data[0,..]: x coordinates
  199. data[1,..]: y coordinates
  200. optional data[2,..]: y error bars
  201. optional data[3,..]: x error bars *)
  202. TYPE Scatter* = OBJECT (GraphXY);
  203. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  204. VAR i:LONGINT; mnw,mnh,mxw,mxh, x,y, ex,ey:LONGINT; scalex,scaley:REAL; rect:WMRectangles.Rectangle;
  205. BEGIN
  206. canvas.Fill(WMRectangles.MakeRect(0, 0, w, h), WMGraphics.White, WMGraphics.ModeCopy);
  207. scalex:=w/width;
  208. scaley:=h/height;
  209. mnw:=-border+ENTIER(0.5+minx* scalex); mxw:=ENTIER(0.5+maxx* scalex);
  210. mnh:=-border+ENTIER(0.5+miny* scaley); mxh:=ENTIER(0.5+maxy* scaley);
  211. FOR i:=0 TO LEN(data,1)-1 DO
  212. x:=-mnw+ENTIER(0.5+data[0,i]*scalex);
  213. y:=h+mnh-ENTIER(0.5+data[1,i]*scaley);
  214. WMRectangles.SetRect(rect, x-1,y-1,x+2,y+2 );
  215. canvas.Fill(rect, WMGraphics.Black, WMGraphics.ModeCopy);
  216. IF LEN(data,0)>2 THEN (* vertical error bars*)
  217. ey:=ENTIER(0.5+data[2,i]*scaley);
  218. canvas.Line(x, y-ey, x, y+ey,WMGraphics.Blue, WMGraphics.ModeCopy);
  219. IF LEN(data,0)>3 THEN (*horizontal error bars*)
  220. ex:=ENTIER(0.5+data[3,i]*scalex);
  221. canvas.Line(x-ex, y, x+ex, y,WMGraphics.Red, WMGraphics.ModeCopy);
  222. END;
  223. END;
  224. END;
  225. Axes(canvas,w,h,mnw,mnh,scalex,scaley);
  226. INC(timestamp);
  227. END Draw;
  228. END Scatter;
  229. (** Regression plot. requires computation of slope,intercept by suited procedure, e.g. derived from /Matrix/StatisticsLinearRegression.SimpleRegression() *)
  230. TYPE Regression*= OBJECT (Scatter)
  231. VAR slope,intercept: LONGREAL;
  232. PROCEDURE &Initialize*(CONST data: ARRAY [*,*] OF LONGREAL; CONST title: ARRAY OF CHAR; regressor:Regressor);
  233. BEGIN
  234. New(data,title);
  235. regressor(data,slope,intercept);
  236. END Initialize;
  237. PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
  238. VAR mnw,mnh,x,y,xx,yy:LONGINT; scalex,scaley, x0, y0, x1, y1:LONGREAL;
  239. BEGIN
  240. Draw^(canvas,w,h,q);
  241. x0:= minx; y0:= x0*slope+intercept;
  242. IF (y0<miny) THEN y0:=miny; x0:=(y0-intercept)/slope;
  243. ELSIF y1>maxy THEN y0:=maxy; x0:=(y0-intercept)/slope;
  244. END;
  245. x1:=maxx; y1:=x1*slope+intercept;
  246. IF (y1<miny) THEN y1:=miny; x1:=(y1-intercept)/slope;
  247. ELSIF y1>maxy THEN y1:=maxy; x1:=(y1-intercept)/slope;
  248. END;
  249. scalex:=w/width; scaley:=h/height;
  250. mnw:=-border+ENTIER(0.5+minx* scalex); mnh:=-border+ENTIER(0.5+miny* scaley);
  251. x:=-mnw+ENTIER(0.5+x0*scalex); y:=h+mnh-ENTIER(0.5+y0*scaley);
  252. xx:=-mnw+ENTIER(0.5+x1*scalex); yy:=h+mnh-ENTIER(0.5+y1*scaley);
  253. canvas.Line(x,y,xx,yy,WMGraphics.Red, WMGraphics.ModeCopy);
  254. END Draw;
  255. END Regression;
  256. PROCEDURE NewWindowPos(dx:LONGINT);
  257. BEGIN
  258. INC(Pos,dx);
  259. PosX:=Pos MOD 900;
  260. PosY:=100+ (Pos DIV 900)*100 MOD 700;
  261. END NewWindowPos;
  262. PROCEDURE Log10(x:LONGREAL):LONGREAL;
  263. BEGIN RETURN MathL.ln(x)/MathL.ln(10);
  264. END Log10;
  265. PROCEDURE Exp10(x:LONGREAL):LONGREAL;
  266. BEGIN RETURN MathL.exp(ENTIER(x)*MathL.ln(10));
  267. END Exp10;
  268. PROCEDURE GetTicks(CONST data: ARRAY [*,*] OF LONGREAL): ARRAY [*, *] OF LONGREAL; (*ticks along X, along Y, at decimal units*)
  269. VAR maxx,maxy,stepx,stepy: LONGREAL; log:LONGREAL; steps,i:LONGINT;
  270. BEGIN
  271. maxx:=MAX(ABS(data[0]));
  272. log:=Log10(maxx);
  273. stepx:=Exp10(log);
  274. maxy:=MAX(ABS(data[1]));
  275. log:=Log10(maxy);
  276. stepy := Exp10(log);
  277. steps:=MAX(ENTIER(maxx/stepx), ENTIER(maxy/stepy));
  278. NEW(RESULT,2,steps);
  279. FOR i:=0 TO steps-1 DO
  280. RESULT[0,i]:=i*stepx;
  281. RESULT[1,i]:=i*stepy;
  282. END;
  283. RETURN RESULT
  284. END GetTicks;
  285. PROCEDURE DummyRegressor(CONST data: ARRAY [*,*] OF LONGREAL; VAR slope,intercept:LONGREAL);
  286. BEGIN
  287. slope:=1; intercept:=2;
  288. END DummyRegressor;
  289. VAR Pos, PosX,PosY: LONGINT;
  290. PROCEDURE Demo*;
  291. VAR h:Histogram; g:Graph; k: Graphs; gx:GraphXY; m:Matrix; s:Scatter; r:Regression;
  292. BEGIN {EXCLUSIVE}
  293. NEW(h, [4,7,8,4,5,9,6,5,3,2,12,17,3,0,2], "Histogram");
  294. NEW(g, [4,7,8,4,5,9,6,5,3,2,12,17,3,-3,2], "Graph");
  295. NEW(k, [[-2,7,8,4,5,9,6,4,7,8,4,5,9,6],
  296. [5,3,2,12,21,3,0,5,3,-2,12,17,4,1]], "MultiGraph");
  297. NEW(gx, [[0.2,-1,0,1,5,9,6,4,7,3,4,5,9,6],
  298. [0.2,3,4,7,12,3,0,5,3,-2,12,17,4,1]], "GraphXY");
  299. NEW(s, [ [0.2,-1,0,1,5,9,6,4,7,3,4,5,9,6],
  300. [0.2,3,4,7,12,3,0,5,3,-2,12,17,4,1],
  301. [1,0.6,1.1,1,1.4,1,1,1,0.7,1,1,0.8,1,1],
  302. [0.5,0.3,0.6,0.4,0.5,0.3,0.6,0.5,0.5,0.4,0.7,0.5,0.5,0.5]], "Scatter with x and y error bars");
  303. NEW(r, [ [0.2,-1,0,1,5,9,6,4,7,3,4,5,9,6],
  304. [0.2,3,4,7,12,3,0,5,3,-2,12,17,4,1],
  305. [1,1,1,1,1,1,1,1,1,1,1,1,1,1]], "Regression with y error bars", DummyRegressor);
  306. NEW(m, [[1,2,3,4],[4,3,2,4],[5,4,-2,-6],[3,1,0,-1]], "Matrix");
  307. END Demo;
  308. PROCEDURE Cleanup;
  309. VAR manager:WM.WindowManager; w,remove:WM.Window;
  310. BEGIN {EXCLUSIVE}
  311. manager:=WM.GetDefaultManager();
  312. manager.lock.AcquireWrite;
  313. w:=manager.GetFirst();
  314. WHILE w#NIL DO
  315. remove:=w;
  316. w:=manager.GetNext(w);
  317. IF (remove#NIL) & (remove IS Window) THEN manager.Remove(remove) END;
  318. END;
  319. manager.lock.ReleaseWrite;
  320. END Cleanup;
  321. BEGIN
  322. Modules.InstallTermHandler(Cleanup);
  323. Pos:=0; NewWindowPos(0);
  324. END WMSimpleGraphs.
  325. SystemTools.Free WMSimpleGraphs ~
  326. WMSimpleGraphs.Demo ~