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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. class GOOGLE_CHARTS
  3. {
  4. protected $URL = 'https://chart.googleapis.com/chart';
  5. protected $Labels = [];
  6. protected $Data = [];
  7. protected $Options = [];
  8. public function __construct($Type, $Width, $Height, $Options)
  9. {
  10. if ($Width * $Height > 300000 || $Height > 1000 || $Width > 1000) {
  11. trigger_error('Tried to make chart too large.');
  12. }
  13. $this->URL .= "?cht=$Type&amp;chs={$Width}x$Height";
  14. $this->Options = $Options;
  15. }
  16. protected function encode($Number)
  17. {
  18. if ($Number === -1) {
  19. return '__';
  20. }
  21. $CharKey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
  22. return $CharKey[floor($Number / 64)].$CharKey[floor($Number % 64)];
  23. }
  24. public function color($Colors)
  25. {
  26. $this->URL .= '&amp;chco='.$Colors;
  27. }
  28. public function lines($Thickness, $Solid = 1, $Blank = 0)
  29. {
  30. $this->URL .= "&amp;chls=$Thickness,$Solid,$Blank";
  31. }
  32. public function title($Title, $Color = '', $Size = '')
  33. {
  34. $this->URL .= '&amp;chtt='.str_replace(array(' ', "\n"), array('+', '|'), $Title);
  35. if (!empty($Color)) {
  36. $this->URL .= '&amp;chts='.$Color;
  37. }
  38. if (!empty($Size)) {
  39. $this->URL .= ','.$Size;
  40. }
  41. }
  42. public function legend($Items, $Placement = '')
  43. {
  44. $this->URL .= '&amp;chdl='.str_replace(' ', '+', implode('|', $Items));
  45. if (!empty($Placement)) {
  46. if (!in_array($Placement, array('b', 't', 'r', 'l', 'bv', 'tv'))) {
  47. trigger_error('Invalid legend placement.');
  48. }
  49. $this->URL .= '&amp;chdlp='.$Placement;
  50. }
  51. }
  52. public function add($Label, $Data)
  53. {
  54. if ($Label !== false) {
  55. $this->Labels[] = $Label;
  56. }
  57. $this->Data[] = $Data;
  58. }
  59. public function grid_lines($SpacingX = 0, $SpacingY = -1, $Solid = 1, $Blank = 1)
  60. {
  61. // Can take 2 more parameters for offset, but we're not bothering with that right now
  62. $this->URL .= "&amp;chg=$SpacingX,$SpacingY,$Solid,$Blank";
  63. }
  64. public function transparent()
  65. {
  66. $this->URL .= '&amp;chf=bg,s,FFFFFF00';
  67. }
  68. public function url()
  69. {
  70. return $this->URL;
  71. }
  72. }
  73. class AREA_GRAPH extends GOOGLE_CHARTS
  74. {
  75. public function __construct($Width, $Height, $Options = [])
  76. {
  77. parent::__construct('lc', $Width, $Height, $Options);
  78. }
  79. public function color($Color)
  80. {
  81. $this->URL .= '&amp;chco='.$Color.'&amp;chm=B,'.$Color.'50,0,0,0';
  82. }
  83. public function generate()
  84. {
  85. $Max = max($this->Data);
  86. $Min = ((isset($this->Options['Break'])) ? $Min = min($this->Data) : 0);
  87. $Data = [];
  88. foreach ($this->Data as $Value) {
  89. $Data[] = $this->encode((($Value - $Min) / ($Max - $Min)) * 4095);
  90. }
  91. $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);
  92. }
  93. }
  94. class PIE_CHART extends GOOGLE_CHARTS
  95. {
  96. public function __construct($Width, $Height, $Options = [])
  97. {
  98. $Type = ((isset($this->Options['3D'])) ? 'p3' : 'p');
  99. parent::__construct($Type, $Width, $Height, $Options);
  100. }
  101. public function generate()
  102. {
  103. $Sum = array_sum($this->Data);
  104. $Other = isset($this->Options['Other']);
  105. $Sort = isset($this->Options['Sort']);
  106. $LabelPercent = isset($this->Options['Percentage']);
  107. if ($Sort && !empty($this->Labels)) {
  108. array_multisort($this->Data, SORT_DESC, $this->Labels);
  109. } elseif ($Sort) {
  110. sort($this->Data);
  111. $this->Data = array_reverse($this->Data);
  112. }
  113. $Data = [];
  114. $Labels = $this->Labels;
  115. $OtherPercentage = 0.00;
  116. $OtherData = 0;
  117. foreach ($this->Data as $Key => $Value) {
  118. $ThisPercentage = number_format(($Value / $Sum) * 100, 2);
  119. $ThisData = ($Value / $Sum) * 4095;
  120. if ($Other && $ThisPercentage < 1) {
  121. $OtherPercentage += $ThisPercentage;
  122. $OtherData += $ThisData;
  123. unset($Data[$Key]);
  124. unset($Labels[$Key]);
  125. continue;
  126. }
  127. if ($LabelPercent) {
  128. $Labels[$Key] .= ' ('.$ThisPercentage.'%)';
  129. }
  130. $Data[] = $this->encode($ThisData);
  131. }
  132. if ($OtherPercentage > 0) {
  133. $OtherLabel = 'Other';
  134. if ($LabelPercent) {
  135. $OtherLabel .= ' ('.$OtherPercentage.'%)';
  136. }
  137. $Labels[] = $OtherLabel;
  138. $Data[] = $this->encode($OtherData);
  139. }
  140. $this->URL .= "&amp;chl=".implode('|', $Labels).'&amp;chd=e:'.implode('', $Data);
  141. }
  142. }
  143. /*
  144. class LOG_BAR_GRAPH extends GOOGLE_CHARTS
  145. {
  146. // todo: Finish
  147. public function __construct($Base, $Width, $Height, $Options = [])
  148. {
  149. parent::__construct('lc', $Width, $Height, $Options);
  150. }
  151. public function color($Color)
  152. {
  153. $this->URL .= '&amp;chco='.$Color.'&amp;chm=B,'.$Color.'50,0,0,0';
  154. }
  155. public function generate()
  156. {
  157. $Max = max($this->Data);
  158. $Min = ((isset($this->Options['Break'])) ? $Min = min($this->Data) : 0);
  159. $Data = [];
  160. foreach ($this->Data as $Value) {
  161. $Data[] = $this->encode((($Value - $Min) / ($Max - $Min)) * 4095);
  162. }
  163. $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);
  164. }
  165. }
  166. */
  167. /*
  168. class POLL_GRAPH extends GOOGLE_CHARTS
  169. {
  170. public function __construct()
  171. {
  172. $this->URL .= '?cht=bhg';
  173. }
  174. public function add($Label, $Data)
  175. {
  176. if ($Label !== false) {
  177. $this->Labels[] = Format::cut_string($Label, 35);
  178. }
  179. $this->Data[] = $Data;
  180. }
  181. public function generate()
  182. {
  183. $Count = count($this->Data);
  184. $Height = (30 * $Count) + 20;
  185. $Max = max($this->Data);
  186. $Sum = array_sum($this->Data);
  187. $Increment = ($Max / $Sum) * 25; // * 100% / 4divisions
  188. $Data = [];
  189. $Labels = [];
  190. foreach ($this->Data as $Key => $Value) {
  191. $Data[] = $this->encode(($Value / $Max) * 4095);
  192. $Labels[] = '@t'.str_replace(array(' ', ','), array('+', '\,'), $this->Labels[$Key]).',000000,1,'.round((($Key + 1) / $Count) - (12 / $Height), 2).':0,12';
  193. }
  194. $this->URL .= "&amp;chbh=25,0,5&amp;chs=214x$Height&amp;chl=0%|".round($Increment, 1)."%|".round($Increment * 2, 1)."%|".round($Increment * 3, 1)."%|".round($Increment * 4, 1)."%&amp;chm=".implode('|', $Labels).'&amp;chd=e:'.implode('', $Data);
  195. }
  196. }
  197. */