ScatterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace Test\CpChart;
  3. use Codeception\Test\Unit;
  4. use CpChart\Data;
  5. use CpChart\Image;
  6. use CpChart\Chart\Scatter;
  7. use UnitTester;
  8. class ScatterTest extends Unit
  9. {
  10. /**
  11. * @var UnitTester
  12. */
  13. protected $tester;
  14. public function testBestFitChartRender()
  15. {
  16. $data = new Data();
  17. for ($i = 0; $i <= 360; $i = $i + 10) {
  18. $data->addPoints(rand(1, 20) * 10 + rand(0, $i), "Probe 1");
  19. }
  20. for ($i = 0; $i <= 360; $i = $i + 10) {
  21. $data->addPoints(rand(1, 2) * 10 + rand(0, $i), "Probe 2");
  22. }
  23. $data->setAxisName(0, "X-Index");
  24. $data->setAxisXY(0, AXIS_X);
  25. $data->setAxisPosition(0, AXIS_POSITION_TOP);
  26. for ($i = 0; $i <= 360; $i = $i + 10) {
  27. $data->addPoints($i, "Probe 3");
  28. }
  29. $data->setSerieOnAxis("Probe 3", 1);
  30. $data->setAxisName(1, "Y-Index");
  31. $data->setAxisXY(1, AXIS_Y);
  32. $data->setAxisPosition(1, AXIS_POSITION_LEFT);
  33. $data->setScatterSerie("Probe 1", "Probe 3", 0);
  34. $data->setScatterSerieDescription(0, "This year");
  35. $data->setScatterSerieColor(0, ["R" => 0, "G" => 0, "B" => 0]);
  36. $data->setScatterSerie("Probe 2", "Probe 3", 1);
  37. $data->setScatterSerieDescription(1, "Last Year");
  38. $image = new Image(400, 400, $data);
  39. $settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
  40. $image->drawFilledRectangle(0, 0, 400, 400, $settings);
  41. $settings = ["StartR" => 219, "StartG" => 231, "StartB" => 139, "EndR" => 1, "EndG" => 138, "EndB" => 68, "Alpha" => 50];
  42. $image->drawGradientArea(0, 0, 400, 400, DIRECTION_VERTICAL, $settings);
  43. $image->drawGradientArea(0, 0, 400, 20, DIRECTION_VERTICAL, ["StartR" => 0, "StartG" => 0, "StartB" => 0, "EndR" => 50, "EndG" => 50, "EndB" => 50, "Alpha" => 80]);
  44. $image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
  45. $image->drawText(10, 13, "drawScatterBestFit() - Linear regression", ["R" => 255, "G" => 255, "B" => 255]);
  46. $image->drawRectangle(0, 0, 399, 399, ["R" => 0, "G" => 0, "B" => 0]);
  47. $image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
  48. $image->setGraphArea(50, 60, 350, 360);
  49. $scatterChart = new Scatter($image, $data);
  50. $scatterChart->drawScatterScale();
  51. $image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
  52. $scatterChart->drawScatterPlotChart();
  53. $scatterChart->drawScatterLegend(280, 380, ["Mode" => LEGEND_HORIZONTAL, "Style" => LEGEND_NOBORDER]);
  54. $scatterChart->drawScatterBestFit();
  55. $filename = $this->tester->getOutputPathForChart('drawScatterBestFit.png');
  56. $image->render($filename);
  57. $image->stroke();
  58. $this->tester->seeFileFound($filename);
  59. }
  60. public function testLineChartRender()
  61. {
  62. $data = new Data();
  63. for ($i = 0; $i <= 360; $i = $i + 10) {
  64. $data->addPoints(cos(deg2rad($i)) * 20, "Probe 1");
  65. }
  66. for ($i = 0; $i <= 360; $i = $i + 10) {
  67. $data->addPoints(sin(deg2rad($i)) * 20, "Probe 2");
  68. }
  69. $data->setAxisName(0, "Index");
  70. $data->setAxisXY(0, AXIS_X);
  71. $data->setAxisPosition(0, AXIS_POSITION_BOTTOM);
  72. for ($i = 0; $i <= 360; $i = $i + 10) {
  73. $data->addPoints($i, "Probe 3");
  74. }
  75. $data->setSerieOnAxis("Probe 3", 1);
  76. $data->setAxisName(1, "Degree");
  77. $data->setAxisXY(1, AXIS_Y);
  78. $data->setAxisUnit(1, "°");
  79. $data->setAxisPosition(1, AXIS_POSITION_RIGHT);
  80. $data->setScatterSerie("Probe 1", "Probe 3", 0);
  81. $data->setScatterSerieDescription(0, "This year");
  82. $data->setScatterSerieTicks(0, 4);
  83. $data->setScatterSerieColor(0, ["R" => 0, "G" => 0, "B" => 0]);
  84. $data->setScatterSerie("Probe 2", "Probe 3", 1);
  85. $data->setScatterSerieDescription(1, "Last Year");
  86. $image = new Image(400, 400, $data);
  87. $settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
  88. $image->drawFilledRectangle(0, 0, 400, 400, $settings);
  89. $settings = ["StartR" => 219, "StartG" => 231, "StartB" => 139, "EndR" => 1, "EndG" => 138, "EndB" => 68, "Alpha" => 50];
  90. $image->drawGradientArea(0, 0, 400, 400, DIRECTION_VERTICAL, $settings);
  91. $image->drawGradientArea(0, 0, 400, 20, DIRECTION_VERTICAL, ["StartR" => 0, "StartG" => 0, "StartB" => 0, "EndR" => 50, "EndG" => 50, "EndB" => 50, "Alpha" => 80]);
  92. $image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
  93. $image->drawText(10, 13, "drawScatterLineChart() - Draw a scatter line chart", ["R" => 255, "G" => 255, "B" => 255]);
  94. $image->drawRectangle(0, 0, 399, 399, ["R" => 0, "G" => 0, "B" => 0]);
  95. $image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
  96. $image->setGraphArea(50, 50, 350, 350);
  97. $scatterChart = new Scatter($image, $data);
  98. $scatterChart->drawScatterScale();
  99. $image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
  100. $scatterChart->drawScatterLineChart();
  101. $scatterChart->drawScatterLegend(280, 380, ["Mode" => LEGEND_HORIZONTAL, "Style" => LEGEND_NOBORDER]);
  102. $filename = $this->tester->getOutputPathForChart('drawScatterLine.png');
  103. $image->render($filename);
  104. $image->stroke();
  105. $this->tester->seeFileFound($filename);
  106. }
  107. public function testPlotChartRender()
  108. {
  109. $data = new Data();
  110. for ($i = 0; $i <= 360; $i = $i + 10) {
  111. $data->addPoints(cos(deg2rad($i)) * 20, "Probe 1");
  112. }
  113. for ($i = 0; $i <= 360; $i = $i + 10) {
  114. $data->addPoints(sin(deg2rad($i)) * 20, "Probe 2");
  115. }
  116. $data->setAxisName(0, "Index");
  117. $data->setAxisXY(0, AXIS_X);
  118. $data->setAxisPosition(0, AXIS_POSITION_BOTTOM);
  119. for ($i = 0; $i <= 360; $i = $i + 10) {
  120. $data->addPoints($i, "Probe 3");
  121. }
  122. $data->setSerieOnAxis("Probe 3", 1);
  123. $data->setAxisName(1, "Degree");
  124. $data->setAxisXY(1, AXIS_Y);
  125. $data->setAxisUnit(1, "°");
  126. $data->setAxisPosition(1, AXIS_POSITION_RIGHT);
  127. $data->setScatterSerie("Probe 1", "Probe 3", 0);
  128. $data->setScatterSerieDescription(0, "This year");
  129. $data->setScatterSerieColor(0, ["R" => 0, "G" => 0, "B" => 0]);
  130. $data->setScatterSerie("Probe 2", "Probe 3", 1);
  131. $data->setScatterSerieDescription(1, "Last Year");
  132. $data->setScatterSeriePicture(1, sprintf("%s/../_data/accept.png", __DIR__));
  133. $image = new Image(400, 400, $data);
  134. $settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
  135. $image->drawFilledRectangle(0, 0, 400, 400, $settings);
  136. $settings = ["StartR" => 219, "StartG" => 231, "StartB" => 139, "EndR" => 1, "EndG" => 138, "EndB" => 68, "Alpha" => 50];
  137. $image->drawGradientArea(0, 0, 400, 400, DIRECTION_VERTICAL, $settings);
  138. $image->drawGradientArea(0, 0, 400, 20, DIRECTION_VERTICAL, ["StartR" => 0, "StartG" => 0, "StartB" => 0, "EndR" => 50, "EndG" => 50, "EndB" => 50, "Alpha" => 80]);
  139. $image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
  140. $image->drawText(10, 13, "drawScatterPlotChart() - Draw a scatter plot chart", ["R" => 255, "G" => 255, "B" => 255]);
  141. $image->drawRectangle(0, 0, 399, 399, ["R" => 0, "G" => 0, "B" => 0]);
  142. $image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
  143. $image->setGraphArea(50, 50, 350, 350);
  144. $scatterChart = new Scatter($image, $data);
  145. $scatterChart->drawScatterScale();
  146. $image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
  147. $scatterChart->drawScatterPlotChart();
  148. $scatterChart->drawScatterLegend(260, 375, ["Mode" => LEGEND_HORIZONTAL, "Style" => LEGEND_NOBORDER]);
  149. $filename = $this->tester->getOutputPathForChart('drawScatterPlot.png');
  150. $image->render($filename);
  151. $image->stroke();
  152. $this->tester->seeFileFound($filename);
  153. }
  154. public function testSplineChartRender()
  155. {
  156. $data = new Data();
  157. for ($i = 0; $i <= 360; $i = $i + 90) {
  158. $data->addPoints(rand(1, 30), "Probe 1");
  159. }
  160. for ($i = 0; $i <= 360; $i = $i + 90) {
  161. $data->addPoints(rand(1, 30), "Probe 2");
  162. }
  163. $data->setAxisName(0, "Index");
  164. $data->setAxisXY(0, AXIS_X);
  165. $data->setAxisPosition(0, AXIS_POSITION_BOTTOM);
  166. for ($i = 0; $i <= 360; $i = $i + 90) {
  167. $data->addPoints($i, "Probe 3");
  168. }
  169. $data->setSerieOnAxis("Probe 3", 1);
  170. $data->setAxisName(1, "Degree");
  171. $data->setAxisXY(1, AXIS_Y);
  172. $data->setAxisUnit(1, "°");
  173. $data->setAxisPosition(1, AXIS_POSITION_RIGHT);
  174. $data->setScatterSerie("Probe 1", "Probe 3", 0);
  175. $data->setScatterSerieDescription(0, "This year");
  176. $data->setScatterSerieTicks(0, 4);
  177. $data->setScatterSerieColor(0, ["R" => 0, "G" => 0, "B" => 0]);
  178. $data->setScatterSerie("Probe 2", "Probe 3", 1);
  179. $data->setScatterSerieDescription(1, "Last Year");
  180. $image = new Image(400, 400, $data);
  181. $settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
  182. $image->drawFilledRectangle(0, 0, 400, 400, $settings);
  183. $settings = ["StartR" => 219, "StartG" => 231, "StartB" => 139, "EndR" => 1, "EndG" => 138, "EndB" => 68, "Alpha" => 50];
  184. $image->drawGradientArea(0, 0, 400, 400, DIRECTION_VERTICAL, $settings);
  185. $image->drawGradientArea(0, 0, 400, 20, DIRECTION_VERTICAL, ["StartR" => 0, "StartG" => 0, "StartB" => 0, "EndR" => 50, "EndG" => 50, "EndB" => 50, "Alpha" => 80]);
  186. $image->setFontProperties(["FontName" => "../fonts/Silkscreen.ttf", "FontSize" => 6]);
  187. $image->drawText(10, 13, "drawScatterSplineChart() - Draw a scatter spline chart", ["R" => 255, "G" => 255, "B" => 255]);
  188. $image->drawRectangle(0, 0, 399, 399, ["R" => 0, "G" => 0, "B" => 0]);
  189. $image->setFontProperties(["FontName" => "../fonts/pf_arma_five.ttf", "FontSize" => 6]);
  190. $image->setGraphArea(50, 50, 350, 350);
  191. $scatterChart = new Scatter($image, $data);
  192. $scatterChart->drawScatterScale();
  193. $image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
  194. $scatterChart->drawScatterSplineChart();
  195. $scatterChart->drawScatterPlotChart();
  196. $scatterChart->drawScatterLegend(280, 380, ["Mode" => LEGEND_HORIZONTAL, "Style" => LEGEND_NOBORDER]);
  197. $filename = $this->tester->getOutputPathForChart('drawScatterSpline.png');
  198. $image->render($filename);
  199. $image->stroke();
  200. $this->tester->seeFileFound($filename);
  201. }
  202. }