Redfern_89

diagram.php

Sep 20th, 2022
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.91 KB | None | 0 0
  1. <?php
  2.     /*
  3.         This is application file
  4.         (c) by Redfern89
  5.        
  6.         Created date: 2022-09-17 22:57:22
  7.     */
  8.     class WO_dia {
  9.        
  10.         function imagedashedrectangle(&$img, $x, $y, $width, $height, $color) {
  11.             imagedashedline($img, $x, $y, $width, $y, $color);
  12.             imagedashedline($img, $x, $y+$height-$y, $width, $y+$height-$y, $color);
  13.             imagedashedline($img, $x, $y, $x, $y + $height - $y, $color);
  14.             imagedashedline($img, $width, $y, $width, $y + $height - $y, $color);
  15.         }
  16.        
  17.         public function begin_act() {
  18.            
  19.             $width      = (isset($_GET['w'])) ? $_GET['w'] : 0;
  20.             $width      = ($width && $width > 0) ? $width : 2000;
  21.             $height     = (isset($_GET['h'])) ? $_GET['h'] : 0;
  22.             $height     = ($height && $height > 0) ? $height : 800;
  23.            
  24.             $paddingX1  = 10;
  25.             $paddingY1  = 15;
  26.             $paddingX2  = 10;
  27.             $paddingY2  = 30;
  28.             $grid_Cnt   = 10;
  29.            
  30.             $gridFont   = 22;
  31.             $ellipseR   = 7;
  32.            
  33.             $data       = @$_GET['data'];
  34.             $data       = is_array($data) ? $data : [];
  35.             $data       = !empty($data) ? $data : [];
  36.            
  37.             $legend     = @$_GET['legend'];
  38.             $legend     = !empty($legend) ? explode(',', $legend) : [];
  39.            
  40.             $title      = @$_GET['title'];
  41.             $titleFS    = 42;
  42.             $titleMB    = 5;
  43.            
  44.             $font       = dirname(__FILE__) . '/arialmt.ttf';
  45.             $img        = imagecreatetruecolor($width, $height);
  46.            
  47.             $dataFull   = array();
  48.             $dataCounts = array();
  49.            
  50.             $dataCountMAX = 0;
  51.             $dataCountMIN = 0;
  52.            
  53.             $legendFS       = 19;
  54.             $legendRectS    = 32;
  55.             $legendFieldMT  = 10;
  56.             $legendFieldMS  = 10;
  57.             $legendFieldMB  = 10;
  58.            
  59.             $colors     = [0xFF0000, 0x0f60f6, 0x009631];
  60.            
  61.             imagefill($img, 0, 0, 0xFCFCFC);
  62.  
  63.             if (!empty($title)) {
  64.                 $titleBbox = imagettfbbox($titleFS, 0, $font, $title);
  65.                 $x = (($width / 2) - (($titleBbox[2] - $titleBbox[0]) / 2));
  66.                 $y = $paddingY1 + (($titleBbox[3] - $titleBbox[5]) / 2) + $titleBbox[3];
  67.                 imagettftext($img, $titleFS, 0, (int)$x, (int)$y, 0x000000, $font, $title);
  68.                 $paddingY1 = ($titleBbox[3] - $titleBbox[5]) + $paddingY1 + $titleMB;
  69.                
  70.             }
  71.            
  72.             if (!empty($data)) {
  73.                 for ($i = 0; $i < count($data); $i++) {
  74.                     $dataCurrent    = $data[$i];
  75.                     $dataCurrent    = explode(',', $dataCurrent);
  76.                     $dataCounts[]   = count($dataCurrent);
  77.                    
  78.                     if (!empty($dataCurrent)) {
  79.                         for ($j = 0; $j < count($dataCurrent); $j++) {
  80.                             $dataNum    = (float)$dataCurrent[$j];
  81.                             $dataFull[] = $dataNum;
  82.                         }
  83.                     }
  84.                 }
  85.                 $dataCountMAX   = max($dataCounts);
  86.                 $dataCountMIN   = min($dataCounts);
  87.                
  88.                 $max            = max($dataFull);
  89.                 $min            = min($dataFull);
  90.                 $average        = ($min + $max) / 2;
  91.                 $max_bbox       = imagettfbbox($gridFont, 0, $font, $max);
  92.                 $min_bbox       = imagettfbbox($gridFont, 0, $font, $min);
  93.                 $ave_bbox       = imagettfbbox($gridFont, 0, $font, $average);
  94.                
  95.                 $bboxes         = array();
  96.                 $bbox_max_width = max($max_bbox[2], $min_bbox[2], $ave_bbox[2]);
  97.                 $paddingX1      = $bbox_max_width + $paddingX1;
  98.  
  99.                 imagettftext($img, $gridFont, 0, (int)(($paddingX1 - $max_bbox[2]) -2), (int)(($paddingY1) - ($max_bbox[7] / 2)), 0x00, $font, $max);
  100.                 imagettftext($img, $gridFont, 0, (int)(($paddingX1 - $min_bbox[2]) -2), (int)(($height - $paddingY2) - ($min_bbox[7] / 2)), 0x00, $font, $min);
  101.                 imagettftext($img, $gridFont, 0, (int)(($paddingX1 - $ave_bbox[2]) -2), (int)((($height / 2) - (($paddingY2 - $paddingY1) / 2) - ($ave_bbox[7] / 2))), 0x00, $font, $average); 
  102.             }
  103.            
  104.             for ($i = 0; $i < $grid_Cnt; $i++) {
  105.                 $linesW = (($height - ($paddingY1 + $paddingY2)) / $grid_Cnt);
  106.                 if ($i % 2) $bg = 0xdfdfdf; else $bg = 0xf0f0f0;
  107.                 $x1 = $paddingX1;
  108.                 $x2 = $width - $paddingX2;
  109.                 $y1 = (int)(($i * $linesW)) + ($paddingY1);
  110.                 $y2 = (int)($y1 + $linesW);
  111.                
  112.                 imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bg);
  113.                 $this -> imagedashedrectangle($img, $x1, $y1, $x2, $y2 , 0x474747);
  114.             }  
  115.            
  116.            
  117.             if (!empty($data)) {
  118.                 for ($i = 0; $i < count($data); $i++) {
  119.                     $dataCurrent    = explode(',', $data[$i]);
  120.                    
  121.                     if (!empty($dataCurrent)) {
  122.                         $dataLinesArray = array();
  123.                         $percC          = count($dataCurrent) * (100 / $dataCountMAX);
  124.                         $dWidthMAX      = $width - ($paddingX1 + $paddingX2);
  125.                         $dWidth         = (int)($percC * ($dWidthMAX / 100));
  126.                         $dHeight        = $height - ($paddingY1 + $paddingY2);
  127.                         $count          = count($dataCurrent);
  128.                         $dataImg        = imagecreatetruecolor($dWidth, $dHeight);
  129.                         $col            = imagecolorallocatealpha($dataImg, 255, 255, 255, 127);
  130.                        
  131.                         imagefill($dataImg, 0, 0, $col);
  132.                         imageantialias($dataImg, true);
  133.                        
  134.                         for ($j = 0; $j < $count; $j++) {
  135.                             if ($count > 0 && $dataCurrent[$j] != '') {
  136.                                 if ($max - $min > 0) {
  137.                                     $x = (int)(($dWidth / ($count -1)) * $j);
  138.                                     $y = (int)($dHeight) - ((int)$dataCurrent[$j] - (int)$min) * ($dHeight) / ((int)$max - (int)$min);
  139.                                     $dataLinesArray[] = array($x, $y);
  140.                                 }
  141.                             }
  142.                         }
  143.                        
  144.                         for ($j = 0; $j < count($dataLinesArray); $j++) {
  145.                             $xy = $dataLinesArray[$j];
  146.                             if ($j != 0) {
  147.                                 $xy_ = $dataLinesArray[$j -1];
  148.                             } else {
  149.                                 $xy_ = $xy;
  150.                             }
  151.                             imageline($dataImg, (int)$xy_[0], (int)$xy_[1], (int)$xy[0], (int)$xy[1], $colors[$i]);
  152.                             imageline($dataImg, (int)$xy_[0]+1, (int)$xy_[1]+1, (int)$xy[0]+1, (int)$xy[1]+1, $colors[$i]);
  153.                             imagefilledellipse($dataImg, (int)$xy[0], (int)$xy[1], $ellipseR, $ellipseR, $colors[$i]);
  154.                             //imagedashedline($dataImg, (int)$xy[0], $dHeight, (int)$xy[0], (int)$xy[1], 0x474747);
  155.                         }
  156.                        
  157.                         $dataLinesArray = array();
  158.                        
  159.                         imagecopyresampled($img, $dataImg, $paddingX1, $paddingY1, 0, 0, $dWidth, $dHeight, $dWidth, $dHeight);
  160.                         imagedestroy($dataImg);
  161.                     }
  162.                 }
  163.             }
  164.             imageantialias($img, true);
  165.            
  166.             if (!empty($legend)) {
  167.                 if (true) {
  168.                     for ($i = 0; $i < count($legend); $i++) {
  169.                         $x1 = ($paddingX1) + $legendFieldMS;
  170.                         $x2 = $x1 + $legendRectS;
  171.                         $mb = ($i * $legendFieldMB);
  172.                         $y1 = ($legendRectS * $i) + ($paddingY1 + $legendFieldMT);
  173.                         $y2 = ($y1 + $legendRectS);
  174.                        
  175.                         imagefilledrectangle($img, $x1, $y1 + $mb, $x2, $y2 + $mb, $colors[$i]);
  176.                         imagerectangle($img, $x1, $y1+$mb, $x2, $y2 + $mb, 0x00);
  177.                        
  178.                         $legendBbox = imagettfbbox($legendFS, 0, $font, $legend[$i]);
  179.                        
  180.                         $x = $paddingX1 + ($legendRectS) + $legendFieldMS + 10;
  181.                         $y = ($legendRectS * $i) + ($paddingY1 + $legendFieldMT) + ($legendRectS) - 5;
  182.                        
  183.                         imagettftext($img, $legendFS, 0, $x, $y + $mb, 0x00, $font, $legend[$i]);
  184.                     }
  185.                 }
  186.             }
  187.            
  188.             imageline($img, $paddingX1, $height - $paddingY2, $width - $paddingX2, $height - $paddingY2, 0x00);
  189.             imageline($img, $paddingX1, $height - $paddingY2-1, $width - $paddingX2, $height - $paddingY2-1, 0x00);
  190.             imageline($img, $paddingX1, $height - $paddingY2, $paddingX1, $paddingY1, 0x00);
  191.             imageline($img, $paddingX1+1, $height - $paddingY2, $paddingX1+1, $paddingY1, 0x00);
  192.            
  193.             header('Content-Type: image/png');
  194.             imagepng($img);
  195.             imagedestroy($img);
  196.            
  197.         }
  198.     }
  199.    
  200. ?>
Advertisement
Add Comment
Please, Sign In to add comment