try to fix bubletree
[contractdashboard.git] / displayBubbletree.php
blob:a/displayBubbletree.php -> blob:b/displayBubbletree.php
  <?php
  include_once ("lib/common.inc.php");
  ?>
<!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("lib/Color.php"); include("lib/Color.php");
$color = new Lux_Color(); $color = new Lux_Color();
   
   
$unspscresult= $conn->prepare('select * from "UNSPSCcategories";'); $unspscresult= $conn->prepare('select * from "UNSPSCcategories";');
$unspscresult->execute(); $unspscresult->execute();
foreach ($unspscresult->fetchAll() as $row) { foreach ($unspscresult->fetchAll() as $row) {
$unspsc[$row['UNSPSC']] = $row['Title']; $unspsc[$row['UNSPSC']] = $row['Title'];
   
} }
$total = 0; $total = 0;
   
$catsresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 3 ) as cat $catsresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 3 ) as cat
FROM contractnotice WHERE "categoryUNSPSC" IS NOT NULL group by "categoryUNSPSC";;'); FROM contractnotice WHERE "categoryUNSPSC" IS NOT NULL group by "categoryUNSPSC";;');
$catsresult->execute(); $catsresult->execute();
foreach ($catsresult->fetchAll() as $row) { foreach ($catsresult->fetchAll() as $row) {
$cats[] = $row['cat']; $cats[] = $row['cat'];
} }
$cattwosresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 5 ) as cat $cattwosresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 5 ) as cat
FROM contractnotice FROM contractnotice
WHERE "categoryUNSPSC" IS NOT NULL WHERE "categoryUNSPSC" IS NOT NULL
GROUP BY "categoryUNSPSC" order by cat ;'); GROUP BY "categoryUNSPSC" order by cat ;');
$cattwosresult->execute(); $cattwosresult->execute();
foreach ($cattwosresult->fetchAll() as $tworow) { foreach ($cattwosresult->fetchAll() as $tworow) {
$cattwos[substr($tworow['cat'],0,2)][] = $tworow['cat']; $cattwos[substr($tworow['cat'],0,2)][] = $tworow['cat'];
} }
   
$catthreesresult = $conn->prepare('SELECT substr( "categoryUNSPSC"::text, 0, 7 ) as cat , SUM( "value" ) as value $catthreesresult = $conn->prepare('SELECT substr( "categoryUNSPSC"::text, 0, 7 ) as cat , SUM( "value" ) as value
FROM contractnotice FROM contractnotice
WHERE "childCN" is null and "categoryUNSPSC" IS NOT NULL WHERE "childCN" is null and "categoryUNSPSC" IS NOT NULL
GROUP BY cat order by cat ;'); GROUP BY cat order by cat ;');
$catthreesresult->execute(); $catthreesresult->execute();
foreach ($catthreesresult->fetchAll() as $threerow) { foreach ($catthreesresult->fetchAll() as $threerow) {
$catthrees[substr($threerow['cat'],0,4)][] = $threerow; $catthrees[substr($threerow['cat'],0,4)][] = $threerow;
} }
$nodes = Array(); $nodes = Array();
foreach ($cats as $catNum) { foreach ($cats as $catNum) {
$catColor = $color->hsv2hex(Array($catNum/10, .7, abs(($catNum*(1/10))-.5) + .5)); $catColor = $color->hsv2hex(Array($catNum/10, .7, abs(($catNum*(1/10))-.5) + .5));
$catName = substr($unspsc[$catNum . "000000"],0,18) . $catNum; $catName = substr($unspsc[$catNum . "000000"],0,18);
$subnodes = Array(); $subnodes = Array();
$catValue = 0; $catValue = 0;
foreach ($cattwos[$catNum] as $twoCatNum) { foreach ($cattwos[$catNum] as $twoCatNum) {
$subcatName = $unspsc[$twoCatNum. "0000"] .$twoCatNum; $subcatName = $unspsc[$twoCatNum. "0000"];
$subsubnodes = Array(); $subsubnodes = Array();
$subCatValue = 1; $subCatValue = 1;
$subCatColor = $color->hsv2hex(Array($catNum/10, rand(1,10)/10, abs(($catNum*(1/10))-.5) + .5)); $subCatColor = $color->hsv2hex(Array($catNum/10, rand(1,10)/10, abs(($catNum*(1/10))-.5) + .5));
foreach ($catthrees[$twoCatNum] as $threerow) { foreach ($catthrees[$twoCatNum] as $threerow) {
$subsubcatName = $unspsc[$threerow['cat'] . "00"] . $threerow['cat']; /*$subsubcatName = $unspsc[$threerow['cat'] . "00"];
$subsubnodes[] = Array( $subsubnodes[] = Array(
"label" => $subsubcatName, "label" => $subsubcatName,
"amount" => $threerow['value'], "amount" => $threerow['value'],
"color" => "#".$subCatColor "color" => "#".$subCatColor
); );*/
$subCatValue += $threerow['value']; $subCatValue += $threerow['value'];
} }
$subnodes[] = Array( /*$subnodes[] = Array(
"label" => $subcatName, "label" => $subcatName,
"amount" => $subCatValue, "amount" => $subCatValue,
"color" => "#".$subCatColor, "color" => "#".$subCatColor,
"children" => $subsubnodes "children" => $subsubnodes
); );*/
$catValue += $subCatValue; $catValue += $subCatValue;
} }
$nodes[] = Array( $nodes[] = Array(
"label" => $catName, "label" => $catName,
"amount" => $catValue, "amount" => $catValue,
"color" => "#".$catColor, "color" => "#".$catColor,
"children" => $subnodes "children" => $subnodes
); );
$total += $catValue; $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 = JSON.parse('" . str_replace(",",",",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>