Start graph and import refactoring
[contractdashboard.git] / lib / graphs.inc.php
blob:a/lib/graphs.inc.php -> blob:b/lib/graphs.inc.php
--- a/lib/graphs.inc.php
+++ b/lib/graphs.inc.php
@@ -6,6 +6,7 @@
  
     <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.js"></script> 
         <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.pie.js"></script> 
+              <script language="javascript" type="text/javascript" src="lib/flot/jquery.flot.selection.js"></script> 
 
 ';
 $includedFlot = true;
@@ -18,6 +19,7 @@
 	 ?>
 	   <center><div id="cndist" style="width:900px;height:550px"></div></center>
 <script type="text/javascript"> 
+       var placeholder = $("#cndist");
 	 $(function () {
 
  var d1 = [];
@@ -36,21 +38,69 @@
 };
 ?>
 
-       var placeholder = $("#cndist");
-
-    var plot = $.plot(placeholder, [
+var data = [
        {
             data: d1,
+series: {
+                   lines: { show: true },
+                   points: { show: true }
+               },
             bars: { show: true }
         },
-    ],
-        {
+    ];
+    var options = 
+    {
 
             grid: { hoverable: true, clickable: true, labelMargin: 17  },
+            selection: { mode: "x" }
+    };
+
+placeholder.bind("plotselected", function (event, ranges) {
+            plot = $.plot(placeholder, data,
+                          $.extend(true, {}, options, {
+                              xaxis: { min: ranges.xaxis.from, max: ranges.xaxis.to }
+                          }));
     });
-
+    var previousPoint = null;
+    placeholder.bind("plothover", function (event, pos, item) {
+        $("#x").text(pos.x.toFixed(2));
+        $("#y").text(pos.y.toFixed(2));
+ 
+            if (item) {
+                if (previousPoint != item.dataIndex) {
+                    previousPoint = item.dataIndex;
+                    
+                    $("#tooltip").remove();
+                    var x = item.datapoint[0].toFixed(2),
+                        y = item.datapoint[1].toFixed(2);
+                    
+                    showTooltip(item.pageX, item.pageY,
+                                item.series.label + " of " + x + " = " + y);
+                }
+            }
+            else {
+                $("#tooltip").remove();
+                previousPoint = null;            
+            }
+    });
+
+    var plot = $.plot(placeholder, data,
+        options);
  });
 
+   function showTooltip(x, y, contents) {
+        $('<div id="tooltip">' + contents + '</div>').css( {
+            position: 'absolute',
+            display: 'none',
+            top: y + 5,
+            left: x + 5,
+            border: '1px solid #fdd',
+            padding: '2px',
+            'background-color': '#fee',
+            opacity: 0.80
+        }).appendTo("body").fadeIn(200);
+    }
+ 
 </script> 
 <?php
 }
@@ -296,40 +346,63 @@
 
 }
 function SuppliersGraph() {
+
+  global $conn;
+  includeFlot();
 $topX = 10;
-$query = "SELECT value, supplierName
-FROM `contractnotice` WHERE (YEAR(contractStart) >= 2009) AND childCN = 0
-GROUP BY supplierABN ORDER BY value DESC limit $topX";
-$result = mysql_query($query);
 $suppliers = Array();
 $values = Array();
-while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
+
+
+$query = 'SELECT SUM("value") as value, MAX("supplierName") as supplierName, (
+ case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID 
+FROM contractnotice
+WHERE "childCN" = 0
+GROUP BY supplierID
+ORDER BY value DESC
+LIMIT '.$topX;
+$query = $conn->prepare($query);
+  $query->execute();
+  if (!$query) {
+    databaseError($conn->errorInfo());
+  }
+  foreach ($query->fetchAll() as $row) {
   setlocale(LC_MONETARY, 'en_US');
  // $value = number_format(doubleval($row["value"]) , 2);
   $suppliers[] = ucsmart($row[1]);
   $values[] = doubleval($row["value"]);
 }
-mysql_free_result($result);
-
-$query = "SELECT sum(a.value) as val, supplierCountry from (SELECT value, supplierName, supplierCountry
-FROM `contractnotice` WHERE (YEAR(contractStart) >= 2009) AND childCN = 0
-GROUP BY supplierName ORDER BY value LIMIT 18446744073709551610 OFFSET $topX) as a group by supplierCountry order by val DESC limit 5 offset 1";
-$result = mysql_query($query);
-while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
-  $suppliers[] = "Other suppliers in ".ucsmart($row["supplierCountry"]);
+
+$query = 'SELECT sum(a.svalue) as val, suppliercountry from (SELECT sum("value") as svalue,  max("supplierCountry") as suppliercountry
+FROM contractnotice WHERE "childCN" = 0 and "supplierCountry" NOT ILIKE \'Australia\'
+GROUP BY "supplierName" ORDER BY svalue LIMIT 18446744073 OFFSET 10) as a group by suppliercountry order by val DESC limit 10 ';
+$query = $conn->prepare($query);
+  $query->execute();
+  if (!$query) {
+    databaseError($conn->errorInfo());
+  }
+
+  foreach ($query->fetchAll() as $row) {
+  $suppliers[] = "Other suppliers in ".ucsmart($row["suppliercountry"]);
   $values[] = doubleval($row[0]);
 }
 
-mysql_free_result($result);
-
-$query = "SELECT sum(a.value) as val, TRUNCATE(supplierPostcode,-2) as postcode from (SELECT value, supplierName, supplierPostcode, supplierCountry
-FROM `contractnotice` WHERE (YEAR(contractStart) >= 2009) AND childCN = 0 
-GROUP BY supplierName ORDER BY value LIMIT 18446744073709551610 OFFSET $topX) as a
-WHERE (supplierCountry LIKE 'Australia' OR supplierCountry LIKE 'AUSTRALIA') AND supplierPostcode < 10000
-group by TRUNCATE(supplierPostcode,-2)
-order by val DESC";
-$result = mysql_query($query);
-while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
+
+$query = 'SELECT sum(a.value) as val, substring(
+supplierpostcode from 0 for 2) as postcode from (SELECT sum(value) as value, max("supplierPostcode") as supplierpostcode, max("supplierCountry") as suppliercountry
+FROM contractnotice WHERE "childCN" = 0 
+GROUP BY "supplierABN" ORDER BY sum(value) LIMIT 1844674 OFFSET 10) as a
+WHERE (suppliercountry ILIKE \'Australia\')
+group by substring(
+supplierpostcode from 0 for 2)
+order by val DESC;';
+$query = $conn->prepare($query);
+  $query->execute();
+  if (!$query) {
+    databaseError($conn->errorInfo());
+  }
+
+  foreach ($query->fetchAll() as $row) {
     if ($row['postcode'][0] == 2 && $row['postcode'][1] == 6) $ACTvalue += $row[0];
     else if ($row['postcode'][0] == 2 || $row['postcode'][0] == 1) $NSWvalue += $row[0];
     else if ($row['postcode'][0] == 3 || $row['postcode'][0] == 8) $Vicvalue += $row[0];
@@ -339,7 +412,6 @@
     else if ($row['postcode'][0] == 7) $Tasvalue += $row[0];
     else if ($row['postcode'][0] == 0) $NTvalue += $row[0];
 }
-mysql_free_result($result);
 $suppliers[] = "Other suppliers in Australia - ACT";
 $values[] = doubleval($ACTvalue);
 $suppliers[] = "Other suppliers in Australia - NSW";
@@ -356,7 +428,54 @@
 $values[] = doubleval($SAvalue);
 $suppliers[] = "Other suppliers in Australia - Tasmania";
 $values[] = doubleval($Tasvalue);
-	
-}
+
 
 ?>
+<script type="text/javascript">
+$(function () {
+  // data
+  var data = [
+  <?php
+foreach ($suppliers as $key => $supplier) {
+    echo '{ label: "'.$supplier.'",  data: '.doubleval($values[$key]).'},';
+  }
+    ?>
+  ];
+  // GRAPH 7
+  $.plot($("#graph7"), data, 
+  {
+    series: {
+      pie: { 
+        show: true,
+          radius: 1,
+          tilt: 0.75,
+                label: {
+                    show: true,
+                    radius: 1,
+                    formatter: function(label, series){
+                        return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+label+': '+Math.round(series.percent)+'%</div>';
+                    },
+                    background: { 
+                        opacity: 0.5,
+                        color: '#000'
+                    }
+                  },
+        combine: {
+          color: '#999',
+          threshold: 0.012
+        }
+      }
+    },
+    legend: {
+      show: false
+    }
+  });
+});
+</script>
+    <div id="graph7" style="width:900px;height:550px"></div>
+
+<?php
+}
+
+?>
+