BarTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Test\CpChart;
  3. use Codeception\Test\Unit;
  4. use CpChart\Data;
  5. use CpChart\Image;
  6. use UnitTester;
  7. class BarTest extends Unit
  8. {
  9. /**
  10. * @var UnitTester
  11. */
  12. protected $tester;
  13. public function testChartRender()
  14. {
  15. $data = new Data();
  16. $data->addPoints([-4, VOID, VOID, 12, 8, 3], "Probe 1");
  17. $data->addPoints([3, 12, 15, 8, 5, -5], "Probe 2");
  18. $data->addPoints([2, 0, 5, 18, 19, 22], "Probe 3");
  19. $data->setSerieTicks("Probe 2", 4);
  20. $data->setAxisName(0, "Temperatures");
  21. $data->addPoints(["Jan", "Feb", "Mar", "Apr", "May", "Jun"], "Labels");
  22. $data->setSerieDescription("Labels", "Months");
  23. $data->setAbscissa("Labels");
  24. $image = new Image(700, 230, $data);
  25. $image->drawFilledRectangle(
  26. 0,
  27. 0,
  28. 700,
  29. 230,
  30. ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107]
  31. );
  32. $settings = [
  33. "StartR" => 219,
  34. "StartG" => 231,
  35. "StartB" => 139,
  36. "EndR" => 1,
  37. "EndG" => 138,
  38. "EndB" => 68,
  39. "Alpha" => 50
  40. ];
  41. $image->drawGradientArea(0, 0, 700, 230, DIRECTION_VERTICAL, $settings);
  42. $image->drawGradientArea(
  43. 0,
  44. 0,
  45. 700,
  46. 20,
  47. DIRECTION_VERTICAL,
  48. ["StartR" => 0, "StartG" => 0, "StartB" => 0, "EndR" => 50, "EndG" => 50, "EndB" => 50, "Alpha" => 80]
  49. );
  50. $image->drawRectangle(0, 0, 699, 229, ["R" => 0, "G" => 0, "B" => 0]);
  51. $image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
  52. $image->drawText(10, 13, "drawBarChart() - draw a bar chart", ["R" => 255, "G" => 255, "B" => 255]);
  53. $image->setFontProperties(["FontName" => "Forgotte.ttf", "FontSize" => 11]);
  54. $image->drawText(250, 55, "Average temperature", ["FontSize" => 20, "Align" => TEXT_ALIGN_BOTTOMMIDDLE]);
  55. $image->setGraphArea(60, 60, 450, 190);
  56. $image->drawFilledRectangle(60, 60, 450, 190, ["R" => 255, "G" => 255, "B" => 255, "Surrounding" => -200, "Alpha" => 10]);
  57. $image->drawScale(["DrawSubTicks" => true]);
  58. $image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
  59. $image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
  60. $image->drawBarChart([
  61. "DisplayValues" => true,
  62. "DisplayColor" => DISPLAY_AUTO,
  63. "Rounded" => true,
  64. "Surrounding" => 60
  65. ]);
  66. $image->setShadow(false);
  67. $image->setGraphArea(500, 60, 670, 190);
  68. $image->drawFilledRectangle(500, 60, 670, 190, ["R" => 255, "G" => 255, "B" => 255, "Surrounding" => -200, "Alpha" => 10]);
  69. $image->drawScale(["Pos" => SCALE_POS_TOPBOTTOM, "DrawSubTicks" => true]);
  70. $image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
  71. $image->drawBarChart();
  72. $image->setShadow(false);
  73. /* Write the chart legend */
  74. $image->drawLegend(510, 205, ["Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL]);
  75. $filename = $this->tester->getOutputPathForChart('drawBarChart.png');
  76. $image->render($filename);
  77. $image->stroke();
  78. $this->tester->seeFileFound($filename);
  79. }
  80. }