BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

charts.class.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. #declare(strict_types=1);
  3. class GOOGLE_CHARTS
  4. {
  5. protected $URL = 'https://chart.googleapis.com/chart';
  6. protected $Labels = [];
  7. protected $Data = [];
  8. protected $Options = [];
  9. public function __construct($Type, $Width, $Height, $Options)
  10. {
  11. if ($Width * $Height > 300000 || $Height > 1000 || $Width > 1000) {
  12. trigger_error('Tried to make chart too large.');
  13. }
  14. $this->URL .= "?cht=$Type&amp;chs={$Width}x$Height";
  15. $this->Options = $Options;
  16. }
  17. protected function encode($Number)
  18. {
  19. if ($Number === -1) {
  20. return '__';
  21. }
  22. $CharKey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
  23. return $CharKey[floor($Number / 64)].$CharKey[floor($Number % 64)];
  24. }
  25. public function color($Colors)
  26. {
  27. $this->URL .= '&amp;chco='.$Colors;
  28. }
  29. public function lines($Thickness, $Solid = 1, $Blank = 0)
  30. {
  31. $this->URL .= "&amp;chls=$Thickness,$Solid,$Blank";
  32. }
  33. public function title($Title, $Color = '', $Size = '')
  34. {
  35. $this->URL .= '&amp;chtt='.str_replace(array(' ', "\n"), array('+', '|'), $Title);
  36. if (!empty($Color)) {
  37. $this->URL .= '&amp;chts='.$Color;
  38. }
  39. if (!empty($Size)) {
  40. $this->URL .= ','.$Size;
  41. }
  42. }
  43. public function legend($Items, $Placement = '')
  44. {
  45. $this->URL .= '&amp;chdl='.str_replace(' ', '+', implode('|', $Items));
  46. if (!empty($Placement)) {
  47. if (!in_array($Placement, array('b', 't', 'r', 'l', 'bv', 'tv'))) {
  48. trigger_error('Invalid legend placement.');
  49. }
  50. $this->URL .= '&amp;chdlp='.$Placement;
  51. }
  52. }
  53. public function add($Label, $Data)
  54. {
  55. if ($Label !== false) {
  56. $this->Labels[] = $Label;
  57. }
  58. $this->Data[] = $Data;
  59. }
  60. public function grid_lines($SpacingX = 0, $SpacingY = -1, $Solid = 1, $Blank = 1)
  61. {
  62. // Can take 2 more parameters for offset, but we're not bothering with that right now
  63. $this->URL .= "&amp;chg=$SpacingX,$SpacingY,$Solid,$Blank";
  64. }
  65. public function transparent()
  66. {
  67. $this->URL .= '&amp;chf=bg,s,FFFFFF00';
  68. }
  69. public function url()
  70. {
  71. return $this->URL;
  72. }
  73. }
  74. class AREA_GRAPH extends GOOGLE_CHARTS
  75. {
  76. public function __construct($Width, $Height, $Options = [])
  77. {
  78. parent::__construct('lc', $Width, $Height, $Options);
  79. }
  80. public function color($Color)
  81. {
  82. $this->URL .= '&amp;chco='.$Color.'&amp;chm=B,'.$Color.'50,0,0,0';
  83. }
  84. public function generate()
  85. {
  86. $Max = max($this->Data);
  87. $Min = ((isset($this->Options['Break'])) ? $Min = min($this->Data) : 0);
  88. $Data = [];
  89. foreach ($this->Data as $Value) {
  90. $Data[] = $this->encode((($Value - $Min) / ($Max - $Min)) * 4095);
  91. }
  92. $this->URL .= "&amp;chxt=y,x&amp;chxs=0,h&amp;chxl=1:|".implode('|', $this->Labels).'&amp;chxr=0,'.$Min.','.($Max - $Min).'&amp;chd=e:'.implode('', $Data);
  93. }
  94. }
  95. class PIE_CHART extends GOOGLE_CHARTS
  96. {
  97. public function __construct($Width, $Height, $Options = [])
  98. {
  99. $Type = ((isset($this->Options['3D'])) ? 'p3' : 'p');
  100. parent::__construct($Type, $Width, $Height, $Options);
  101. }
  102. public function generate()
  103. {
  104. $Sum = array_sum($this->Data);
  105. $Other = isset($this->Options['Other']);
  106. $Sort = isset($this->Options['Sort']);
  107. $LabelPercent = isset($this->Options['Percentage']);
  108. if ($Sort && !empty($this->Labels)) {
  109. array_multisort($this->Data, SORT_DESC, $this->Labels);
  110. } elseif ($Sort) {
  111. sort($this->Data);
  112. $this->Data = array_reverse($this->Data);
  113. }
  114. $Data = [];
  115. $Labels = $this->Labels;
  116. $OtherPercentage = 0.00;
  117. $OtherData = 0;
  118. foreach ($this->Data as $Key => $Value) {
  119. $ThisPercentage = number_format(($Value / $Sum) * 100, 2);
  120. $ThisData = ($Value / $Sum) * 4095;
  121. if ($Other && $ThisPercentage < 1) {
  122. $OtherPercentage += $ThisPercentage;
  123. $OtherData += $ThisData;
  124. unset($Data[$Key]);
  125. unset($Labels[$Key]);
  126. continue;
  127. }
  128. if ($LabelPercent) {
  129. $Labels[$Key] .= ' ('.$ThisPercentage.'%)';
  130. }
  131. $Data[] = $this->encode($ThisData);
  132. }
  133. if ($OtherPercentage > 0) {
  134. $OtherLabel = 'Other';
  135. if ($LabelPercent) {
  136. $OtherLabel .= ' ('.$OtherPercentage.'%)';
  137. }
  138. $Labels[] = $OtherLabel;
  139. $Data[] = $this->encode($OtherData);
  140. }
  141. $this->URL .= "&amp;chl=".implode('|', $Labels).'&amp;chd=e:'.implode('', $Data);
  142. }
  143. }