graph/ranking fixes
graph/ranking fixes


Former-commit-id: c08fb5bb63762d6d850ae16d8fc7ad947b845078

file:a/graph.php -> file:b/graph.php
<?php <?php
include_once('include/common.inc.php'); include_once('include/common.inc.php');
//include_header(); //include_header();
$format = "html"; $format = "html";
if (isset($_REQUEST['format'])) { if (isset($_REQUEST['format'])) {
$format = $_REQUEST['format']; $format = $_REQUEST['format'];
} }
   
function add_node($id, $label, $parent="") { function add_node($id, $label, $parent="") {
global $format; global $format;
if ($format == "html") { if ($format == "html") {
// echo "nodes[\"$id\"] = graph.newNode({label: \"$label\"});" . PHP_EOL; // echo "nodes[\"$id\"] = graph.newNode({label: \"$label\"});" . PHP_EOL;
} }
if ($format == "dot" && $label != "") { if ($format == "dot" && $label != "") {
echo "$id [label=\"$label\"];". PHP_EOL; echo "\"$id\" [label=\"$label\", shape=plaintext];". PHP_EOL;
} }
if ($format == "gexf") { if ($format == "gexf") {
echo "<node id='$id' label=\"".htmlentities($label,ENT_XML1)."\" ".($parent != ""? "pid='$parent'><viz:size value='1'/>":"><viz:size value='2'/>") echo "<node id='$id' label=\"".htmlentities($label)."\" ".($parent != ""? "pid='$parent'><viz:size value='1'/>":"><viz:size value='2'/>")
."<viz:color b='".rand(0,255)."' g='".rand(0,255)."' r='".rand(0,255)."'/>" ."<viz:color b='".rand(0,255)."' g='".rand(0,255)."' r='".rand(0,255)."'/>"
."</node>". PHP_EOL; ."</node>". PHP_EOL;
} }
} }
   
function add_edge($from, $to, $color) { function add_edge($from, $to, $color) {
global $format; global $format;
if ($format == "html") { if ($format == "html") {
// echo "graph.newEdge(nodes[\"$from\"], nodes['$to'], {color: '$color'});" . PHP_EOL; // echo "graph.newEdge(nodes[\"$from\"], nodes['$to'], {color: '$color'});" . PHP_EOL;
} }
if ($format == "dot") { if ($format == "dot") {
echo "$from -> $to ".($color != ""? "[color=$color]":"").";". PHP_EOL; echo "\"$from\" -> \"$to\" ".($color != ""? "[color=$color]":"").";". PHP_EOL;
} }
if ($format == "gexf") { if ($format == "gexf") {
echo "<edge id='$from$to' source='$from' target='$to' />". PHP_EOL; echo "<edge id='$from$to' source='$from' target='$to' />". PHP_EOL;
} }
} }
if ($format == "gexf") { if ($format == "gexf") {
//header('Content-Type: text/xml'); //header('Content-Type: text/xml');
header('Content-Type: application/gexf+xml'); header('Content-Type: application/gexf+xml');
echo '<?xml version="1.0" encoding="UTF-8"?> echo '<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:viz="http://www.gexf.net/1.2draft/viz" version="1.2"> <gexf xmlns="http://www.gexf.net/1.2draft" xmlns:viz="http://www.gexf.net/1.2draft/viz" version="1.2">
<meta lastmodifieddate="2009-03-20"> <meta lastmodifieddate="2009-03-20">
<creator>Gexf.net</creator> <creator>Gexf.net</creator>
<description>A hello world! file</description> <description>A hello world! file</description>
</meta> </meta>
<graph mode="static" defaultedgetype="directed"> <graph mode="static" defaultedgetype="directed">
<nodes>'. PHP_EOL; <nodes>'. PHP_EOL;
} }
   
if ($format == "dot") { if ($format == "dot") {
echo 'digraph g {'. PHP_EOL; echo 'digraph g {'. PHP_EOL;
} }
$db = $server->get_db('disclosr-agencies'); $db = $server->get_db('disclosr-agencies');
add_node("fedg","Federal Government - Commonwealth of Australia"); add_node("fedg","Federal Government - Commonwealth of Australia");
try { try {
$rows = $db->get_view("app", "byCanonicalName", null, true)->rows; $rows = $db->get_view("app", "byCanonicalName", null, true)->rows;
//print_r($rows); //print_r($rows);
foreach ($rows as $row) { foreach ($rows as $row) {
add_node($row->id, $row->key); add_node($row->id, $row->value->name);
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
if ($format == "gexf") { if ($format == "gexf") {
echo '</nodes> echo '</nodes>
<edges>'. PHP_EOL; <edges>'. PHP_EOL;
} }
try { try {
$rows = $db->get_view("app", "byDeptStateName", null, true)->rows; $rows = $db->get_view("app", "byDeptStateName", null, true)->rows;
//print_r($rows); //print_r($rows);
foreach ($rows as $row) { foreach ($rows as $row) {
add_edge("fedg", $row->value, 'yellow'); add_edge("fedg", $row->value, 'yellow');
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
   
try { try {
$rows = $db->get_view("app", "parentOrgs", null, true)->rows; $rows = $db->get_view("app", "parentOrgs", null, true)->rows;
// print_r($rows); // print_r($rows);
foreach ($rows as $row) { foreach ($rows as $row) {
add_edge($row->key, $row->value, 'blue'); add_edge($row->key, $row->value, 'blue');
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
if ($format == "html") { if ($format == "html") {
?> ?>
<div id="sigma-example" width="960" style="min-height:800px;background-color: #333;"></div> <div id="sigma-example" width="960" style="min-height:800px;background-color: #333;"></div>
<script src="js/sigma.min.js"></script> <script src="js/sigma.min.js"></script>
<script src="js/sigma/plugins/sigma.parseGexf.js"></script> <script src="js/sigma/plugins/sigma.parseGexf.js"></script>
<script src="js/sigma/plugins/sigma.forceatlas2.js"></script> <script src="js/sigma/plugins/sigma.forceatlas2.js"></script>
<script type="text/javascript">function init() { <script type="text/javascript">function init() {
// Instanciate sigma.js and customize rendering : // Instanciate sigma.js and customize rendering :
var sigInst = sigma.init(document.getElementById('sigma-example')).drawingProperties({ var sigInst = sigma.init(document.getElementById('sigma-example')).drawingProperties({
defaultLabelColor: '#fff', defaultLabelColor: '#fff',
defaultLabelSize: 14, defaultLabelSize: 14,
defaultLabelBGColor: '#fff', defaultLabelBGColor: '#fff',
defaultLabelHoverColor: '#000', defaultLabelHoverColor: '#000',
labelThreshold: 6, labelThreshold: 6,
defaultEdgeType: 'curve' defaultEdgeType: 'curve'
}).graphProperties({ }).graphProperties({
minNodeSize: 0.5, minNodeSize: 0.5,
maxNodeSize: 5, maxNodeSize: 5,
minEdgeSize: 5, minEdgeSize: 5,
maxEdgeSize: 5 maxEdgeSize: 5
}).mouseProperties({ }).mouseProperties({
maxRatio: 32 maxRatio: 32
}); });
   
// Parse a GEXF encoded file to fill the graph // Parse a GEXF encoded file to fill the graph
// (requires "sigma.parseGexf.js" to be included) // (requires "sigma.parseGexf.js" to be included)
sigInst.parseGexf('graph.php?format=gexf'); sigInst.parseGexf('graph.php?format=gexf');
sigInst.bind('downnodes',function(event){ sigInst.bind('downnodes',function(event){
var nodes = event.content; var nodes = event.content;
}); });
// Start the ForceAtlas2 algorithm // Start the ForceAtlas2 algorithm
// (requires "sigma.forceatlas2.js" to be included) // (requires "sigma.forceatlas2.js" to be included)
sigInst.startForceAtlas2(); sigInst.startForceAtlas2();
// Draw the graph : // Draw the graph :
sigInst.draw(); sigInst.draw();
} }
   
if (document.addEventListener) { if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false); document.addEventListener("DOMContentLoaded", init, false);
} else { } else {
window.onload = init; window.onload = init;
} }
</script> </script>
   
<?php <?php
} }
if ($format == "dot") { if ($format == "dot") {
echo "}"; echo "}";
} }
if ($format == "gexf") { if ($format == "gexf") {
echo ' </edges> echo ' </edges>
</graph> </graph>
</gexf>'. PHP_EOL; </gexf>'. PHP_EOL;
} }
//include_footer(); //include_footer();
?> ?>
   
   
<
<?php <?php
include_once('include/common.inc.php'); include_once('include/common.inc.php');
include_header('Open Gov Rankings'); include_header('Open Gov Rankings');
$db = $server->get_db('disclosr-agencies'); $db = $server->get_db('disclosr-agencies');
?> ?>
<div class="foundation-header"> <div class="foundation-header">
<h1><a href="about.php">Open Government Rankings</a></h1> <h1><a href="about.php">Open Government Rankings</a></h1>
<h4 class="subheader"></h4> <h4 class="subheader"></h4>
</div> </div>
<table> <table>
<?php <?php
$agenciesdb = $server->get_db('disclosr-agencies'); $agenciesdb = $server->get_db('disclosr-agencies');
//$docsdb = $server->get_db('disclosr-documents'); //$docsdb = $server->get_db('disclosr-documents');
$scoredagencies = Array(); $scoredagencies = Array();
$scores = Array(); $scores = Array();
$columnKeys = Array(); $columnKeys = Array();
   
try { try {
$rows = $agenciesdb->get_view("app", "all", null, true)->rows; $rows = $agenciesdb->get_view("app", "all", null, true)->rows;
   
   
if ($rows) { if ($rows) {
foreach ($rows as $row) { foreach ($rows as $row) {
$columns = Array(); $columns = Array();
foreach ($row->value as $key => $value) { foreach ($row->value as $key => $value) {
if ((strstr($key, "has") || strstr($key, "URL")) && $key != "rtkURLs") { if ((strstr($key, "has") || strstr($key, "URL")) && $key != "rtkURLs") {
//echo "$key<br>"; //echo "$key<br>";
$columns[$key] = $value; $columns[$key] = $value;
} }
} }
//print_r(array_keys($columns)); //print_r(array_keys($columns));
$columnKeys = array_unique(array_merge($columnKeys, array_keys($columns))); $columnKeys = array_unique(array_merge($columnKeys, array_keys($columns)));
//print_r($columnKeys); //print_r($columnKeys);
$score = count($columns); $score = count($columns);
$scores[$score]++; if (isset($scores[$score])){
$scoredagencies[] = Array("id"=> $row->key, "website"=> $row->value->website, "name" => $row->value->name, "columns" => $columns, "score" => $score); $scores[$score]++;
  } else {
  $scores[$score] =1;
  }
  $scoredagencies[] = Array("id"=> $row->key, "website"=> (isset($row->value->website)?$row->value->website:""), "name" => $row->value->name, "columns" => $columns, "score" => $score);
} }
} }
   
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
function cmp($a, $b) function cmp($a, $b)
{ {
if ($a['score'] == $b['score']) { if ($a['score'] == $b['score']) {
return strcmp($a['name'], $b['name']); return strcmp($a['name'], $b['name']);
} }
return ($a['score'] > $b['score']) ? -1 : 1; return ($a['score'] > $b['score']) ? -1 : 1;
} }
   
usort($scoredagencies, "cmp"); usort($scoredagencies, "cmp");
echo "<tr>"; echo "<tr>";
echo "<th>Agency Name</th>"; echo "<th>Agency Name</th>";
echo "<th>Score</th>"; echo "<th>Score</th>";
foreach ($columnKeys as $columnID) { foreach ($columnKeys as $columnID) {
echo "<th>" . (isset($schemas['agency']["properties"][$columnID]['x-title']) ? $schemas['agency']["properties"][$columnID]['x-title'] : "<i>$columnID</i>") . "</th>"; echo "<th>" . (isset($schemas['agency']["properties"][$columnID]['x-title']) ? $schemas['agency']["properties"][$columnID]['x-title'] : "<i>$columnID</i>") . "</th>";
} }
echo "</tr>"; echo "</tr>";
foreach ($scoredagencies as $scoredagency) { foreach ($scoredagencies as $scoredagency) {
echo "<tr>"; echo "<tr>";
echo "<td><b><a href='getAgency.php?id=" . $scoredagency['id'] . "'>". $scoredagency['name'] . "</a></b></td>"; echo "<td><b><a href='getAgency.php?id=" . $scoredagency['id'] . "'>". $scoredagency['name'] . "</a></b></td>";
echo "<td><b>" . $scoredagency['score'] . "</b></td>"; echo "<td><b>" . $scoredagency['score'] . "</b></td>";
foreach ($columnKeys as $key) { foreach ($columnKeys as $key) {
echo "<td style='text-align: center;'>"; echo "<td style='text-align: center;'>";
if (isset($scoredagency['columns'][$key])) { if (isset($scoredagency['columns'][$key])) {
$value = $scoredagency['columns'][$key]; $value = $scoredagency['columns'][$key];
if (is_array($value)) { if (is_array($value)) {
if (count($value) == 1) { if (count($value) == 1) {
$href = $value[0]; $href = $value[0];
} else { } else {
$href = $value[0]; $href = $value[0];
} }
   
} else { } else {
$href = $value; $href = $value;
} }
if ($href[0] == "@") { if (isset($href[0]) && $href[0] == "@") {
$href = str_replace("@","https://twitter.com/",$href); $href = str_replace("@","https://twitter.com/",$href);
} }
//$href= urlencode($href); //$href= urlencode($href);
   
echo "<font color='lightgreen'>"; echo "<font color='lightgreen'>";
   
if (strstr($href, "http")) { if (strstr($href, "http")) {
echo "<a title='Yes' href='$href' style='color:lightgreen;'>&check;</a>"; echo "<a title='Yes' href='$href' style='color:lightgreen;'>&check;</a>";
} else { } else {
echo "&check;"; echo "&check;";
} }
   
echo "</font>"; echo "</font>";
} else { } else {
echo "<font color='orange'><abbr title='No'>✘</abbr></font>"; echo "<font color='orange'><abbr title='No'>✘</abbr></font>";
} }
echo "</td>"; echo "</td>";
} }
echo "</tr>\n"; echo "</tr>\n";
} }
?> ?>
</table><br> </table><br>
<div id="criteria" style="width:500px;height:900px;"></div> <div id="criteria" style="width:500px;height:900px;"></div>
<div id="scores" style="width:900px;height:500px;"></div> <div id="scores" style="width:900px;height:500px;"></div>
<script id="source"> <script id="source">
window.onload = function () { window.onload = function () {
$(document).ready(function () { $(document).ready(function () {
var d1 = []; var d1 = [];
var scorelabels = []; var scorelabels = [];
<?php <?php
try { try {
$rows = $db->get_view("app", "scoreHas?group=true", null, true)->rows; $rows = $db->get_view("app", "scoreHas?group=true", null, true)->rows;
   
   
$dataValues = Array(); $dataValues = Array();
foreach ($rows as $row) { foreach ($rows as $row) {
$dataValues[$row->value] = $row->key; $dataValues[$row->value] = $row->key;
} }
$i = 0; $i = 0;
ksort($dataValues); ksort($dataValues);
foreach ($dataValues as $value => $key) { foreach ($dataValues as $value => $key) {
   
echo " d1.push([$value, $i]);" . PHP_EOL; echo " d1.push([$value, $i]);" . PHP_EOL;
echo " scorelabels.push('$key');" . PHP_EOL; echo " scorelabels.push('$key');" . PHP_EOL;
$i++; $i++;
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
?> ?>
function scoretrackformatter(obj) { function scoretrackformatter(obj) {
if (scorelabels[Math.floor(obj.y)]) { if (scorelabels[Math.floor(obj.y)]) {
return (scorelabels[Math.floor(obj.y)]) + "=" + obj.x; return (scorelabels[Math.floor(obj.y)]) + "=" + obj.x;
   
} else { } else {
return ""; return "";
} }
} }
   
function scoretickformatter(val, axis) { function scoretickformatter(val, axis) {
if (scorelabels[Math.floor(val)]) { if (scorelabels[Math.floor(val)]) {
return (scorelabels[Math.floor(val)]) ; return (scorelabels[Math.floor(val)]) ;
   
} else { } else {
return ""; return "";
} }
} }
   
Flotr.draw(document.getElementById("criteria"), [ Flotr.draw(document.getElementById("criteria"), [
{data: d1} {data: d1}
], { ], {
title: 'Total count of agencies with criteria', title: 'Total count of agencies with criteria',
HtmlText: true, HtmlText: true,
bars: { bars: {
show: true, show: true,
horizontal: true horizontal: true
}, },
mouse: { mouse: {
track: true, track: true,
relative: true, relative: true,
trackFormatter: scoretrackformatter trackFormatter: scoretrackformatter
}, yaxis: { }, yaxis: {
autoscaling: true, autoscaling: true,
minorTickFreq: 0.6, minorTickFreq: 0.6,
noTicks: scorelabels.length, noTicks: scorelabels.length,
tickFormatter: scoretickformatter tickFormatter: scoretickformatter
}, },
xaxis: { xaxis: {
autoscaling: true autoscaling: true
   
} }
}); });
   
var d2 = []; var d2 = [];
<?php <?php
try { try {
   
ksort($scores); ksort($scores);
foreach ($scores as $key => $value) { foreach ($scores as $key => $value) {
   
echo " d2.push([$key,$value]);" . PHP_EOL; echo " d2.push([$key,$value]);" . PHP_EOL;
$i++; $i++;
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
?> ?>
   
   
Flotr.draw(document.getElementById("scores"), [ Flotr.draw(document.getElementById("scores"), [
{data: d2} {data: d2}
], { ], {
title: 'Frequency distribution of Scores', title: 'Frequency distribution of Scores',
HtmlText: true, HtmlText: true,
bars: { bars: {
show: true show: true
}, },
mouse: { mouse: {