-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.html
More file actions
executable file
·220 lines (169 loc) · 6.45 KB
/
edit.html
File metadata and controls
executable file
·220 lines (169 loc) · 6.45 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>思维导图</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.node circle {
fill: #999;
}
.node text {
font: 10px sans-serif;
}
.node--internal circle {
fill: #555;
}
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #555;
stroke-opacity: 0.4;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<svg width="800" height="600"></svg>
<br>选中元素<br>
<input type="text" name="选中元素" value="" id="select">
<input type="submit" id="delete" value="delete" />
<br>增加元素<br>
<input type="text" name="增加元素" value="" id="addNum">
<input type="submit" id="add" value="add" />
<br>修改元素<br>
<input type="text" name="修改名字" value="" id="changename1">
<input type="submit" id="change" value="change" />
<script>
//currentdata 当前数据
d3.csv("flare.csv", function(error, data) {
//console.log(data);
if (error) throw error;
document.getElementById("delete")
.addEventListener("click", deletedata);
document.getElementById("add")
.addEventListener("click", adddata);
document.getElementById("change")
.addEventListener("click", changename);
function saveData() {
// var request = new XMLHttpRequest();
// request.open("POST", document.URL, true);
// request.onreadystatechange = on_change;
// function on_change() {
// if (request.readyState == 4) {
// if (request.status == 200) {
// alert("Save file success");
// } else {
// alert("Save file failed");
// }
// }
// }
// request.send(d3.csvFormat(data));
}
draw(data);
});
//画图之函数,可供重复调用
function draw(data) {
//console.log(data);
currentdate = data;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height")
svg.selectAll("*").remove();
var g = svg.append("g").attr("transform", "translate(40,0)");
var tree = d3.tree()
.size([height, width - 160]);
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
var root = stratify(data)
.sort(function(a, b) { return (a.height - b.height) || a.id.localeCompare(b.id); });
var link = g.selectAll(".link")
.data(tree(root).links())
.enter().append("path")
.attr("class", "link")
.attr("d", d3.linkHorizontal()
.x(function(d) { return d.y; })
.y(function(d) { return d.x; }));
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.on("click",function(d,i){
for(var key in d3.select(this)){
if(key == "_groups"){
var text = d3.select(this)[key][0][0].getElementsByTagName('text')[0];
console.log(text);
document.getElementById("select").value = text.innerHTML;
}
}
myarr = new Array();
for(var x in data){
myarr.push(data[x]);
}
// draw(data);
// console.log(data);
})
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dy", 3)
.attr("x", function(d) { return d.children ? -8 : 8; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });
}
function deletedata(){
if(document.getElementById("select").value != null){
for(var x in myarr){
var helloword = myarr[x]["id"];
if(typeof(helloword)!="undefined" && helloword.indexOf(document.getElementById("select").value)>-1){
for(var x in currentdate){
var word = currentdate[x]["id"];
if(typeof(word)!="undefined" && helloword == word){
currentdate.splice(x,1);
break;
}
}
}
}
draw(currentdate);
}
}
function adddata(){
if(document.getElementById("select").value != null && document.getElementById("addNum").value != null){
for(var x in myarr){
var helloword = myarr[x]["id"];
if(typeof(helloword)!="undefined" && helloword.endWith(document.getElementById("select").value)){
var newnode = helloword + "." + (document.getElementById("addNum").value);
var dic = new Array();
dic["id"] = newnode;
currentdate.push(dic);
console.log(newnode);
}
}
draw(currentdate);
}
}
function changename(){
if(document.getElementById("select").value != null && document.getElementById("changename1").value != null){
for(var x in currentdate){
var word = currentdate[x]["id"];
if(typeof(word)!="undefined" && word.indexOf(document.getElementById("select").value)>-1){
currentdate[x]["id"] = word.replace(document.getElementById("select").value,document.getElementById("changename1").value);
console.log(word.replace(document.getElementById("select").value,document.getElementById("changename1").value));
console.log(document.getElementById("changename1").value);
}
}
console.log(currentdate);
draw(currentdate);
}
}
String.prototype.endWith=function(endStr){
var d=this.length-endStr.length;
return (d>=0&&this.lastIndexOf(endStr)==d)
}
</script>
</body>
</html>