/
visualize.php
165 lines (148 loc) · 4.25 KB
/
visualize.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
$dbhost = 'http://localhost:8777';
// save to db function
function databaseCall($path = '/', $wut = null, $getheaders = true) {
global $dbhost;
if (is_array($wut)) {
$body = json_encode($wut);
} else if ($wut != null) {
$body = trim($wut);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dbhost . $path);
curl_setopt($ch, CURLOPT_HEADER, $getheaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
if (isset($body) && $body != '') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
$raw_result = curl_exec($ch);
curl_close($ch);
if ($raw_result == '') {
return false;
}
$json_result = json_decode($raw_result, true);
if (is_array($json_result)) {
return $json_result;
} else {
return $raw_result;
}
}
if (databaseCall('/') == '') {
die('database not accessible, make sure it\'s running');
}
// ok -- get stuff
$nodes = databaseCall('/nodes', null, false);
if (!is_array($nodes)) {
die('no nodes to show');
}
$conns = databaseCall('/connections', null, false);
?>
<!--
<?php
print_r($nodes);
print_r($conns);
?>
-->
<?php
function getNodeCoords($node_id) {
global $nodes;
$coords = array('X' => 0, 'Y' => 0);
foreach ($nodes as $node) {
if ($node['Id'] * 1 == $node_id) {
$coords['X'] = $node['X'] * 1;
$coords['Y'] = $node['Y'] * 1;
}
}
return $coords;
}
$grid_scale = 25; // this many pixels for every grid point
$grid_width = 0; // will be set in a minute
$grid_height = 0; // will be set in a minute
// get max node X and add 1 to make the max width
// get max node Y and add 1 to make the max height
foreach ($nodes as $node) {
if ($node['X'] * 1 >= $grid_width) {
$grid_width = $node['X'] + 2;
}
if ($node['Y'] * 1 >= $grid_height) {
$grid_height = $node['Y'] + 2;
}
}
$actual_grid_width = $grid_width * $grid_scale;
$actual_grid_height = $grid_height * $grid_scale;
?><!doctype html>
<html>
<head>
<title>Visualize the Graph</title>
</head>
<body style="margin:0;padding:0;">
<!-- ><h1>SIGIL Visualization</h1> -->
<div>
<canvas id="graph" style="background-color:#fff;" width="<?php echo $actual_grid_width; ?>px" height="<?php echo $actual_grid_height; ?>px"></canvas>
</div>
<script type="text/javascript">
// set up the canvas
var a_canvas = document.getElementById('graph');
var a = a_canvas.getContext('2d');
a.font = "12px Courier New";
// draw gridlines
a.strokeStyle = "#ddd";
a.lineWidth = 1;
for (var x = 0.5; x < <?php echo $actual_grid_width; ?>; x += <?php echo $grid_scale; ?>) {
a.beginPath();
a.moveTo(x, 0);
a.lineTo(x, <?php echo $actual_grid_height; ?>);
a.stroke();
a.closePath();
}
for (var y = 0.5; y < <?php echo $actual_grid_height; ?>; y += <?php echo $grid_scale; ?>) {
a.beginPath();
a.moveTo(0, y);
a.lineTo(<?php echo $actual_grid_width; ?>, y);
a.stroke();
a.closePath();
}
// draw connections
a.strokeStyle = "#900";
a.lineWidth = 2;
<?php
if (is_array($conns)) {
foreach ($conns as $conn) {
$source_coords = getNodeCoords($conn['Source']);
$target_coords = getNodeCoords($conn['Target']);
echo 'a.beginPath();'."\n";
echo 'a.moveTo('.($source_coords['X'] * $grid_scale).', '.($source_coords['Y'] * $grid_scale).');'."\n"; // source
echo 'a.lineTo('.($target_coords['X'] * $grid_scale).', '.($target_coords['Y'] * $grid_scale).');'."\n"; // target
echo 'a.stroke();'."\n";
echo 'a.closePath();'."\n";
}
}
?>
// draw nodes
<?php
foreach ($nodes as $node) {
echo 'a.beginPath();'."\n";
echo 'a.arc('.($node['X'] * $grid_scale).', '.($node['Y'] * $grid_scale).', 5, 0, 2 * Math.PI, false);'."\n";
if (isset($node['Extra']) && isset($node['Extra']['color'])) {
$fill_color = $node['Extra']['color'];
} else {
$fill_color = '#aaa';
}
echo 'a.fillStyle = "'.$fill_color.'";'."\n"; // fill of the node itself
echo 'a.fill();'."\n";
echo 'a.closePath();'."\n";
echo 'a.fillStyle = "#ccc";'."\n";
echo 'a.textAlign = "center";'."\n";
echo 'a.fillText("'.$node['Name'].'", '.($node['X'] * $grid_scale).', '.($node['Y'] * $grid_scale).');'."\n";
echo 'a.fillStyle = "#000";'."\n";
echo 'a.fillText("'.$node['Name'].'", '.($node['X'] * $grid_scale - 1).', '.($node['Y'] * $grid_scale - 1).');'."\n";
}
?>
// that's all, folks
</script>
</body>
</html>