tsgraph.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. unit tsgraph;
  2. {$mode ObjFPC}
  3. interface
  4. uses
  5. Classes, SysUtils, tssql, ftFont,fpimage,fpimgcanv,fpWritePng,fpcanvas;
  6. Type
  7. { TTestSuiteGraph }
  8. TTestSuiteGraph = class(TObject)
  9. Private
  10. FVars : TQueryData;
  11. Protected
  12. procedure DoDrawPie(Img: TFPCustomImage; Skipped, Failed, Total: Integer);
  13. Public
  14. constructor create(aVars : TQueryData);
  15. procedure DrawPie(aPNGImage: TStream; Skipped, Failed, Total: Integer);
  16. end;
  17. implementation
  18. constructor TTestSuiteGraph.create(aVars: TQueryData);
  19. begin
  20. fVars:=aVars;
  21. ftFont.InitEngine;
  22. FontMgr.SearchPath:='/usr/share/fonts/truetype/liberation/';
  23. end;
  24. procedure TTestSuiteGraph.DrawPie(aPNGImage: TStream; Skipped, Failed, Total: Integer);
  25. var
  26. lImg : TFPMemoryImage;
  27. lWriter: TFPWriterPNG;
  28. begin
  29. lWriter:=Nil;
  30. lImg:=TFPMemoryImage.Create(320,320);
  31. try
  32. DoDrawPie(lImg,Skipped,Failed,Total);
  33. lWriter:=TFPWriterPNG.Create;
  34. lWriter.UseAlpha:=True;
  35. lWriter.ImageWrite(aPNGImage,lImg);
  36. aPNGImage.Position:=0;
  37. finally
  38. lWriter.Free;
  39. lImg.Free;
  40. end;
  41. end;
  42. procedure TTestSuiteGraph.DoDrawPie(Img: TFPCustomImage; Skipped, Failed, Total: Integer);
  43. Var
  44. Cnv : TFPImageCanvas;
  45. Procedure AddPie(X,Y,R : Integer; AStart,AStop : Double; Col : TFPColor);
  46. Var
  47. DX,Dy : Integer;
  48. begin
  49. DX:=Round(R*Cos(AStart));
  50. DY:=Round(R*Sin(AStart));
  51. Cnv.Line(X,Y,X+DX,Y-DY);
  52. DX:=Round(R*Cos(AStop));
  53. DY:=Round(R*Sin(AStop));
  54. Cnv.Line(X,Y,X+DX,Y-Dy);
  55. DX:=Round(R/2*Cos((AStart+AStop)/2));
  56. DY:=Round(R/2*Sin((AStart+AStop)/2));
  57. Cnv.Brush.FpColor:=Col;
  58. Cnv.FloodFill(X+DX,Y-DY);
  59. end;
  60. Function FractionAngle(F,T : Integer): Double;
  61. begin
  62. Result:=(2*Pi*(F/T))
  63. end;
  64. Var
  65. W,H,FH,CR,RA : Integer;
  66. A1,A2,FR,SR,PR : Double;
  67. R : TRect;
  68. F : TFreeTypeFont;
  69. begin
  70. F:=TFreeTypeFont.Create;
  71. With F do
  72. begin
  73. Name:='LiberationSans-Regular.ttf';
  74. FontIndex:=0;
  75. Size:=12;
  76. FPColor:=colred;
  77. AntiAliased:=False;
  78. Resolution:=96;
  79. end;
  80. if FVars.Debug then
  81. Writeln(stdout,'Creating image');
  82. Cnv:=TFPImageCanvas.Create(Img);
  83. if FVars.Debug then
  84. Writeln(stdout,'CNV=0x',hexstr(ptruint(cnv),16));
  85. if FVars.Debug then
  86. Writeln(stdout,'Getting width and height');
  87. W:=Img.Width;
  88. H:=Img.Height;
  89. if FVars.Debug then
  90. begin
  91. Writeln(stdout,'width=',W,' height=',H);
  92. //system.flush(stdout);
  93. end;
  94. // Writeln('Transparant');
  95. cnv.Brush.Style:=bsSolid;
  96. cnv.Brush.FPColor:=colTransparent;
  97. cnv.Pen.FPColor:=colWhite;
  98. Cnv.Rectangle(0,0,W,H);
  99. if FVars.DEbug then
  100. Writeln(stdout,'Setting font');
  101. Cnv.Font:=F;
  102. if FVars.Debug then
  103. Writeln(stdout,'Getting textwidth ');
  104. FH:=CNV.GetTextHeight('A');
  105. If FH=0 then
  106. FH:=14; // 3 * 14;
  107. if FVars.Debug then
  108. writeln(stdout,'FH=',FH);
  109. Inc(FH,3);
  110. R.Top:=FH*4;
  111. R.Left:=0;
  112. R.Bottom:=H;
  113. CR:=H-(FH*4);
  114. If W>CR then
  115. R.Right:=CR
  116. else
  117. R.Right:=W;
  118. Ra:=CR div 2;
  119. if FVars.DEbug then
  120. begin
  121. Writeln(stdout,'Setting pen color');
  122. system.flush(stdout);
  123. end;
  124. Cnv.Pen.FPColor:=colBlack;
  125. if FVars.Debug then
  126. begin
  127. Writeln(stdout,'Palette size : ',Img.Palette.Count);
  128. Writeln(stdout,'Setting brush style');
  129. system.flush(stdout);
  130. end;
  131. cnv.brush.FPColor:=colYellow;
  132. SR:=Skipped/Total;
  133. FR:=Failed/Total;
  134. PR:=1-SR-FR;
  135. cnv.font.FPColor:=colDkGray;
  136. Cnv.Textout(1,FH*2,Format('%d Skipped (%3.1f%%)',[Skipped,SR*100]));
  137. // cnv.pen.width:=1;
  138. // Writeln('Drawing ellipse');
  139. Cnv.Ellipse(R);
  140. if FVars.Debug then
  141. begin
  142. Writeln(stdout,'Setting text');
  143. system.flush(stdout);
  144. end;
  145. A1:=0;
  146. A2:=A1+FractionAngle(Failed,Total);
  147. cnv.font.FPColor:=colRed;
  148. Cnv.Textout(1,FH*3,Format('%d Failed (%3.1f%%)',[Failed,FR*100]));
  149. AddPie(Ra,R.Top+Ra,Ra,A1,A2,ColRed);
  150. cnv.font.FPColor:=colGreen;
  151. Cnv.Textout(1,FH,Format('%d Passed (%3.1f%%)',[Total-Skipped-Failed,PR*100]));
  152. // Writeln('Palette size : ',Img.Palette.Count);
  153. A1:=A2;
  154. A2:=A1+FractionAngle(Total-(Skipped+Failed),Total);
  155. AddPie(Ra,R.Top+Ra,Ra,A1,A2,ColGreen);
  156. // Writeln('Palette size : ',Img.Palette.Count);
  157. // Writeln('All done');
  158. end;
  159. end.