-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctestWorking2.php
More file actions
166 lines (137 loc) · 4.07 KB
/
ctestWorking2.php
File metadata and controls
166 lines (137 loc) · 4.07 KB
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
166
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
stroke: #000;
stroke-width: 1.5px;
}
.node {
stroke: #fff;
stroke-width: 1.5px;
}
d3-tip {
line-height: 1;
color: black;
}
</style>
<body>
<script src="js/d3.min.js"></script>
<script src="js/d3.tooltip.js"></script>
<script>
//DIMENSIONS, radius for border
var width = 1500,
height = 750,
radius = 10;
//how we color the graph. color() is fed
//an index into an 'array' of colors,
//used to group nodes
var color = d3.scale.category10();
var nodes = [],
links = [];
//force graph, click-and-draggable
var force = d3.layout.force()
.nodes(nodes)
.links(links)
.gravity(.15)
.charge(-400)
.linkDistance(50)
.size([width, height])
.on("tick", tick);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
//node/edge group declarations
var node = svg.selectAll(".node")
var link = svg.selectAll(".link");
//////////////BEGIN TOOLTIP ON MOUSEOVER CODE
//offset -> adjust text offset
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
console.log(d.id);
return d.id + "";
})
svg.call(tip);
//////////////END TOOLTIP ON MOUSEOVER CODE
// 1. Add three nodes and three links.
<?php
include_once "includes/nodeadder.php";
//print all node/edge declarations and pushes to array
printGraphDeclarations();
?>
start();
//////////////DOUBLE CLICK TO HIGHLIGHT CAPABILITY
//toggle stores whether the highlighting is one
var toggle = 0;
//Create an array logging what is connected to what
var linkedByIndex = {};
/* Prolly not needed
for(i = 0; i < node.length; i++) {
linkedByIndex[i + "," + i] = 1;
};
*/
//console.log("links.length = " + links.length);
for(i = 0; i < links.length; i++) {
linkedByIndex[links[i].source.index + "," + links[i].target.index] = 1;
};
//console.log("links.length = " + links.length);
//console.log(linkedByIndex.toString());
//This function looks up whether a pair are neighbors
function neighboring(a, b) {
return linkedByIndex[a.index + "," + b.index];
}
//Called to fade out all nodes that aren't
//connected to the double clicked node
function connectedNodes() {
if (toggle == 0) {
//Reduce opacity of all but the neighboring nodes
d = d3.select(this).node().__data__;
node.style("opacity", function(o) {
//console.log("Selected node: o=" + o.index + ", d=" + d.index +"\n");
if (d == o) return 1;
return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
});
link.style("opacity", function(o) {
return d.index==o.source.index | d.index==o.target.index ? 1 : 0.1;
});
//Reduce the op
toggle = 1;
} else {
//put them back to opacity=1
node.style("opacity", 1);
link.style("opacity", 1);
toggle = 0;
}
}
//////////////END DOUBLE CLICK TO HIGHLIGHT CODE
function start() {
link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
link.enter()
.insert("line", ".node")
.attr("class", "link");
link.exit().remove();
node = node.data(force.nodes(), function(d) { return d.id;});
//functions for each node on init of graph
node.enter()
.append("circle")
.attr("class", function(d) { return "node " + d.id; })
.attr("r", 8)
.style("fill", function(d) { return color(d.group); })
.call(force.drag)
.on("dblclick", connectedNodes)
.on("mouseover", tip.show)
.on("mouseout", tip.hide);
node.exit().remove();
force.start();
}
function tick() {
//updates position & the radius/math.min is to keep inside the screen
node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); })
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
}
</script>