ホームページ バックエンド開発 PHPチュートリアル PHPはヒストグラムを描画します

PHPはヒストグラムを描画します

Jul 25, 2016 am 08:43 AM

  1. /***
  2. * @project 棒グラフ プログラム
  3. * @license GPL
  4. * @package
  5. * @file GrapBar.php
  6. * @date 2007-4-3
  7. * @version 1.0
  8. * @最終更新日
  9. * 棒グラフの定義 (縦棒グラフ) クラス
  10. *
  11. * 注意: 使用する前に、フォント パスが存在し、アクセスが許可されていることを確認してください。エラーが発生した場合は、php.ini 設定の open_basedir 項目も確認してください。そのようなパスがない場合は、追加してください。
  12. *
  13. * レポートなどのためのインテリジェントな棒グラフ プログラム
  14. *
  15. ** が含まれています。*/
  16. define("DEFAULT_FONT_PATH", "c:/windows/fonts/simhei.ttf");
  17. クラス SingleBar
  18. {
  19. private $_x;
  20. プライベート $_y;
  21. プライベート $_h;
  22. パブリック $_l = 50;
  23. プライベート $_w = null;
  24. プライベート $_srcPoints = array();
  25. プライベート $_points = array();
  26. public function __construct($x, $y, $h, $l = 50, $w = null)
  27. {
  28. $this->_x = $x;
  29. $this->_y = $y;
  30. $this->_h = $h;
  31. $this->_l = $l;
  32. $this->_w = $w;
  33. $this->_srcPoints = $this->getSrcPoints();
  34. $this->_points = $this->getPoints();
  35. }
  36. public function getSrcPoints()
  37. {
  38. return array(
  39. array($this->_x , $this->_y),
  40. array($this->>_x+$this->_l , $ this->_y),
  41. array($this->_x+(1.35*$this->_l), $this->_y-(0.35*$this->_l)),
  42. array($ this->_x+(0.35*$this->_l), $this->_y-(0.35*$this->_l)),
  43. array($this->_x , $this->>; _y+$this->_h)、
  44. array($this->_x+$this->_l 、$this->y+$this->_h)、
  45. array($this->_x+(1.35) *$this->_l), $this->_y+$this->_h-(0.35*$this->_l))
  46. );
  47. }
  48. パブリック関数 getPoints()
  49. {
  50. $points = array();
  51. foreach($this->_srcPoints as $key => $val)
  52. {
  53. $points[] = $val[0];
  54. $points[] = $val[1];
  55. }
  56. $ポイントを返します;
  57. }
  58. public function getTopPoints()
  59. {
  60. return array_slice($this->_points, 0, 8); //顶坐标
  61. }
  62. public function getBelowPoints()
  63. {
  64. return array_merge(array_slice($this->_points, 0, 2), array_slice($this->_points, 8, 4), array_slice($ this->_points, 2, 2)); //下坐标
  65. }
  66. public function getRightSidePoints()
  67. {
  68. return array_merge(array_slice($this->_points, 2, 2), array_slice($this->_points, 10, 4), array_slice($ this->_points, 4, 2)); //右侧坐标
  69. }
  70. public functiondraw($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = 'LEFT')
  71. {
  72. if (is_null($borderColor))
  73. {
  74. $borderColor = 0xcccccc; $top_rgb = $this->getRGB($topColor);
  75. $below_rgb = $this->getRGB($belowColor);
  76. $side_rgb = $this->getRGB($sideColor);
  77. $top_color = imagecolorallocate($image, $top_rgb['R'], $top_rgb['G'], $top_rgb['B']);
  78. $below_color = imagecolorallocate($image, $below_rgb['R'], $below_rgb['G'], $below_rgb['B']);
  79. $side_color = imagecolorallocate($image, $side_rgb['R'], $side_rgb['G'], $side_rgb['B']);
  80. imagefilledpolygon($image, $this->getTopPoints(), 4, $top_color); //画顶面
  81. imagepolygon($image, $this->getTopPoints(), 4, $borderColor); //画顶面边線
  82. imagefilledpolygon($image, $this->getBelowPoints(), 4, $below_color); //画下面
  83. imagepolygon($image, $this->getBelowPoints(), 4, $borderColor); //画下面边線
  84. if ($type == 'LEFT')
  85. {
  86. imagefilledpolygon($image, $this->getRightSidePoints(), 4, $side_color); //画右面
  87. imagepolygon($image, $this->getRightSidePoints(), 4, $borderColor); //画面边線
  88. }
  89. }
  90. public function getRGB($color)
  91. {
  92. $ar = array();
  93. $color = hexdec($color);
  94. $ar['R'] = ($color>>16) & 0xff;
  95. $ar['G'] = ($color>>8) & 0xff;
  96. $ar['B'] = ($color) & 0xff;
  97. $ar を返します。
  98. }
  99. }
  100. クラス Bar
  101. {
  102. プライベート $_W;
  103. プライベート $_H;
  104. プライベート $_bgColor = "ffffff";
  105. プライベート $_barHeights = array();
  106. プライベート $_barTexts = array();
  107. プライベート $_barColors = array();
  108. 公開 $_title;
  109. public $_paddingTop = 30;
  110. public $_paddingBottom = 100;
  111. public $_paddingLeft = 45;
  112. public $_paddingRight = 2;
  113. パブリック $_barL = 50;
  114. 公開 $image;
  115. public function __construct($imgW, $imgH, $barHeights, $barTexts = null, $barColors = null)
  116. {
  117. $this->_W = $imgW;
  118. $this->_H = $imgH;
  119. $this->_barHeights = $barHeights;
  120. $this->_barTexts = $barTexts;
  121. $this->_barColors = $barColors;
  122. $this->_paddingBottom = $this->resetPaddingBottom();
  123. $this->_H = $this->resetHeight();
  124. $this->image = imagecreatetruecolor($this->_W, $this->_H);
  125. }
  126. パブリック関数 ストローク()
  127. {
  128. $this->drawBg();
  129. $this->drawBars();
  130. $this->drawTitle();
  131. $this->drawLables();
  132. ob_start();
  133. //header("Content-type: image/png");
  134. //imagepng($this->image);
  135. header("Content-type: " .image_type_to_mime_type(IMAGETYPE_JPEG));
  136. imagejpeg($this->image);
  137. imagedestroy($this->image);
  138. }
  139. パブリック関数drawBg()
  140. {
  141. $img_w = $this->_W;
  142. $img_h = $this->_H;
  143. $paddingTop = $this->_paddingTop;
  144. $paddingBottom = $this->_paddingBottom;
  145. $paddingLeft = $this->_paddingLeft;
  146. $paddingRight = $this->_paddingRight;
  147. $rgb = $this->getRGB($this->_bgColor);
  148. $bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
  149. imagefilledrectangle($this->image, 0, 0, $img_w, $img_h, $bg);
  150. $side_bg = imagecolorallocatealpha($this->image, 220, 220, 220, 75);
  151. $side_bg2 = imagecolorallocate($this->image, 220, 220, 220);
  152. $border_color = imagecolorallocate($this->image, 190, 190, 190);
  153. $line_color = imagecolorallocate($this->image, 236, 236, 236);
  154. $dial_color = imagecolorallocate($this->image, 131, 131, 131);
  155. $x1 = $paddingLeft;
  156. $y1 = $paddingTop;
  157. $x2 = $img_w - $paddingRight;
  158. $y2 = $img_h - $paddingBottom;
  159. imagerectangle($this->image, $x1, $y1, $x2, $y2, $border_color);
  160. imagefilledpolygon($this->image, array($x1-5,$y1+10, $x1-5,$y2+10, $x1,$y2, $x1,$y1), 4, $side_bg) ;
  161. imagepolygon($this->image, array($x1-5,$y1+10, $x1-5,$y2+10, $x1,$y2, $x1,$y1), 4, $border_color) ;
  162. imagefilledpolygon($this->image, array($x1-5,$y2+10, $x2-5,$y2+10, $x2,$y2, $x1,$y2), 4, $side_bg) ;
  163. imagepolygon($this->image, array($x1-5,$y2+10, $x2-5,$y2+10, $x2,$y2, $x1,$y2), 4, $border_color) ;
  164. imageline($this->image, $x1, $y2, $x2, $y2, $side_bg2);
  165. $total_h = $img_h - $paddingTop - $paddingBottom;
  166. $every_h = $total_h/11;
  167. for($i=1; $i {
  168. imageline($this->image, $x1, $y1+($every_h*$i), $x2, $y1+($every_h) *$i)、$line_color); //画ネットワーク
  169. }
  170. $max_h = max($this->_barHeights);
  171. for($i=1; $i {
  172. $value = $max_h - (($max_h/10)*($i-1));
  173. $value = strval($value);
  174. $str_w = strlen($value)*5;
  175. imageline($this->image, $x1-5-3, $y1+10+($every_h*$i), $x1-3+1, $y1+10+($every_h*$i), $dial_color); //画刻度線
  176. imagestring($this->image, 3, $x1-5-3-$str_w-1, $y1+10+($every_h*$i)-5, $value, 0x000000);
  177. }
  178. }
  179. パブリック関数drawBars()
  180. {
  181. $counts = count($this->_barHeights);
  182. if (empty($this->_barColors))
  183. {
  184. $color = $this->setColor();
  185. $this->_barColors = array_slice($color, 0, $counts);
  186. //シャッフル($this->_barColors);
  187. }
  188. $every_w = ($this->_W - $this->_paddingLeft - $this->_paddingRight)/$counts; //每一段宽
  189. $barL = $every_w;
  190. $barL = ($barL > $this->_barL*1.35+6) ? $this->_barL : $barL/1.35 - 6;
  191. $max_h = max($this->_barHeights);
  192. $ruler_h = $this->_H - $this->_paddingTop - $this->_paddingBottom; //标尺总高さ
  193. $stander_h = $ruler_h - ($ruler_h/11); //标尺10等分の高さ
  194. $i = 0;
  195. foreach ($this->_barHeights as $val)
  196. {
  197. $h = ($stander_h/$max_h)*$val;
  198. $x = $this->_paddingLeft + ($every_w*$i) + (($every_w - ($barL*1.35))/2);;
  199. $y = $this->_H - $this->_paddingBottom + 10 - $h;
  200. //$t_color = $this->_barColors[$i];
  201. $b_color = $this->_barColors[$i];
  202. //$s_color = $this->_barColors[$i];
  203. $rgb = $this->getRGB($this->_barColors[$i]);
  204. $R = $rgb['R'] * 0.7;
  205. $G = $rgb['G'] * 0.7;
  206. $B = $rgb['B'] * 0.7;
  207. $c1 = $R > 0 ?デチェック($R) : '00';
  208. $c2 = $G > 0 ? dechex($G) : '00';
  209. $c3 = $B > 0 ? dechex($B) : '00';
  210. $t_color = $b_color;
  211. $s_color = $c1。 $c2 。 $c3;
  212. $SingleBar = 新しい SingleBar($x, $y, $h, $barL);
  213. $SingleBar->draw($this->image, $t_color, $b_color, $s_color);
  214. $i++;
  215. }
  216. }
  217. public functiondrawTitle()
  218. {
  219. if (empty($this->_title))
  220. {
  221. return;
  222. }
  223. $font = 5;
  224. $font_w = imagefontwidth($font);
  225. $len = strlen($this->_title);
  226. $x = ($this->_W + $this->_paddingLeft - $this->_paddingRight)/2;
  227. $x -= ($len*$font_w)/2;
  228. $y = ($this->_paddingTop - $font_w)/2 + 12;
  229. //imagestring($this->image, $font, $x, $y, $title, 0x000000);
  230. imagettftext($this->image, 12, 0, $x, $y, 0x000000, DEFAULT_FONT_PATH, $this->_title);
  231. }
  232. パブリック関数drawLables()
  233. {
  234. $x1 = $this->_paddingLeft - 5;
  235. $y1 = $this->_H - $this->_paddingBottom + 20;
  236. $x2 = $this->_W - $this->_paddingRight;
  237. $y2 = $this->_H - 10;
  238. //imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, 0xffffff);
  239. imagerectangle($this->image, $x1, $y1, $x2, $y2, 0x000000);
  240. $space = 5;
  241. $x = $x1 + 3;
  242. $y = $y1 + 3;
  243. foreach ($this->_barTexts as $key => $val)
  244. {
  245. $color = $this->_barColors[$key];
  246. $rgb = $this->getRGB($color);
  247. $bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
  248. imagefilledrectangle($this->image, $x, $y, $x+12, $y+12, $bg); //绘12*12の四角形
  249. imagerectangle($this->image, $x, $y, $x+12, $y+12, 0x000000); //绘12*12の四角形
  250. imagettftext($this->image, 12, 0, $x+12+3, $y+12, 0x000000, DEFAULT_FONT_PATH, $val);
  251. $x += 12 + $space + (strlen($val)*8) + $space;
  252. if ($x + (strlen($val)*8) >= $this->_W - $this->_paddingLeft - $this->_paddingRight)
  253. {
  254. $x = $x1 + 3;
  255. $y = $y + 12 + 3;
  256. }
  257. }
  258. }
  259. パブリック関数resetPaddingBottom()
  260. {
  261. $ruler_w = $this->_W - $this->_paddingLeft - $this->_paddingRight;
  262. $label_w = $this->getLableTotalWidth();
  263. $lines = ceil($label_w / $ruler_w);
  264. $h = 12 * $lines + (3 * ($lines + 1)) + 30;
  265. $h を返します;
  266. }
  267. パブリック関数resetHeight()
  268. {
  269. $padding_bottom = $this->resetPaddingBottom();
  270. if ($this->_H - $padding_bottom < 222)
  271. {
  272. return 222 + $padding_bottom;
  273. }
  274. return $this->_H;
  275. }
  276. public function getLableTotalWidth()
  277. {
  278. $counts = count($this->_barTexts);
  279. $space = 5;
  280. $total_len = 0;
  281. foreach ($this->_barTexts as $val)
  282. {
  283. $total_len += strlen($val); $tx_w = ($total_len * 9) + ((12 + 3 + $space) * $counts);
  284. $tx_w を返します;
  285. }
  286. public function setBg($color)
  287. {
  288. $this->_bgColor = $color;
  289. }
  290. public function setTitle($title)
  291. {
  292. $this->_title = $title;
  293. }
  294. public function setColor()
  295. {
  296. $ar = array('ff', '00', '33', '66', '99', 'cc');
  297. $color = array();
  298. for ($i=0; $i {
  299. for ($j=0; $j {
  300. for($k=0; $k {
  301. $color[] = $ar[$i] . $ar[$j] 。 $ar[$k]; $color2 = array();
  302. for ($i=1; $i {
  303. $color2[] = $color[$i];
  304. }
  305. $color2 を返します。
  306. }
  307. パブリック関数 getRGB($color)
  308. {
  309. $ar = array();
  310. $color = hexdec($color);
  311. $ar['R'] = ($color>>16) & 0xff;
  312. $ar['G'] = ($color>>8) & 0xff;
  313. $ar['B'] = ($color) & 0xff;
  314. $ar を返します。
  315. }
  316. }
  317. /***/
  318. $bar = new Bar(500, 300, array(600, 300, 30, 500, 400, 250, 350, 360), array('AAAAA', 'BBBBB') 、'CCCCC'、'DDDDD'、'EEEEEE'、'FFFFFF'、'GGGGGG'、'HHHHHHHHH'));
  319. $bar->setTitle('打造完美柱状態图!');
  320. $bar->ストローク();
  321. /***/
  322. ?>
  323. 复制代


柱状図、php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

JSON Web Tokens(JWT)とPHP APIでのユースケースを説明してください。 JSON Web Tokens(JWT)とPHP APIでのユースケースを説明してください。 Apr 05, 2025 am 12:04 AM

JWTは、JSONに基づくオープン標準であり、主にアイデンティティ認証と情報交換のために、当事者間で情報を安全に送信するために使用されます。 1。JWTは、ヘッダー、ペイロード、署名の3つの部分で構成されています。 2。JWTの実用的な原則には、JWTの生成、JWTの検証、ペイロードの解析という3つのステップが含まれます。 3. PHPでの認証にJWTを使用する場合、JWTを生成および検証でき、ユーザーの役割と許可情報を高度な使用に含めることができます。 4.一般的なエラーには、署名検証障害、トークンの有効期限、およびペイロードが大きくなります。デバッグスキルには、デバッグツールの使用とロギングが含まれます。 5.パフォーマンスの最適化とベストプラクティスには、適切な署名アルゴリズムの使用、有効期間を合理的に設定することが含まれます。

セッションのハイジャックはどのように機能し、どのようにPHPでそれを軽減できますか? セッションのハイジャックはどのように機能し、どのようにPHPでそれを軽減できますか? Apr 06, 2025 am 12:02 AM

セッションハイジャックは、次の手順で達成できます。1。セッションIDを取得します。2。セッションIDを使用します。3。セッションをアクティブに保ちます。 PHPでのセッションハイジャックを防ぐための方法には次のものが含まれます。1。セッション_regenerate_id()関数を使用して、セッションIDを再生します。2。データベースを介してストアセッションデータを3。

確固たる原則と、それらがPHP開発にどのように適用されるかを説明してください。 確固たる原則と、それらがPHP開発にどのように適用されるかを説明してください。 Apr 03, 2025 am 12:04 AM

PHP開発における固体原理の適用には、次のものが含まれます。1。単一責任原則(SRP):各クラスは1つの機能のみを担当します。 2。オープンおよびクローズ原理(OCP):変更は、変更ではなく拡張によって達成されます。 3。Lischの代替原則(LSP):サブクラスは、プログラムの精度に影響を与えることなく、基本クラスを置き換えることができます。 4。インターフェイス分離原理(ISP):依存関係や未使用の方法を避けるために、細粒インターフェイスを使用します。 5。依存関係の反転原理(DIP):高レベルのモジュールと低レベルのモジュールは抽象化に依存し、依存関係噴射を通じて実装されます。

phpstormでCLIモードをデバッグする方法は? phpstormでCLIモードをデバッグする方法は? Apr 01, 2025 pm 02:57 PM

phpstormでCLIモードをデバッグする方法は? PHPStormで開発するときは、PHPをコマンドラインインターフェイス(CLI)モードでデバッグする必要がある場合があります。

システムの再起動後にUnixSocketの権限を自動的に設定する方法は? システムの再起動後にUnixSocketの権限を自動的に設定する方法は? Mar 31, 2025 pm 11:54 PM

システムが再起動した後、UnixSocketの権限を自動的に設定する方法。システムが再起動するたびに、UnixSocketの許可を変更するために次のコマンドを実行する必要があります:sudo ...

PHPでの後期静的結合を説明します(静的::)。 PHPでの後期静的結合を説明します(静的::)。 Apr 03, 2025 am 12:04 AM

静的結合(静的::) PHPで後期静的結合(LSB)を実装し、クラスを定義するのではなく、静的コンテキストで呼び出しクラスを参照できるようにします。 1)解析プロセスは実行時に実行されます。2)継承関係のコールクラスを検索します。3)パフォーマンスオーバーヘッドをもたらす可能性があります。

PHPのCurlライブラリを使用してJSONデータを含むPOSTリクエストを送信する方法は? PHPのCurlライブラリを使用してJSONデータを含むPOSTリクエストを送信する方法は? Apr 01, 2025 pm 03:12 PM

PHP開発でPHPのCurlライブラリを使用してJSONデータを送信すると、外部APIと対話する必要があることがよくあります。一般的な方法の1つは、Curlライブラリを使用して投稿を送信することです。

See all articles