function draw_data($data) { global $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm; sort($data); $x_ticks = count($data); $y_ticks = 10; $y_scale = 1; $prescale = 1; $unit = 'K'; $offset = 0; $gr_h = $ih - $ytm - $ybm; $x_step = ($iw - $xlm - $xrm) / $x_ticks; $y_step = ($ih - $ytm - $ybm) / $y_ticks; $bar_w = $x_step / 2; // // determine scale // $low = 99999999999; $high = 0; for ($i = 0; $i < $x_ticks; $i++) { if ($data[$i]['rx'] < $low) { $low = $data[$i]['rx']; } if ($data[$i]['tx'] < $low) { $low = $data[$i]['tx']; } if ($data[$i]['rx'] > $high) { $high = $data[$i]['rx']; } if ($data[$i]['tx'] > $high) { $high = $data[$i]['tx']; } } while ($high > $prescale * $y_scale * $y_ticks) { $y_scale = $y_scale * 2; if ($y_scale >= 1024) { $prescale = $prescale * 1024; $y_scale = $y_scale / 1024; if ($unit == 'K') { $unit = 'M'; } else { if ($unit == 'M') { $unit = 'G'; } else { if ($unit == 'G') { $unit = 'T'; } } } } } draw_grid($x_ticks, $y_ticks); // // graph scale factor (per pixel) // $sf = $prescale * $y_scale * $y_ticks / $gr_h; if ($data[0] == 'nodata') { $text = 'no data available'; svg_text($iw / 2, $ytm + 80, $text, array('stroke' => $cl['text']['rgb'], 'fill' => $cl['text']['rgb'], 'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-size' => '16pt', 'text-anchor' => 'middle')); } 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_ticks < 20 ? 8 : 6; $space = 0; $x1 = (int) $x; $y1 = (int) $y; $w = (int) ($bar_w - $space); $h = (int) ($ih - $ybm - $y); $x2 = (int) ($x + $bar_w - $space); $y2 = (int) ($ih - $ybm); svg_group(array('stroke' => $cl['rx_border']['rgb'], 'stroke-opacity' => $cl['rx_border']['opacity'], 'stroke-width' => 1, 'stroke-linejoin' => 'round', 'fill' => $cl['rx']['rgb'], 'fill-opacity' => $cl['rx']['opacity'])); svg_rect($x1, $y1, $w, $h); svg_rect($x1 - $depth, $y1 + $depth, $w, $h); svg_poly(array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth)); svg_poly(array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth)); svg_group_end(); $y1 = (int) ($ytm + ($ih - $ytm - $ybm) - ($data[$i]['tx'] - $offset) / $sf); $x1 = (int) ($x1 + $bar_w); $x2 = (int) ($x2 + $bar_w); $w = (int) ($bar_w - $space); $h = (int) ($ih - $ybm - $y1 - 1); svg_group(array('stroke' => $cl['tx_border']['rgb'], 'stroke-opacity' => $cl['tx_border']['opacity'], 'stroke-width' => 1, 'stroke-linejoin' => 'round', 'fill' => $cl['tx']['rgb'], 'fill-opacity' => $cl['tx']['opacity'])); svg_rect($x1, $y1, $w, $h); svg_rect($x1 - $depth, $y1 + $depth, $w, $h); svg_poly(array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth)); svg_poly(array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth)); svg_group_end(); } // // axis labels // svg_group(array('fill' => $cl['text']['rgb'], 'fill-opacity' => $cl['text']['opacity'], 'stroke-width' => '0', 'font-family' => SVG_FONT, 'font-size' => '10pt', 'text-anchor' => 'end')); for ($i = 0; $i <= $y_ticks; $i++) { $label = $i * $y_scale . $unit; $tx = $xlm - 16; $ty = (int) ($ih - $ybm - $i * $y_step + 8 + $depth); svg_text($tx, $ty, $label); } svg_group_end(); svg_group(array('fill' => $cl['text']['rgb'], 'fill-opacity' => $cl['text']['opacity'], 'stroke-width' => '0', 'font-family' => SVG_FONT, 'font-size' => '10pt', 'text-anchor' => 'middle')); for ($i = 0; $i < $x_ticks; $i++) { $label = $data[$i]['img_label']; svg_text($xlm + $i * $x_step + $x_step / 2 - $depth - 4, $ih - $ybm + 20 + $depth, $label); } svg_group_end(); } draw_border(); // // legend // svg_rect($xlm, $ih - $ybm + 39, 8, 8, array('stroke' => $cl['text']['rgb'], 'stroke-width' => 1, 'fill' => $cl['rx']['rgb'])); svg_text($xlm + 14, $ih - $ybm + 48, T('bytes in'), array('fill' => $cl['text']['rgb'], 'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-size' => '8pt')); svg_rect($xlm + 120, $ih - $ybm + 39, 8, 8, array('stroke' => $cl['text']['rgb'], 'stroke-width' => 1, 'fill' => $cl['tx']['rgb'])); svg_text($xlm + 134, $ih - $ybm + 48, T('bytes out'), array('fill' => $cl['text']['rgb'], 'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-size' => '8pt')); }
function draw_data($data) { global $im, $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm; sort($data); $x_ticks = count($data); $y_ticks = 10; $y_scale = 1; $prescale = 1; $unit = 'K'; $offset = 0; $gr_h = $ih - $ytm - $ybm; $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1); $y_step = ($ih - $ytm - $ybm) / $y_ticks; $bar_w = $x_step / 2; // // determine scale // $low = 99999999999; $high = 0; for ($i = 0; $i < $x_ticks; $i++) { if ($data[$i]['rx'] < $low) { $low = $data[$i]['rx']; } if ($data[$i]['tx'] < $low) { $low = $data[$i]['tx']; } if ($data[$i]['rx'] > $high) { $high = $data[$i]['rx']; } if ($data[$i]['tx'] > $high) { $high = $data[$i]['tx']; } } while ($high > $prescale * $y_scale * $y_ticks) { $y_scale = $y_scale * 2; if ($y_scale >= 1024) { $prescale = $prescale * 1024; $y_scale = $y_scale / 1024; if ($unit == 'K') { $unit = 'M'; } else { if ($unit == 'M') { $unit = 'G'; } else { if ($unit == 'G') { $unit = 'T'; } } } } } 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')); }
$minYVal = min(min($y), min($y1)); if ($maxXVal == $minXVal) { $maxXVal += 10; $minXVal -= 10; } if ($maxYVal == $minYVal) { $maxYVal += 6; $minYVal -= 6; } //вычисляем масштаб преобразования данных в координаты рабочей области $scaleX = ($maxX - $x0) / ($maxXVal - $minXVal); $scaleY = ($maxY - $y0) / ($maxYVal - $minYVal); //задаем шаг для координатной сетки в пикселах $xStep = 30; $yStep = 30; draw_border($xStep, $yStep, round($xStep / $scaleX, 3), round($yStep / $scaleY, 5)); //отрисовка единственной точки if (count($x) == 1) { $x[1] = $x[0] - 0.01 * ($maxXVal - $minXVal); $y[1] = $y[0] - 0.01 * ($maxYVal - $minYVal); $x[2] = $x[1]; $y[2] = $y[1] + 0.02 * ($maxYVal - $minYVal); $x[3] = $x[2] + 0.02 * ($maxXVal - $minXVal); $y[3] = $y[2]; $x[4] = $x[3]; $y[4] = $y[3] - 0.02 * ($maxYVal - $minYVal); $x[5] = $x[4] - 0.02 * ($maxXVal - $minXVal); $y[5] = $y[4]; } for ($i = 0; $i < count($x); $i++) { $x[$i] -= $minXVal;