$high) $high = $data[$i]['rx']; if ($data[$i]['tx'] > $high) $high = $data[$i]['tx']; } // 修改这里的单位转换逻辑 $high = $high * 1.2; // 增加20%余量 if ($high < 1024) { $unit = 'K'; } else if ($high < 1048576) { // 1024 * 1024 $unit = 'M'; $prescale = 1024; $high = $high / 1024; } else if ($high < 1073741824) { // 1024 * 1024 * 1024 $unit = 'G'; $prescale = 1048576; $high = $high / 1048576; } else { $unit = 'T'; $prescale = 1073741824; $high = $high / 1073741824; } // 计算合适的刻度 $y_scale = ceil($high / $y_ticks); // 确保最小刻度 if ($y_scale < 1) { $y_scale = 1; } draw_grid($x_ticks, $y_ticks); // // graph scale factor (per pixel) // imagesetthickness($im, 1); $sf = ($prescale * $y_scale * $y_ticks) / $gr_h; if (count($data) == 0) { $text = T('no data available'); $bbox = imagettfbbox(10, 0, GRAPH_FONT, $text); $textwidth = $bbox[2] - $bbox[0]; imagettftext($im, 10, 0, ($iw-$textwidth)/2, $ytm + 80, $cl['text'], GRAPH_FONT, $text); } else { // // draw bars // for ($i=0; $i<$x_ticks; $i++) { $x = $xlm + ($i * $x_step); $y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf); $depth = $x_step / 8; $space = 0; $x1 = $x; $y1 = $y; $x2 = $x + $bar_w - $space; $y2 = $ih - $ybm; imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['rx']); imagerectangle($im, $x1, $y1, $x2, $y2, $cl['rx_border']); imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 -$depth, $y2 + $depth, $cl['rx']); imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['rx_border']); imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx']); imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['rx_border']); imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx']); imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['rx_border']); $y1 = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['tx'] - $offset) / $sf); $x1 = $x1 + $bar_w; $x2 = $x2 + $bar_w; imagefilledrectangle($im, $x1, $y1, $x2, $y2, $cl['tx']); imagerectangle($im, $x1, $y1, $x2, $y2, $cl['tx_border']); imagefilledrectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx']); imagerectangle($im, $x1 - $depth, $y1 + $depth, $x2 - $depth, $y2 + $depth, $cl['tx_border']); imagefilledpolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx']); imagepolygon($im, array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth), 4, $cl['tx_border']); imagefilledpolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx']); imagepolygon($im, array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth), 4, $cl['tx_border']); } // // axis labels // for ($i=0; $i<=$y_ticks; $i++) { $label = ($i * $y_scale).$unit; $bbox = imagettfbbox(8, 0, GRAPH_FONT, $label); $textwidth = $bbox[2] - $bbox[0]; imagettftext($im, 8, 0, $xlm - $textwidth - 16, ($ih - $ybm) - ($i * $y_step) + 8 + $depth, $cl['text'], GRAPH_FONT, $label); } for ($i=0; $i<$x_ticks; $i++) { $label = $data[$i]['img_label']; $bbox = imagettfbbox(9, 0, GRAPH_FONT, $label); $textwidth = $bbox[2] - $bbox[0]; imagettftext($im, 9, 0, $xlm + ($i * $x_step) + ($x_step / 2) - ($textwidth / 2) - $depth - 4, $ih - $ybm + 20 + $depth, $cl['text'], GRAPH_FONT, $label); } } draw_border(); // // legend // imagefilledrectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['rx']); imagerectangle($im, $xlm, $ih-$ybm+39, $xlm+8,$ih-$ybm+47,$cl['text']); imagettftext($im, 8,0, $xlm+14, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes in')); imagefilledrectangle($im, $xlm+120 , $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['tx']); imagerectangle($im, $xlm+120, $ih-$ybm+39, $xlm+128,$ih-$ybm+47,$cl['text']); imagettftext($im, 8,0, $xlm+134, $ih-$ybm+48,$cl['text'], GRAPH_FONT,T('bytes out')); } function output_image() { global $page,$hour,$day,$month,$im,$iface; if ($page == 'summary') return; init_image(); if ($page == 'h') { draw_data($hour); } else if ($page == 'd') { draw_data($day); } else if ($page == 'm') { draw_data($month); } header('Content-type: image/png'); imagepng($im); } get_vnstat_data(); output_image(); ?>