5/29/2019

PHP :: สร้างกราฟต่าง ๆ ด้วย PHP

PHP :: สร้างกราฟต่าง ๆ ด้วย PHP

ตัวอย่าง
https://intranet.sci.com/sci/AF/ConsumeChart/OT1.php?Year=2018

1. Download lib มาไว้ที่เครื่อง ที่ Run Web apache
https://www.highcharts.com/blog/download/

2. เข้าดูตัวอย่างใน examples จะมีตัวอย่างกราฟต่าง ๆ
PHP ตัวอย่าง เช่น
  1. <?php
  2. error_reporting(E_ERROR);
  3. define('IN_PHPBB', true);
  4. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../../../';
  5. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  6. include($phpbb_root_path . 'common.' . $phpEx);
  7.  
  8. include($phpbb_root_path ."sci/include/config.php");
  9. //include($phpbb_root_path ."sci/include/db.php");
  10. include($phpbb_root_path ."sci/include/functions.php");
  11. include($phpbb_root_path ."sci/include/db2.php");
  12.  
  13. $dep = array('DS', 'IF', 'OD', 'PD', 'QA', 'SS2', 'SS1:SS', 'SS1', 'PR', 'WH');
  14. $depTH = array('ฝอบ.', 'ฝทง.', 'ฝพอ.', 'ฝผล.', 'ฝปค.', 'ฝข.2', 'งสข.', 'ฝข.1', 'ฝวจ.', 'งคส.');
  15. //$SQL = "SELECT AccountNum,AccountName,SUM(AMOUNT),Department FROM sci_AxUsedWaste.Data";
  16.  
  17. $curYear = $_GET["Year"];
  18. if($curYear == ""){
  19.     $curYear = date("Y");
  20.     $yearLoop = array('2019', '2018', '2017');
  21. } else {
  22.     $yearLoop = array($curYear);
  23. }
  24.  
  25. ?>
  26. <!DOCTYPE HTML>
  27. <html>
  28.     <head>
  29.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  30.         <meta name="viewport" content="width=device-width, initial-scale=1">
  31.         <title>Highcharts Example</title>
  32.  
  33.         <style type="text/css">
  34.  
  35.         </style>
  36.     </head>
  37.     <body>
  38. <script src="../lib/code/highcharts.js"></script>
  39. <script src="../lib/code/modules/exporting.js"></script>
  40. <script src="../lib/code/modules/export-data.js"></script>
  41.  
  42. <!-- ---------------------------------------- 1 ------------------------------------------------------ --> 
  43. <div id="container" style="min-width: 310px; max-width: 1000px; margin: 0 auto"></div>
  44.  
  45.         <script type="text/javascript">
  46. Highcharts.setOptions({
  47.     lang: {
  48.       decimalPoint: '.',
  49.       thousandsSep: ','
  50.     }
  51. });
  52. var chart = new Highcharts.Chart({
  53.       chart: {
  54.          renderTo: 'container',
  55.          type: 'line'
  56.       },
  57.         title: {
  58.                 text: "กราฟแสดงจำนวน OT แยกฝ่าย"
  59.         },
  60.  
  61.         xAxis: {
  62.         categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  63.         max: 11
  64.         },
  65.  
  66.         yAxis: {
  67.         title: {
  68.             text: 'บาท'
  69.         }
  70.       },       
  71.         tooltip: {
  72.         crosshairs: true,
  73.         shared: true
  74.         },
  75.        
  76.         plotOptions: {
  77.         line: {
  78.             dataLabels: {
  79.                 enabled: true
  80.             },
  81.             marker: {
  82.                 radius: 4,
  83.                 lineColor: '#666666',
  84.                 lineWidth: 1
  85.             }
  86.         },
  87.         /*series: {
  88.             cursor: 'pointer',
  89.             point: {
  90.                 events: {
  91.                     click: function () {
  92.                         //alert('Category: ' + this.category + ', value: ' + this.y);
  93.                         location.href = 'consumeByMonth.php?acc=<?php echo $acc ?>' + '&mm=' + this.category + '&yy=' + <?php echo $curYear ?>;
  94.                     }
  95.                 }
  96.             }
  97.         }*/
  98.         },     
  99.         series: [
  100. <?php
  101.     for($i = 0; $i < 10; $i++ ){
  102.         $data = [];
  103.         $SQL = "SELECT * FROM sci_consume_chart.ot";
  104.         $SQL .= " WHERE YearMonth LIKE '$curYear-%'";
  105.         $SQL .= " AND Department LIKE '$depTH[$i]%'";
  106.         $SQL .= " ORDER BY Department";
  107.         $result = $mysqli->query($SQL);
  108. //      echo $SQL;
  109. //      exit;
  110.         while($row = $result->fetch_array()){
  111.             $data += [ $row["YearMonth"] => $row["OTCost"] ];
  112.         }
  113.        
  114.         for($n = 1; $n < date(m); $n++){
  115.             $date = sprintf($curYear."-%02s", $n);
  116.             if(!array_key_exists($date, $data)){
  117.                 $data += [ $date => 0 ];
  118.             }      
  119.         }
  120.         ksort($data);
  121. ?>         
  122.       {
  123.         name: <?php echo "'".$depTH[$i]."'" ?>,
  124.          data: [<?php echo join($data, ',') ?>]
  125.       },
  126. <?php
  127.     }
  128. ?>      
  129.       ]  
  130. });
  131.         </script>
  132.  
  133. <!-- --------------------------------------------- 2 ---------------------------------------------------- -->   
  134. <br>
  135. <br>       
  136.  
  137. <?php
  138. //############# Line 1 PD Weight ##################//
  139. $data1 = [];   
  140. $SQL = "SELECT * FROM sci_consume_chart.PD_weight";
  141. $SQL .= " WHERE YearMonth LIKE '$curYear-%'";
  142. $SQL .= " ORDER BY YearMonth";
  143. $result = $mysqli->query($SQL);
  144. while($row = $result->fetch_array()){
  145.     $data1 += [ $row["YearMonth"] => $row["Weight"] ];
  146. }
  147.  
  148. for($n = 1; $n <= 12; $n++){
  149.     $date = sprintf($curYear."-%02s", $n);
  150.     if(!array_key_exists($date, $data1)){
  151.         $data1 += [ $date => 0 ];
  152.     }      
  153. }
  154. ksort($data1);
  155. //print_r($dataColumn);    
  156.  
  157. //############# Line 2 OTHour ##################//
  158. for($i = 0; $i < 10; $i++ ){
  159.     $data2 = [];    //Reset.
  160.     $SQL = "SELECT SUM(OTHour) AS SumOTHour, YearMonth FROM sci_consume_chart.ot";
  161.     $SQL .= " WHERE YearMonth LIKE '$curYear-%'";
  162.     $SQL .= " GROUP BY YearMonth";
  163.     $SQL .= " ORDER BY Department";
  164.     $result = $mysqli->query($SQL);
  165.     while($row = $result->fetch_array()){
  166.         $data2 += [ $row["YearMonth"] => $row["SumOTHour"] ];
  167.     }
  168.    
  169.     for($n = 1; $n <= 12; $n++){
  170.         $date = sprintf($curYear."-%02s", $n);
  171.         if(!array_key_exists($date, $data2)){
  172.             $data2 += [ $date => 0 ];
  173.         }      
  174.     }
  175.     ksort($data2);
  176.    $data2 = join($data2, ',');
  177. }
  178.  
  179.  
  180. ?>
  181. <div id="container2" style="min-width: 310px; max-width: 1180px; margin: 0 auto"></div>
  182. <script type="text/javascript">
  183. var chart2 = new Highcharts.chart('container2', {
  184.     chart: {
  185.         zoomType: 'xy'
  186.     },
  187.     title: {
  188.         text: "กราฟแสดงน้ำหนักผลิต (กก.) และ OT (ชั่วโมง)"
  189.     },
  190.     subtitle: {
  191.         text: ''
  192.     },
  193.     xAxis: [{
  194.         categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  195.             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  196.         crosshair: true
  197.     }],
  198.     yAxis: [{ // Primary yAxis
  199.         labels: {
  200.             format: '{value} กิโลกรัม',
  201.             style: {
  202.                 color: Highcharts.getOptions().colors[1]
  203.             }
  204.         },
  205.         title: {
  206.             text: 'น้ำหนักผลิต',
  207.             style: {
  208.                 color: Highcharts.getOptions().colors[1]
  209.             }
  210.         },
  211.         opposite: true
  212.  
  213.     }, { // Secondary yAxis
  214.         gridLineWidth: 0,
  215.         title: {
  216.             text: 'OT',
  217.             style: {
  218.                 color: Highcharts.getOptions().colors[1]
  219.             }
  220.         },
  221.         labels: {
  222.             format: '{value} ชั่วโมง',
  223.             style: {
  224.                 color: Highcharts.getOptions().colors[1]
  225.             }
  226.         }
  227.     }],
  228.     tooltip: {
  229.         shared: true
  230.     },
  231.     legend: {
  232.           //layout: 'vertical', 
  233.         layout: 'horizontal',
  234.         align: 'left',
  235.         x: 100,
  236.         verticalAlign: 'top',
  237.         y: 20,
  238.         floating: true,
  239.         backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 'rgba(255,255,255,0.25)'
  240.     },
  241.         plotOptions: {
  242.         column: {
  243.             dataLabels: {
  244.                 enabled: true,
  245.                 color: 'red'        
  246.             }
  247.         },
  248.         line: {
  249.             dataLabels: {
  250.                 enabled: true
  251.             },
  252.             marker: {
  253.                 radius: 4,
  254.                 lineColor: '#666666',
  255.                 lineWidth: 1
  256.             }
  257.         },/*
  258.         series: {
  259.             cursor: 'pointer',
  260.             point: {
  261.                 events: {
  262.                     click: function () {
  263.                         //alert('Category: ' + this.category + ', value: ' + this.y);
  264.                         location.href = 'TopItemByMonth.php?itemid=<?php echo $itemid ?>' + '&mm=' + this.category + '&yy=' + <?php echo $prvYear ?>;
  265.                     }
  266.                 }
  267.             }
  268.         }*/
  269.         },    
  270.     series: [{
  271.         name: 'น้ำหนักผลิต',
  272.         type: 'column',
  273.         data: [<?php echo join($data1, ',') ?>],
  274.         tooltip: {
  275.             valueSuffix: ' กิโลกรัม'
  276.         }
  277.  
  278.     }, {
  279.         name: 'Sum ทุกฝ่าย',
  280.         type: 'line',
  281.         yAxis: 1,
  282.         data: [<?php echo $data2; ?>],
  283.         tooltip: {
  284.             valueSuffix: ' บาท'
  285.         }
  286.                    
  287.     }]
  288. });
  289.  
  290. </script>   
  291. <br>
  292. <br>
  293. <!-- ---------------------------------------- 3 ------------------------------------------------------ --> 
  294. <div id="container3" style="min-width: 310px; max-width: 1000px; margin: 0 auto"></div>
  295.  
  296.         <script type="text/javascript">
  297. Highcharts.setOptions({
  298.     lang: {
  299.       decimalPoint: '.',
  300.       thousandsSep: ','
  301.     }
  302. });
  303. var chart3 = new Highcharts.Chart({
  304.       chart: {
  305.          renderTo: 'container3',
  306.          type: 'line'
  307.       },
  308.         title: {
  309.                 text: "กราฟแสดงอัตราส่วน OT/น้ำหนักผลิต (ชั่วโมง/ตัน)"
  310.         },
  311.  
  312.         xAxis: {
  313.         categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  314.         max: 11
  315.         },
  316.  
  317.         yAxis: {
  318.         title: {
  319.             text: 'ชั่วโมง'
  320.         }
  321.       },       
  322.         tooltip: {
  323.         crosshairs: true,
  324.         shared: true
  325.         },
  326.        
  327.         plotOptions: {
  328.         line: {
  329.             dataLabels: {
  330.                 enabled: true
  331.             },
  332.             marker: {
  333.                 radius: 4,
  334.                 lineColor: '#666666',
  335.                 lineWidth: 1
  336.             }
  337.         },
  338.         /*series: {
  339.             cursor: 'pointer',
  340.             point: {
  341.                 events: {
  342.                     click: function () {
  343.                         //alert('Category: ' + this.category + ', value: ' + this.y);
  344.                         location.href = 'consumeByMonth.php?acc=<?php echo $acc ?>' + '&mm=' + this.category + '&yy=' + <?php echo $curYear ?>;
  345.                     }
  346.                 }
  347.             }
  348.         }*/
  349.         },     
  350.         series: [
  351. <?php
  352. if($curYear == date(Y)){
  353.     $loop = 3;
  354. } else {
  355.     $loop = 1;
  356. }   
  357.  
  358. for($i = 0; $i < $loop; $i++ ){
  359.     $data3 = [];
  360.     $SQL = "SELECT * FROM sci_consume_chart.PD_weight";
  361.     $SQL .= " WHERE YearMonth LIKE '$yearLoop[$i]-%'";
  362.     $SQL .= " ORDER BY YearMonth";
  363.     $result = $mysqli->query($SQL);
  364.     //$x = 1;
  365.     while($row = $result->fetch_array()){
  366.         //echo $x.":".$row["Weight"]."<br>";
  367.         $SQL2 = "SELECT SUM(OTHour) AS SumOTHour FROM sci_consume_chart.ot";
  368.         $SQL2 .= " WHERE YearMonth = '".$row["YearMonth"]."'";
  369.         $SQL2 .= " GROUP BY '".$row["YearMonth"]."'";
  370.         $result2 = $mysqli->query($SQL2);
  371.         //echo $SQL2."<br>";
  372.          
  373.         $row2 = $result2->fetch_array();
  374.         $ValuePrint = number_format($row2["SumOTHour"] / ($row["Weight"] / 1000), 2, ".", "");
  375.         $data3 += [ $row["YearMonth"] => $ValuePrint ];
  376.         //$x++; 
  377.     }
  378.    
  379.     if($curYear == $yearLoop[$i]){
  380.         for($n = 1; $n < date(m); $n++){
  381.             $date = sprintf($curYear."-%02s", $n);
  382.             if(!array_key_exists($date, $data3)){
  383.                 $data3 += [ $date => 0 ];
  384.             }      
  385.         }
  386.     }   
  387.     ksort($data3);
  388. ?>         
  389.       {
  390.         name: <?php echo "'".$yearLoop[$i]."'" ?>
  391.          data: [<?php echo join($data3, ',') ?>]
  392.       },
  393. <?php
  394. }
  395. ?>          
  396.       ]  
  397. });
  398.         </script>   
  399.        
  400.     </body>
  401. </html>

No comments:

Post a Comment