graph.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. //
  3. // vnStat PHP frontend (c)2006-2010 Bjorge Dijkstra (bjd@jooz.net)
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. //
  19. //
  20. // see file COPYING or at http://www.gnu.org/licenses/gpl.html
  21. // for more information.
  22. //
  23. require 'config.php';
  24. require 'localize.php';
  25. require 'vnstat.php';
  26. validate_input();
  27. require "./themes/$style/theme.php";
  28. function allocate_color($im, $colors)
  29. {
  30. return imagecolorallocatealpha($im, $colors[0], $colors[1], $colors[2], $colors[3]);
  31. }
  32. function init_image()
  33. {
  34. global $im, $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style;
  35. if ($graph == 'none')
  36. return;
  37. //
  38. // image object
  39. //
  40. $xlm = 70;
  41. $xrm = 20;
  42. $ytm = 35;
  43. $ybm = 60;
  44. if ($graph == 'small')
  45. {
  46. $iw = 300 + $xrm + $xlm;
  47. $ih = 100 + $ytm + $ybm;
  48. }
  49. else
  50. {
  51. $iw = 600 + $xrm + $xlm;
  52. $ih = 200 + $ytm + $ybm;
  53. }
  54. $im = imagecreatetruecolor($iw,$ih);
  55. //
  56. // colors
  57. //
  58. $cs = $colorscheme;
  59. $cl['image_background'] = allocate_color($im, $cs['image_background']);
  60. $cl['background'] = allocate_color($im, $cs['graph_background']);
  61. $cl['background_2'] = allocate_color($im, $cs['graph_background_2']);
  62. $cl['grid_stipple_1'] = allocate_color($im, $cs['grid_stipple_1']);
  63. $cl['grid_stipple_2'] = allocate_color($im, $cs['grid_stipple_2']);
  64. $cl['text'] = allocate_color($im, $cs['text']);
  65. $cl['border'] = allocate_color($im, $cs['border']);
  66. $cl['rx'] = allocate_color($im, $cs['rx']);
  67. $cl['rx_border'] = allocate_color($im, $cs['rx_border']);
  68. $cl['tx'] = allocate_color($im, $cs['tx']);
  69. $cl['tx_border'] = allocate_color($im, $cs['tx_border']);
  70. imagefilledrectangle($im,0,0,$iw,$ih,$cl['image_background']);
  71. imagefilledrectangle($im,$xlm,$ytm,$iw-$xrm,$ih-$ybm, $cl['background']);
  72. $x_step = ($iw - $xlm - $xrm) / 12;
  73. $depth = ($x_step / 8) + 4;
  74. imagefilledpolygon($im, array($xlm, $ytm, $xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $xlm - $depth, $ytm + $depth), 4, $cl['background_2']);
  75. imagefilledpolygon($im, array($xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $iw - $xrm - $depth, $ih - $ybm + $depth, $iw - $xrm, $ih - $ybm), 4, $cl['background_2']);
  76. // draw title
  77. $text = T('Traffic data for')." $iface";
  78. $bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
  79. $textwidth = $bbox[2] - $bbox[0];
  80. imagettftext($im, 10, 0, ($iw-$textwidth)/2, ($ytm/2), $cl['text'], GRAPH_FONT, $text);
  81. }
  82. function draw_border()
  83. {
  84. global $im,$cl,$iw,$ih;
  85. imageline($im, 0, 0,$iw-1, 0, $cl['border']);
  86. imageline($im, 0,$ih-1,$iw-1,$ih-1, $cl['border']);
  87. imageline($im, 0, 0, 0,$ih-1, $cl['border']);
  88. imageline($im, $iw-1, 0,$iw-1,$ih-1, $cl['border']);
  89. }
  90. function draw_grid($x_ticks, $y_ticks)
  91. {
  92. global $im, $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
  93. $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
  94. $y_step = ($ih - $ytm - $ybm) / $y_ticks;
  95. $depth = 10;//($x_step / 8) + 4;
  96. $ls = array($cl['grid_stipple_1'],$cl['grid_stipple_2']);
  97. imagesetstyle($im, $ls);
  98. for ($i=$xlm;$i<=($iw-$xrm); $i += $x_step)
  99. {
  100. imageline($im, $i, $ytm, $i, $ih - $ybm, IMG_COLOR_STYLED);
  101. imageline($im, $i, $ih - $ybm, $i - $depth, $ih - $ybm + $depth, IMG_COLOR_STYLED);
  102. }
  103. for ($i=$ytm;$i<=($ih-$ybm); $i += $y_step)
  104. {
  105. imageline($im, $xlm, $i, $iw - $xrm, $i, IMG_COLOR_STYLED);
  106. imageline($im, $xlm, $i, $xlm - $depth, $i + $depth, IMG_COLOR_STYLED);
  107. }
  108. imageline($im, $xlm, $ytm, $xlm, $ih - $ybm, $cl['border']);
  109. imageline($im, $xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm, $cl['border']);
  110. }
  111. function draw_data($data)
  112. {
  113. global $im,$cl,$iw,$ih,$xlm,$xrm,$ytm,$ybm;
  114. sort($data);
  115. $x_ticks = count($data);
  116. $y_ticks = 10;
  117. $y_scale = 1;
  118. $prescale = 1;
  119. $unit = 'K';
  120. $offset = 0;
  121. $gr_h = $ih - $ytm - $ybm;
  122. $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
  123. $y_step = ($ih - $ytm - $ybm) / $y_ticks;
  124. $bar_w = ($x_step / 2);
  125. // 找出最大值
  126. $low = 99999999999;
  127. $high = 0;
  128. for ($i=0; $i<$x_ticks; $i++)
  129. {
  130. if ($data[$i]['rx'] < $low)
  131. $low = $data[$i]['rx'];
  132. if ($data[$i]['tx'] < $low)
  133. $low = $data[$i]['tx'];
  134. if ($data[$i]['rx'] > $high)
  135. $high = $data[$i]['rx'];
  136. if ($data[$i]['tx'] > $high)
  137. $high = $data[$i]['tx'];
  138. }
  139. // 修改这里的单位转换逻辑
  140. $high = $high * 1.2; // 增加20%余量
  141. if ($high < 1024) {
  142. $unit = 'K';
  143. } else if ($high < 1048576) { // 1024 * 1024
  144. $unit = 'M';
  145. $prescale = 1024;
  146. $high = $high / 1024;
  147. } else if ($high < 1073741824) { // 1024 * 1024 * 1024
  148. $unit = 'G';
  149. $prescale = 1048576;
  150. $high = $high / 1048576;
  151. } else {
  152. $unit = 'T';
  153. $prescale = 1073741824;
  154. $high = $high / 1073741824;
  155. }
  156. // 计算合适的刻度
  157. $y_scale = ceil($high / $y_ticks);
  158. // 确保最小刻度
  159. if ($y_scale < 1) {
  160. $y_scale = 1;
  161. }
  162. draw_grid($x_ticks, $y_ticks);
  163. //
  164. // graph scale factor (per pixel)
  165. //
  166. imagesetthickness($im, 1);
  167. $sf = ($prescale * $y_scale * $y_ticks) / $gr_h;
  168. if (count($data) == 0)
  169. {
  170. $text = T('no data available');
  171. $bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
  172. $textwidth = $bbox[2] - $bbox[0];
  173. imagettftext($im, 10, 0, ($iw-$textwidth)/2, $ytm + 80, $cl['text'], GRAPH_FONT, $text);
  174. }
  175. else
  176. {
  177. //
  178. // draw bars
  179. //
  180. for ($i=0; $i<$x_ticks; $i++)
  181. {
  182. $x = $xlm + ($i * $x_step);
  183. $y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf);
  184. $depth = $x_step / 8;
  185. $space = 0;
  186. $x1 = $x;
  187. $y1 = $y;
  188. $x2 = $x + $bar_w - $space;
  189. $y2 = $ih - $ybm;
  190. imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['rx']);
  191. imagerectangle($im, $x1, $y1, $x2, $y2, $cl['rx_border']);
  192. imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 -$depth, $y2 + $depth, $cl['rx']);
  193. imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['rx_border']);
  194. imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx']);
  195. imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx_border']);
  196. imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx']);
  197. imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx_border']);
  198. $y1 = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['tx'] - $offset) / $sf);
  199. $x1 = $x1 + $bar_w;
  200. $x2 = $x2 + $bar_w;
  201. imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['tx']);
  202. imagerectangle($im, $x1, $y1, $x2, $y2, $cl['tx_border']);
  203. imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx']);
  204. imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx_border']);
  205. imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx']);
  206. imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx_border']);
  207. imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx']);
  208. imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx_border']);
  209. }
  210. //
  211. // axis labels
  212. //
  213. for ($i=0; $i<=$y_ticks; $i++)
  214. {
  215. $label = ($i * $y_scale).$unit;
  216. $bbox = imagettfbbox(8, 0, GRAPH_FONT, $label);
  217. $textwidth = $bbox[2] - $bbox[0];
  218. imagettftext($im, 8, 0, $xlm - $textwidth - 16, ($ih - $ybm) - ($i * $y_step) + 8 + $depth, $cl['text'], GRAPH_FONT, $label);
  219. }
  220. for ($i=0; $i<$x_ticks; $i++)
  221. {
  222. $label = $data[$i]['img_label'];
  223. $bbox = imagettfbbox(9, 0, GRAPH_FONT, $label);
  224. $textwidth = $bbox[2] - $bbox[0];
  225. imagettftext($im, 9, 0, $xlm + ($i * $x_step) + ($x_step / 2) - ($textwidth / 2) - $depth - 4, $ih - $ybm + 20 + $depth, $cl['text'], GRAPH_FONT, $label);
  226. }
  227. }
  228. draw_border();
  229. //
  230. // legend
  231. //
  232. imagefilledrectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['rx']);
  233. imagerectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['text']);
  234. imagettftext($im, 8,0, $xlm+14, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes in'));
  235. imagefilledrectangle($im, $xlm+120 , $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['tx']);
  236. imagerectangle($im, $xlm+120, $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['text']);
  237. imagettftext($im, 8,0, $xlm+134, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes out'));
  238. }
  239. function output_image()
  240. {
  241. global $page,$hour,$day,$month,$im,$iface;
  242. if ($page == 'summary')
  243. return;
  244. init_image();
  245. if ($page == 'h')
  246. {
  247. draw_data($hour);
  248. }
  249. else if ($page == 'd')
  250. {
  251. draw_data($day);
  252. }
  253. else if ($page == 'm')
  254. {
  255. draw_data($month);
  256. }
  257. header('Content-type: image/png');
  258. imagepng($im);
  259. }
  260. get_vnstat_data();
  261. output_image();
  262. ?>