Add dot/graphviz graph output
[disclosr.git] / graph.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
include_once('include/common.inc.php');
//include_header();
$format = "html";
if (isset($_REQUEST['format'])) {
    $format = $_REQUEST['format'];
}
 
function add_node($id, $label) {
    global $format;
    if ($format == "html") {
        echo "nodes[\"$id\"] = graph.newNode({label: \"$label\"});" . PHP_EOL;
    }
     if ($format == "dot" && $label != "") {
         echo "$id [label=\"$label\"];". PHP_EOL;
     }
}
 
function add_edge($from, $to, $color) {
    global $format;
    if ($format == "html") {
        echo "graph.newEdge(nodes[\"$from\"], nodes['$to'], {color: '$color'});" . PHP_EOL;
    }
    if ($format == "dot") {
        echo "$from -> $to ".($color != ""? "[color=$color]":"").";". PHP_EOL;
    }
}
 
if ($format == "html") {
    ?>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script src="lib/springy/springy.js"></script>
    <script src="lib/springy/springyui.js"></script>
    <script>
        var graph = new Graph();
        var nodes = [];
    <?php
}
if ($format == "dot") {
    echo 'digraph g {'. PHP_EOL;
}
$db = $server->get_db('disclosr-agencies');
 add_node("fedg","Federal Government - Commonwealth of Australia");
try {
    $rows = $db->get_view("app", "byCanonicalName", null, true)->rows;
//print_r($rows);
    foreach ($rows as $row) {
        add_node($row->id, $row->key);
    }
} catch (SetteeRestClientException $e) {
    setteErrorHandler($e);
}
 
try {
    $rows = $db->get_view("app", "byDeptStateName", null, true)->rows;
//print_r($rows);
    foreach ($rows as $row) {
        add_edge("fedg", $row->value, 'yellow');
    }
} catch (SetteeRestClientException $e) {
    setteErrorHandler($e);
}
 
try {
    $rows = $db->get_view("app", "parentOrgs", null, true)->rows;
//   print_r($rows);
    foreach ($rows as $row) {
        add_edge($row->key, $row->value, 'blue');
    }
} catch (SetteeRestClientException $e) {
    setteErrorHandler($e);
}
if ($format == "html") {
    ?>
        window.onload = function() {
            $(document).ready(function() {
                var springy = $('#springydemo').springy({
                    graph: graph
                });
            });
        };
    </script>
 
    <canvas id="springydemo" width="1260" height="680" />
    <?php
}
if ($format == "dot") {
    echo "}";
}
//include_footer();
?>