📄 graph_svg.php
字号:
<?php require 'config.php'; require 'vnstat.php'; function svg_create($width, $height) { header('Content-type: image/svg+xml'); print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"; print "<svg width=\"$width\" height=\"$height\" version=\"1.2\" baseProfile=\"tiny\" xmlns=\"http://www.w3.org/2000/svg\">\n"; print "<g style=\"shape-rendering: crispEdges\">\n"; } function svg_end() { print "</g>\n"; print "</svg>\n"; } function svg_options($options) { foreach ($options as $key => $value) { print "$key=\"$value\" "; } } function svg_group($options) { print "<g "; svg_options($options); print ">\n"; } function svg_group_end() { print "</g>\n"; } function svg_text($x, $y, $text, $options = array()) { print "<text x=\"$x\" y=\"$y\" "; svg_options($options); print ">$text</text>\n"; } function svg_line($x1, $y1, $x2, $y2, $options = array()) { print "<line x1=\"$x1\" y1=\"$y1\" x2=\"$x2\" y2=\"$y2\" "; svg_options($options); print "/>\n"; } function svg_rect($x, $y, $w, $h, $options = array()) { print "<rect x=\"$x\" y=\"$y\" width=\"$w\" height=\"$h\" "; svg_options($options); print "/>\n"; } function svg_poly($points, $options = array()) { print "<polygon points=\""; for ($p = 0; $p < count($points); $p += 2) { print $points[$p].','.$points[$p+1].' '; } svg_options($options); print "\"/>\n"; } function allocate_color($colors) { $col['rgb'] = sprintf("#%02X%02X%02X", $colors[0], $colors[1], $colors[2]); $col['opacity'] = (127 - $colors[3]) / 127; return $col; } function init_image() { global $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style, $iface_title; if ($graph == 'none') return; // // image object // $xlm = 70; $xrm = 20; $ytm = 35; $ybm = 60; if ($graph == 'small') { $iw = 300 + $xrm + $xlm; $ih = 100 + $ytm + $ybm; } else { $iw = 600 + $xrm + $xlm; $ih = 200 + $ytm + $ybm; } svg_create($iw, $ih); // // colors // $cs = $colorscheme[$style]; $cl['image_background'] = allocate_color($cs['image_background']); $cl['background'] = allocate_color($cs['graph_background']); $cl['background_2'] = allocate_color($cs['graph_background_2']); $cl['grid_stipple_1'] = allocate_color($cs['grid_stipple_1']); $cl['grid_stipple_2'] = allocate_color($cs['grid_stipple_2']); $cl['text'] = allocate_color($cs['text']); $cl['border'] = allocate_color($cs['border']); $cl['rx'] = allocate_color($cs['rx']); $cl['rx_border'] = allocate_color($cs['rx_border']); $cl['tx'] = allocate_color($cs['tx']); $cl['tx_border'] = allocate_color($cs['tx_border']); svg_rect(0, 0, $iw, $ih, array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['image_background']['rgb']) ); svg_rect($xlm, $ytm, $iw-$xrm-$xlm, $ih-$ybm-$ytm, array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['background']['rgb']) ); $depth = 12; svg_group( array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['background_2']['rgb'], 'fill-opacity' => $cl['background_2']['opacity']) ); svg_poly(array($xlm, $ytm, $xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $xlm - $depth, $ytm + $depth)); svg_poly(array($xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $iw - $xrm - $depth, $ih - $ybm + $depth, $iw - $xrm, $ih - $ybm)); svg_group_end(); // draw title $text="$iface_title[$iface]($iface) 鎺ュ彛鐨勬祦閲忕ず鎰忓浘"; svg_text($iw / 2, ($ytm / 2), $text, array( 'stroke' => $cl['text'], 'stroke-width' => 0, 'font-family' => 'Simhei', 'font-weight' => 'bold', 'text-anchor' => 'middle' )); } function draw_border() { global $cl, $iw, $ih; svg_rect(1, 1, $iw-2, $ih-2, array( 'stroke' => $cl['border']['rgb'], 'stroke-opacity' => $cl['border']['opacity'], 'stroke-width' => 1, 'fill' => 'none') ); } function draw_grid($x_ticks, $y_ticks) { global $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm; $x_step = ($iw - $xlm - $xrm) / $x_ticks; $y_step = ($ih - $ytm - $ybm) / $y_ticks; $depth = 12; svg_group( array( 'stroke' => $cl['grid_stipple_1']['rgb'], 'stroke-opacity' => $cl['grid_stipple_1']['opacity'], 'stroke-width' => '1px', 'stroke-dasharray' => '1,1' ) ); for ($i = $xlm; $i <= ($iw - $xrm); $i += $x_step) { svg_line($i, $ytm, $i, $ih-$ybm); svg_line($i, $ih-$ybm, $i-$depth, $ih-$ybm+$depth); } for ($i = $ytm; $i <= ($ih - $ybm); $i += $y_step) { svg_line($xlm, $i, $iw - $xrm, $i); svg_line($xlm, $i, $xlm - $depth, $i + $depth); } svg_group_end(); svg_group( array( 'stroke' => $cl['border']['rgb'], 'stroke-width' => '1px', 'stroke-opacity' => $cl['border']['opacity'] ) ); svg_line($xlm, $ytm, $xlm, $ih - $ybm); svg_line($xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm); svg_group_end(); } 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 = '娌℃湁鍙
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -