วันพุธที่ 16 พฤษภาคม พ.ศ. 2555

Fusion Chart แสดงข้อมูลกราฟจากฐานข้อมูล




<?php require_once('Connections/rs_Inflowhour.php');
    include("../fusioncharts/Charts/FusionCharts.php");
// header('content-type:text/xml');
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;  
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_rs_Inflowhour, $rs_Inflowhour);
$query_rsChart = "SELECT   plantreal.r_date,  format(plantreal.fore, 2) AS forebay,  rulecurve.upper,  rulecurve.lower
FROM
  plantreal
  LEFT JOIN rulecurve ON (concat(right(plantreal.r_date,2),'/',mid(plantreal.r_date,6,2)) = rulecurve.r_date)
WHERE
  month(plantreal.r_date) BETWEEN '01' AND '12' AND   year(plantreal.r_date) = year(curdate()) AND   plantreal.r_time = '24:00'
ORDER BY  plantreal.r_date";
$rsChart = mysql_query($query_rsChart, $rs_Inflowhour) or die(mysql_error());
$row_rsChart = mysql_fetch_assoc($rsChart);
$totalRows_rsChart = mysql_num_rows($rsChart);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Untitled Document</title>
<SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/Charts/FusionCharts.js"></SCRIPT>
<style type="text/css">
<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>
</HEAD>
<BODY>

<CENTER>
  <?php do {
$dt[] = $row_rsChart['r_date'];
$dt1[] = $row_rsChart['forebay'];
$dt2[] = $row_rsChart['upper'];
$dt3[] = $row_rsChart['lower'];
} while ($row_rsChart = mysql_fetch_assoc($rsChart));
$data1 = array($dt);
$data2 = array($dt1);
$data3 = array($dt2);  
$data4 = array($dt3);    
for($i=0;$i<$totalRows_rsChart;$i++)
  {
$datas[$i][1] = $dt[$i] ;
$datas[$i][2] = $dt1[$i] ;
$datas[$i][3] = $dt2[$i] ;
$datas[$i][4] = $dt3[$i] ; }
   ?>

<?php
//$strXML = "chart caption='Monthly Sales Summary' subcaption='For the year 2006' xAxisName='Month' yAxisName='Sales' yAxisMinValue='15000'  numberPrefix='$' showValues='0' alternateHGridColor='FCB541' alternateHGridAlpha='20' divLineColor='FCB541' divLineAlpha='50' canvasBorderColor='666666' baseFontColor='666666' lineColor='FCB541'>";

     // $strXML = "<chart caption='Sales by Product' numberPrefix='' formatNumberScale='1' rotateValues='1' placeValuesInside='1' decimals='0' >";
$strXML = "<chart palette='2' caption='ข้อมูลระดับน้ำเทียบกับ Upper - Lower Rulecurve' subCaption='ประจำเดือน' showValues='0' divLineDecimalPrecision='1' yAxisName='M.MSL.'  limitsDecimalPrecision='1' PYAxisName='Amount' SYAxisName='Quantity' numberPrefix='' formatNumberScale='0' areaOverColumns='0' legendIconScale='2' setAdaptiveYMin='1' >";
           //Initialize <categories> element - necessary to generate a multi-series chart
           $strCategories = "<categories>";
          //Initiate <dataset> elements
           $strDataCurr = "<dataset seriesName='ปริมาณน้ำ' color='33CC33' anchorBorderColor='000000'  renderAs='Area'>";
           $strDataCurr1 = "<dataset seriesName='Upper Rulecurve' color='0000FF' anchorBorderColor='330099' renderAs='Line'>";
           $strDataCurr2 = "<dataset seriesName='Lower Rulecurve' color='FF0000' anchorBorderColor='CC0000' renderAs='Line'>";
           //Iterate through the data
           //Append <category label='...' /> to strCategories
   foreach ($datas as $arSubData) {
           $strCategories .= "<category label='" . $arSubData[1] . "' />";
           //Add <set value='...' /> to both the datasets
           $strDataCurr .= "<set value='" . $arSubData[2] . "' />";
 $strDataCurr1 .= "<set value='" . $arSubData[3] . "' />";
          $strDataCurr2 .= "<set value='" . $arSubData[4] . "' />";
           //Close <categories> element }
            }    
      $strCategories .= "</categories>";
           //Close <dataset> elements
           $strDataCurr .= "</dataset>";
  $strDataCurr1 .= "</dataset>";
    $strDataCurr2 .= "</dataset>";
           //Assemble the entire XML now
           $strXML .= $strCategories . $strDataCurr . $strDataCurr1  . $strDataCurr2  . "</chart>";
  //echo $strXML;
   //Create the chart - Column 2D Chart with data from strXML
   echo renderChart("../FusionCharts/Charts/MSCombi2D.swf", "", $strXML, "FactoryDetailed", 800, 550, false, false);

mysql_free_result($rsChart);
?>

การเชื่อมตาราง สองตารางเข้าด้วยกัน JOIN MYSQL

การเชื่อมระหว่าง Table 1 และ Table 2 ซึ่งมี Field ที่ไม่เหมือนกันจึงจำเป็นต้องกำหนดให้ ข้อมูลตรงกันโดยคำสั่ง mysql CONCAT

Table 1

SELECT
  rulecurve.r_date,
  rulecurve.upper,
  rulecurve.lower
FROM
  rulecurve
 WHERE
 right(rulecurve.r_date,2) = '01'
ORDER BY
    right(rulecurve.r_date,2) ASC,left(rulecurve.r_date,2) ASC

show data

r_date                      upper                             lower
01/01 161.94 148.87
02/01 161.87 148.82
03/01 161.81 148.78
04/01 161.75 148.73
05/01 161.69 148.69
06/01 161.62 148.65
07/01 161.56 148.6
08/01 161.5 148.56
09/01 161.43 148.52
10/01 161.37 148.47
11/01 161.31 148.43
12/01 161.25 148.38
13/01 161.18 148.34
14/01 161.12 148.3
15/01 161.06 148.25
16/01 160.99 148.21
17/01 160.93 148.16
18/01 160.87 148.12
19/01 160.81 148.08
20/01 160.74 148.03
21/01 160.68 147.99
22/01 160.62 147.94
23/01 160.55 147.9
24/01 160.49 147.86
25/01 160.43 147.81
26/01 160.37 147.77
27/01 160.3 147.73
28/01 160.24 147.68
29/01 160.18 147.64
30/01 160.11 147.59
31/01 160.05 147.55

Table 2

SELECT plantreal.r_date,
       format(plantreal.fore, 2) as forebay,
       format(plantreal.`infl24`,2) as inflow
FROM plantreal
WHERE month (plantreal.r_date) = '01' and
      year (plantreal.r_date) = year (curdate()) and
      plantreal.r_time = '24:00'
ORDER BY plantreal.r_date ASC

show data
r_date forebay inflow
1/1/2012 158.96 8.59
2/1/2012 158.85 9.82
3/1/2012 158.73 7.3
4/1/2012 158.6 8.87
5/1/2012 158.45 11.17
6/1/2012 158.28 9.28
7/1/2012 158.12 8.16
8/1/2012 157.99 9.09
9/1/2012 157.88 9.14
10/1/2012 157.75 8.24
11/1/2012 157.62 8.31
12/1/2012 157.48 5.82
13/1/2012 157.34 6.25
14/1/2012 157.21 8.46
15/1/2012 157.09 6.94
16/1/2012 156.98 9.37
17/1/2012 156.84 4.13
18/1/2012 156.71 6.75
19/1/2012 156.59 9.19
20/1/2012 156.46 6.96
21/1/2012 156.33 6.77
22/1/2012 156.22 9.58
23/1/2012 156.1 7.3
24/1/2012 155.97 8.9
25/1/2012 155.81 1.86
26/1/2012 155.61 0
27/1/2012 155.42 0.07
28/1/2012 155.23 1.4
29/1/2012 155.06 7
30/1/2012 154.88 4.21
31/1/2012 154.7 6.8


การเชื่อมระหว่าง Table 1 และ Table 2 ซึ่งมี Field ที่ไม่เหมือนกันจึงจำเป็นต้องกำหนดให้ ข้อมูลตรงกันโดยคำสั่ง mysql CONCAT = 
concat(right(plantreal.r_date,2),"/",mid(plantreal.r_date,6,2)
ผลลัพธ์  2012-01-11 เป็น 11/01
จากนั้นทำการ Join Table  โดยเอา Table 2 เป็น หลัก
LEFT JOIN rulecurve ON (concat(right(plantreal.r_date,2),"/",mid(plantreal.r_date,6,2)) = rulecurve.r_date)
ผลลัพธ์  11/01 = 11/01

Table 3

SELECT 
  plantreal.r_date,
  format(plantreal.fore, 2) AS forebay,
   rulecurve.r_date as ru_date,
  rulecurve.upper,
  rulecurve.lower
FROM
  plantreal 
  LEFT JOIN rulecurve ON (concat(right(plantreal.r_date,2),"/",mid(plantreal.r_date,6,2)) = rulecurve.r_date)
WHERE
  month(plantreal.r_date) = '05' AND 
  year(plantreal.r_date) = year(curdate()) AND 
  plantreal.r_time = '24:00'
ORDER BY
  plantreal.r_date


show data

r_date forebay ru_date upper lower
1/5/2012 141.56 01/05 152.41 137.62
2/5/2012 141.46 02/05 152.32 137.58
3/5/2012 141.35 03/05 152.23 137.53
4/5/2012 141.22 04/05 152.14 137.49
5/5/2012 141.09 05/05 152.05 137.45
6/5/2012 141.04 06/05 151.96 137.41
7/5/2012 141.03 07/05 151.86 137.37
8/5/2012 140.99 08/05 151.77 137.33
9/5/2012 140.98 09/05 151.68 137.29
10/5/2012 140.96 10/05 151.59 137.25
11/5/2012 140.9 11/05 151.5 137.2
12/5/2012 140.82 12/05 151.41 137.16
13/5/2012 140.73 13/05 151.32 137.12
14/5/2012 140.66 14/05 151.23 137.08
15/5/2012 140.57 15/05 151.14 137.04