📄 sparkline.php
字号:
$this->Debug("Sparkline :: DrawFill($x, $y, '$color')", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { if ($handle === false) $handle = $this->imageHandle; return imagefill($handle, $x, $this->TxGDYToSLY($y, $handle), $colorHandle); } } // function DrawFill function DrawLine($x1, $y1, $x2, $y2, $color, $thickness = 1, $handle = false) { $this->Debug("Sparkline :: DrawLine($x1, $y1, $x2, $y2, '$color', $thickness)", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { if ($handle === false) $handle = $this->imageHandle; imagesetthickness($handle, $thickness); $result = imageline($handle, $x1, $this->TxGDYToSLY($y1, $handle), $x2, $this->TxGDYToSLY($y2, $handle), $colorHandle); imagesetthickness($handle, 1); return $result; } } // function DrawLine function DrawPoint($x, $y, $color, $handle = false) { $this->Debug("Sparkline :: DrawPoint($x, $y, '$color')", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { if ($handle === false) $handle = $this->imageHandle; return imagesetpixel($handle, $x, $this->TxGDYToSLY($y, $handle), $colorHandle); } } // function DrawPoint function DrawRectangle($x1, $y1, $x2, $y2, $color, $handle = false) { $this->Debug("Sparkline :: DrawRectangle($x1, $y1, $x2, $y2 '$color')", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { if ($handle === false) $handle = $this->imageHandle; return imagerectangle($handle, $x1, $this->TxGDYToSLY($y1, $handle), $x2, $this->TxGDYToSLY($y2, $handle), $colorHandle); } } // function DrawRectangle function DrawRectangleFilled($x1, $y1, $x2, $y2, $color, $handle = false) { $this->Debug("Sparkline :: DrawRectangleFilled($x1, $y1, $x2, $y2 '$color')", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { // NB: switch y1, y2 post conversion // if ($y1 < $y2) { $yt = $y1; $y1 = $y2; $y2 = $yt; } if ($handle === false) $handle = $this->imageHandle; return imagefilledrectangle($handle, $x1, $this->TxGDYToSLY($y1, $handle), $x2, $this->TxGDYToSLY($y2, $handle), $colorHandle); } } // function DrawRectangleFilled function DrawCircleFilled($x, $y, $diameter, $color, $handle = false) { $this->Debug("Sparkline :: DrawCircleFilled($x, $y, $diameter, '$color')", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { if ($handle === false) $handle = $this->imageHandle; return imagefilledellipse($handle, $x, $this->TxGDYToSLY($y, $handle), $diameter, $diameter, $colorHandle); } } // function DrawCircleFilled function DrawText($string, $x, $y, $color, $font = FONT_1, $handle = false) { $this->Debug("Sparkline :: DrawText('$string', $x, $y, '$color', $font)", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { // adjust for font height so x,y corresponds to bottom left of font // if ($handle === false) $handle = $this->imageHandle; return imagestring($handle, $font, $x, $this->TxGDYToSLY($y + imagefontheight($font), $handle), $string, $colorHandle); } } // function DrawText function DrawTextRelative($string, $x, $y, $color, $position, $padding = 2, $font = FONT_1, $handle = false) { $this->Debug("Sparkline :: DrawTextRelative('$string', $x, $y, '$color', $position, $font, $padding)", DEBUG_DRAW); if (!$this->IsError() && $colorHandle = $this->GetColorHandle($color)) { if ($handle === false) $handle = $this->imageHandle; // rendered text width, height // $textHeight = imagefontheight($font); $textWidth = imagefontwidth($font) * strlen($string); // set (pxX, pxY) based on position and point // switch($position) { case TEXT_TOP: $x = $x - round($textWidth / 2); $y = $y + $padding; break; case TEXT_RIGHT: $x = $x + $padding; $y = $y - round($textHeight / 2); break; case TEXT_BOTTOM: $x = $x - round($textWidth / 2); $y = $y - $padding - $textHeight; break; case TEXT_LEFT: default: $x = $x - $padding - $textWidth; $y = $y - round($textHeight / 2); break; } // truncate bounds based on string size in pixels, image bounds // order: TRBL // $y = min($y, $this->GetImageHeight() - $textHeight); $x = min($x, $this->GetImageWidth() - $textWidth); $y = max($y, 0); $x = max($x, 0); return $this->DrawText($string, $x, $y, $color, $font, $handle); } } // function DrawTextRelative function DrawImageCopyResampled($dhandle, $shandle, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh) { $this->Debug("Sparkline :: DrawImageCopyResampled($dhhandle, $shandle, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh)", DEBUG_DRAW); if (!$this->IsError()) { return imagecopyresampled($dhandle, // dest handle $shandle, // src handle $dx, $dy, // dest x, y $sx, $sy, // src x, y $dw, $dh, // dest w, h $sw, $sh); // src w, h } } // function DrawImageCopyResampled //////////////////////////////////////////////////////////////////////////// // coordinate system functions // world coordinates are referenced as points or pt // graph coordinates are referenced as pixels or px // sparkline inverts GD Y pixel coordinates; the bottom left of the // image rendering area is px(0,0) // all coordinate transformation functions are prefixed with Tx // all coordinate transformation functions depend on a valid image handle // and will only return valid results after all Set* calls are performed // function TxGDYToSLY($gdY, $handle) { return imagesy($handle) - 1 - $gdY; } // function TxGDYToSLY function TxPxToPt($pxX, $pxY, $handle) { // TODO; must occur after data series conversion } // function TxPxToPt function TxPtToPx($ptX, $ptY, $handle) { // TODO; must occur after data series conversion } // function TxPtToPx function GetGraphWidth() { return $this->graphAreaPx[1][0] - $this->graphAreaPx[0][0]; } // function GetGraphWidth function GetGraphHeight() { return $this->graphAreaPx[1][1] - $this->graphAreaPx[0][1]; } // function GetGraphHeight function GetImageWidth() { return $this->imageX; } // function GetImageWidth function GetImageHeight() { return $this->imageY; } // function GetImageHeight //////////////////////////////////////////////////////////////////////////// // image output // function Output($file = '') { $this->Debug("Sparkline :: Output($file)", DEBUG_CALLS); if ($this->IsError()) { $colorError = imagecolorallocate($this->imageHandle, 0xFF, 0x00, 0x00); imagestring($this->imageHandle, 1, ($this->imageX / 2) - (5 * imagefontwidth(1) / 2), ($this->imageY / 2) - (imagefontheight(1) / 2), "ERROR", $colorError); } if ($file == '') { header('Content-type: image/png'); imagepng($this->imageHandle); } else { imagepng($this->imageHandle, $file); } $this->Debug('Sparkline :: Output - total execution time: ' . round($this->microTimer() - $this->startTime, 4) . ' seconds', DEBUG_STATS); } // function Output function OutputToFile($file) { $this->Output($file); } // function OutputToFile} // class Sparkline?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -