Much more optimized bubble tree
Much more optimized bubble tree

<?php <?php
include_once ("../lib/common.inc.php"); include_once ("../lib/common.inc.php");
$query = 'SELECT c."CNID",c."parentCN",c."amendmentReason",p."childCN" FROM contractnotice as c LEFT OUTER JOIN contractnotice as p on c."parentCN" = p."CNID" $query = 'SELECT c."CNID",c."parentCN",c."amendmentReason",p."childCN" FROM contractnotice as c LEFT OUTER JOIN contractnotice as p on c."parentCN" = p."CNID"
WHERE WHERE
c."parentCN" > 0 AND p."childCN" IS NULL '; c."parentCN" > 0 AND p."childCN" IS NULL ';
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
} }
foreach ($query->fetchAll() as $row) { foreach ($query->fetchAll() as $row) {
$conn->exec('UPDATE contractnotice SET "childCN" = \'' . $conn->exec('UPDATE contractnotice SET "childCN" = \'' .
$row['CNID'] . '\', "amendmentReason" = \'' . $row['CNID'] . '\', "amendmentReason" = \'' .
$row['amendmentReason'] . '\' where "CNID" = \'' . $row['amendmentReason'] . '\' where "CNID" = \'' .
$row['parentCN'] . '\';'); $row['parentCN'] . '\';');
//echo 'UPDATE contractnotice SET "childCN" = \'' . //echo 'UPDATE contractnotice SET "childCN" = \'' .
$row['CNID'] . '\', "amendmentReason" = \'' . $row['CNID'] . '\', "amendmentReason" = \'' .
$row['amendmentReason'] . '\' where "CNID" = \'' . $row['amendmentReason'] . '\' where "CNID" = \'' .
$row['parentCN'] . '\';'; $row['parentCN'] . '\';';
$errors = $conn->errorInfo(); $errors = $conn->errorInfo();
if ($errors[1] == 7 || $errors[1] ==0) if ($errors[1] == 7 || $errors[1] ==0)
echo $row['CNID'] . " linked to parent " . $row['parentCN'] . echo $row['CNID'] . " linked to parent " . $row['parentCN'] .
"<br>\n"; "<br>\n";
else print_r($errors); else print_r($errors);
   
} }
// also need to eliminate CN 100528/100529 - check for double parent CNs with no childCN, latest sequent CN id wins childCN = 0 // also need to eliminate CN 100528/100529 - check for double parent CNs with no childCN, latest sequent CN id keeps childCN = 0
  $query = 'SELECT "parentCN", array_agg("CNID"), count(*) from contractnotice WHERE "parentCN" IN
  (
  SELECT "parentCN"
  FROM contractnotice
  GROUP BY "parentCN"
  HAVING COUNT(*) > 1
  AND "parentCN" != 0
  )
  AND "childCN" = 0
  GROUP BY "parentCN" having count(*) > 1';
?> ?>
   
   
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
<title>Minimal BubbleTree Demo</title> <title>Minimal BubbleTree Demo</title>
<script type="text/javascript" src="lib/bubbletree/lib/jquery-1.5.2.min.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/jquery.history.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/jquery.history.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/raphael.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/raphael.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/vis4.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/vis4.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/Tween.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/Tween.js"></script>
<script type="text/javascript" src="lib/bubbletree/build/bubbletree.js"></script> <script type="text/javascript" src="lib/bubbletree/build/bubbletree.js"></script>
<link rel="stylesheet" type="text/css" href="lib/bubbletree/build/bubbletree.css" /> <link rel="stylesheet" type="text/css" href="lib/bubbletree/build/bubbletree.css" />
<script type="text/javascript" src="lib/bubbletree/styles/cofog.js"></script> <script type="text/javascript" src="lib/bubbletree/styles/cofog.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
<?php <?php
include_once ("lib/common.inc.php"); include_once ("lib/common.inc.php");
   
