-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
237 lines (222 loc) · 6.02 KB
/
Copy pathtest.html
File metadata and controls
237 lines (222 loc) · 6.02 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html>
<head>
<title>关系图</title>
<script src="jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="d3/d3.v3.min.js"></script>
</head>
<style>
.node circle {
cursor: pointer;
stroke: #3182bd;
stroke-width: 0.5px;
opacity:0.7;
}
.node text {
font: 12px sans-serif;
pointer-events: none;
text-anchor: middle;
<!--font-family:STCaiyun;.attr("opacity", "0.5")
font-size:120;-->
}
line.link {
fill: none;
stroke: #EEEEEE;
}
</style>
<body>输入框
<div id="history">查看历史:</div>
<div id="a" style="width:740px;height:740px;float:left;border:solid 1px #a0ffff;"></div>
</body>
<script type="text/javascript">
function jdtd3(){
$("#a").empty();
var width = 740,
height = 740,
dragEvent = 0,
root;
var force = d3.layout.force()
.linkDistance(80)
.charge(-120)
.gravity(.05)
.size([width, height])
.on("tick", tick);
//$("#a").empty();
var svg = d3.select("#a").append("svg")
.attr("width", width)
.attr("height", height)
.call( // <-A
d3.behavior.zoom() // <-B
.scaleExtent([-1, 10]) // <-C
.on("zoom", zoom) // <-D
).append("g");
var link = svg.selectAll(".link"),//连接线
node = svg.selectAll(".node");//节点
//image1 = svg.selectAll(".image");//图片
d3.json("data/graph22.json", function(error, json) {
root = json;
update();
});
function zoom() {
if(dragEvent!=2){
svg.attr("transform", "translate("
+ d3.event.translate
+ ")scale(" + d3.event.scale + ")");
}
}
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update links.
link = link.data(links, function(d) { return d.target.id; });
link.exit().remove();
link.enter().insert("line", ".node")
.attr("class", "link")
.style("stroke-width",function(d){
//if(d.source.num==20){
//alert(d.source.num);
return d.target.num+2.3;
//}else{
//return 2.3;
//}
});//stroke-width:20 大小
// Update nodes.
node = node.data(nodes, function(d) { return d.id; });
node.exit().remove();
var drag = force.drag()
.on("dragstart",function(d,i){//拖拽开始
dragEvent=1;
//$("#history").append(dragEvent);
d.fixed = true; //拖拽开始后设定被拖拽对象为固定
})
.on("dragend",function(d,i){//拖拽结束
dragEvent=0;
//$("#history").append(dragEvent);
})
.on("drag",function(d,i){//拖拽
dragEvent=2;
//$("#history").append(dragEvent);
});
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.on("click", click)
.call(drag);
nodeEnter.append("circle")
.attr("r", function(d) { return Math.sqrt(d.size) / 3 || 13.5; });
nodeEnter.append("text")
.attr("dy", ".35em")
.text(function(d) { return d.name; }).style("fill", textColor).style("font-weight",fontWeight);//粗细 font-weight="bold"
//11解答问题数图片
nodeEnter.append("image")
.attr("width",15)
.attr("height",15)
.attr("transform", function(d) {
return "translate(-30,8)";
})
.attr("xlink:href",function(d){
if(typeof(d.children)=="undefined"&&typeof(d._children)=="undefined"){
return "images/gif03.gif";
}
});
//11解答问题数
nodeEnter.append("text")
.attr("dy", ".35em")
.style("fill", "#436EEE")
.attr("transform", function(d) {
return "translate(-5,17)";
})
.text(function(d) { return d.size; });
//22点赞数图片
nodeEnter.append("image")
.attr("width",15)
.attr("height",15)
.attr("transform", function(d) {
return "translate(10,9)";
})
.attr("xlink:href",function(d){
if(typeof(d.children)=="undefined"&&typeof(d._children)=="undefined"){
return "images/gif07.gif";
}
});
//22点赞数
nodeEnter.append("text")
.attr("dy", ".35em")
.style("fill", "green")
.attr("transform", function(d) {
return "translate(36,17)";
})
.text(function(d) { return d.size; });
node.select("circle")
.style("fill", color);
}
//拖动更改坐标位置
function tick() {
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; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
//主节点判别 修改字体为粗体
function fontWeight(d){
if(d.name.indexOf("医院") > 0 ){
return "bold";
}
}
//主节点判别 修改字体颜色
function textColor(d){
if(d.name.indexOf("医院") > 0 ){
return "orange";
}
}
//颜色变更
function color(d) {
if(d.name.indexOf("医院") > 0 ){
return "red";
}
return d._children ? "yellow" // collapsed package
: d.children ? "#436EEE" // expanded package
: "#0099CC"; // leaf node
}
// 点击事件
function click(d) {
if(typeof(d.children)=="undefined"&&typeof(d._children)=="undefined"){
overclick(d);
}else{
if (d3.event.defaultPrevented) return; // ignore drag
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
}
update();
}
//点击事件 最后一层
function overclick(d){
//alert(d.name);
$("#history").append("<a href='#' onclick='jdtd3();' style='text-decoration:none;'>"+d.name+"</a>>");
jdtd3();
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}
}
jdtd3();
</script>
</html>