include("lib/Color.php"); include("lib/Color.php");
$color = new Lux_Color(); $color = new Lux_Color();
   
   
$unspscresult = mysql_query("select * from UNSPSCcategories;"); $unspscresult= $conn->prepare('select * from "UNSPSCcategories";');
while ($row = mysql_fetch_assoc($unspscresult)) { $unspscresult->execute();
$unspsc[$row['UNSPSC']] = $row['Title']; foreach ($unspscresult->fetchAll() as $row) {
  $unspsc[$row['UNSPSC']] = $row['Title'];
   
} }
$total = 0; $total = 0;
$cats = 0;  
$catsresult = mysql_query("SELECT LEFT( categoryUNSPSC, 1 ) as cat , $catsresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 2 ) as cat
SUM( value ) as value FROM contractnotice WHERE "categoryUNSPSC" IS NOT NULL group by "categoryUNSPSC";;');
FROM `contractnotice` $catsresult->execute();
WHERE childCN = 0 foreach ($catsresult->fetchAll() as $row) {
GROUP BY cat ;"); $cats[] = $row['cat'];
  }
  $cattwosresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 3 ) as cat
  FROM contractnotice
  WHERE "categoryUNSPSC" IS NOT NULL
  GROUP BY "categoryUNSPSC" order by cat ;');
  $cattwosresult->execute();
  foreach ($cattwosresult->fetchAll() as $tworow) {
  $cattwos[substr($tworow['cat'],0,1)][] = $tworow['cat'];
  }
   
   
   
  $catthreesresult = $conn->prepare('SELECT substr( "categoryUNSPSC"::text, 0, 4 ) as cat , SUM( "value" ) as value
  FROM contractnotice
  WHERE "childCN" = 0 and "categoryUNSPSC" IS NOT NULL
  GROUP BY cat order by cat ;');
  $catthreesresult->execute();
  foreach ($catthreesresult->fetchAll() as $threerow) {
  $catthrees[substr($threerow['cat'],0,2)][] = $threerow;
  }
$nodes = Array(); $nodes = Array();
while ($row = mysql_fetch_assoc($catsresult)) { foreach ($cats as $catNum) {
$cats++; $catColor = $color->hsl2hex(Array($catNum/10, .7, .5));
$catColor = $color->hsl2hex(Array($cats/10, .7, .5)); $catName = $unspsc[$catNum . "0000000"] . $catNum;
$catName = $unspsc[$row['cat'] . "0000000"] . $row['cat'];  
if ($row['cat'] == "") $catName = "null";  
$subnodes = Array(); $subnodes = Array();
$cattwosresult = mysql_query("SELECT LEFT( categoryUNSPSC, 2 ) as cat , $catValue = 0;
SUM( value ) as value foreach ($cattwos[$catNum] as $twoCatNum) {
FROM `contractnotice` $subcatName = $unspsc[$twoCatNum. "000000"] .$twoCatNum;
WHERE childCN = 0 and LEFT( categoryUNSPSC, 1 ) = '{$row['cat']}'  
GROUP BY cat ;");  
while ($tworow = mysql_fetch_assoc($cattwosresult)) {  
$subcatName = $unspsc[$tworow['cat'] . "000000"] . $tworow['cat'];  
if ($tworow['cat'] == "") $subcatName = "null";  
$subsubnodes = Array(); $subsubnodes = Array();
$catthreesresult = mysql_query("SELECT LEFT( categoryUNSPSC, 3 ) as cat , $subCatValue = 0;
SUM( value ) as value $subCatColor = $color->hsl2hex(Array($catNum/10, rand(1,10)/10, .5));
FROM `contractnotice` foreach ($catthrees[$twoCatNum] as $threerow) {
WHERE childCN = 0 and LEFT( categoryUNSPSC, 2 ) = '{$tworow['cat']}'  
GROUP BY cat ;");  
$subCatColor = $color->hsl2hex(Array($cats/10, rand(1,10)/10, .5));  
while ($threerow = mysql_fetch_assoc($catthreesresult)) {  
$subsubcatName = $unspsc[$threerow['cat'] . "00000"] . $threerow['cat']; $subsubcatName = $unspsc[$threerow['cat'] . "00000"] . $threerow['cat'];
if ($threerow['cat'] == "") $subsubcatName = "null";  
$subsubnodes[] = Array( $subsubnodes[] = Array(
"label" => $subsubcatName, "label" => $subsubcatName,
"amount" => $threerow['value'], "amount" => $threerow['value'],
"color" => "#".$subCatColor "color" => "#".$subCatColor
); );
  $subCatValue += $threerow['value'];
} }
$subnodes[] = Array( $subnodes[] = Array(
"label" => $subcatName, "label" => $subcatName,
"amount" => $tworow['value'], "amount" => $subCatValue,
"color" => "#".$subCatColor, "color" => "#".$subCatColor,
"children" => $subsubnodes "children" => $subsubnodes
); );
  $catValue += $subCatValue;
} }
$nodes[] = Array( $nodes[] = Array(
"label" => $catName, "label" => $catName,
"amount" => $row['value'], "amount" => $catValue,
"color" => "#".$catColor, "color" => "#".$catColor,
"children" => $subnodes "children" => $subnodes
); );
$total+= $row['value']; $total += $catValue;
} }
$data = Array( $data = Array(
"label" => "Australian Federal Government Contract Spending", "label" => "Australian Federal Government Contract Spending",
"amount" => $total, "amount" => $total,
"color" => "#000000", "color" => "#000000",
"children" => $nodes "children" => $nodes
); );
echo "var data =eval('('+'" . json_encode($data) . "'+')');"; echo "var data =eval('('+'" . json_encode($data) . "'+')');";
?> ?>
   
new BubbleTree({ new BubbleTree({
data: data, data: data,
container: '.bubbletree' container: '.bubbletree'
}); });
}); });
</script> </script>
</head> </head>
<body> <body>
<div class="bubbletree-wrapper"> <div class="bubbletree-wrapper">
<div class="bubbletree"></div> <div class="bubbletree"></div>
</div> </div>
</body> </body>
</html> </html>