我是新的网页设计,我正在使用javascript库绘制树。我的问题是它在页面加载时自动绘制。我想有一个按钮,然后点击这幅画,但我找不到怎么做。尝试将该函数传递给javascript文件,但在传递它时我不知道在html文件中要调用它。


这是小提琴 https://jsfiddle.net/nse4w959/


 < script src =“http://d3js.org/d3.v3.min.js”>< / script>
< script>
 var treeData = [
 {
 “名称”:“顶级”,
 “parent”:“null”,
 “孩子们”:[
 {
 “名称”:“2级:A”,
 “父母”:“顶级”,
 “孩子们”:[
 {
 “名字”:“A之子”,
 “父母”:“等级2:A”
 },
 {
 “名字”:“A的女儿”,
 “父母”:“等级2:A”
 }
 ]
 },
 {
 “名称”:“2级:B”,
 “父母”:“顶级”
 }
 ]
 }
 ];
 // **************生成树形图*****************
 var margin = {top:40,right:120,bottom:20,left:120},
 width = 960 - margin.right - margin.left,
 height = 500 - margin.top - margin.bottom;
 var i = 0;
 var tree = d3.layout.tree()。size([height,width]);
 var diagonal = d3.svg.diagonal()。projection(function(d){return [d.x,d.y];});
 var svg = d3.select(“body”)。append(“svg”)
 .attr(“width”,width + margin.right + margin.left)
 .attr(“height”,height + margin.top + margin.bottom).append(“g”)
 .attr(“transform”,“translate(”+ margin.left +“,”+ margin.top +“)”);
 root = treeData [0];
更新(根);
功能更新(来源){
 //计算新的树布局。
 var nodes = tree.nodes(root).reverse(),
 links = tree.links(nodes);
 //标准化为fixed-depth.nodes.forEach(function(d){d.y = d.depth * 100;});
 //声明节点......
 var node = svg.selectAll(“g.node”)。data(nodes,function(d){return d.id ||(d.id = ++ i);});
 //输入节点。
 var nodeEnter = node.enter()。append(“g”)。attr(“class”,“node”)
 .attr(“transform”,function(d){
 return“translate(”+ d.x +“,”+ d.y +“)”;});
 nodeEnter.append(“circle”)。attr(“r”,10).style(“fill”,“#ff”);
 nodeEnter.append(“text”)。attr(“y”,function(d){
 return d.children || d._children?-18:18;})
 .attr(“dy”,“。35em”)
 .attr(“text-anchor”,“middle”)
 .text(function(d){return d.name;})
 .style(“fill-opacity”,1);
 //声明链接...
 var link = svg.selectAll(“path.link”)
 .data(links,function(d){return d.target.id;});
 //输入链接。
 link.enter()。insert(“path”,“g”)
 .attr(“class”,“link”)
 .attr(“d”,对角线);
 }
< /脚本>
 代码>


答案 0 :(得分:2)
调用按钮的最简单方法是
<button onclick="update()">Update</button>
答案 1 :(得分:1)
您可以使用Javascript函数包装代码,并使用按钮的onclick
属性将其激活。
检查下面的代码并查看其工作原理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 12px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
function renderTree() {
var treeData = [
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
}
];
// ************** Generate the tree diagram *****************
var margin = {top: 40, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree().size([height, width]);
var diagonal = d3.svg.diagonal().projection(function(d) { return [d.x, d.y]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom).append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.nodes.forEach(function(d) { d.y = d.depth * 100; });
// Declare the nodes…
var node = svg.selectAll("g.node").data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g").attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; });
nodeEnter.append("circle").attr("r", 10).style("fill", "#fff");
nodeEnter.append("text").attr("y", function(d) {
return d.children || d._children ? -18 : 18; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name; })
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
}
}
</script>
<input type="button" value="Render" onclick="renderTree();">
</body>
</html>
答案 2 :(得分:0)
https://www.codeproject.com/Tips/1021936/Creating-Vertical-Collapsible-Tree-With-d-js支持
html文件
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<input id="clickMe" type="button" value="Click Me" onclick="go();" />
<div id="tree">
</div>
</body>
</html>
css文件
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
.tree {
margin-bottom: 10px;
overflow: auto;
}
js file
function go(){
var treeData =
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A",
"children": []
},
{
"name": "Daughter of A",
"parent": "Level 2: A",
"children": []
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level",
"children": []
}
]
};
BuildVerticaLTree(treeData,"#tree");
}
function BuildVerticaLTree(treeData, treeContainerDom) {
var margin = {
top: 40,
right: 120,
bottom: 20,
left: 120
};
var width = 960 - margin.right - margin.left;
var height = 960 - margin.top - margin.bottom;
var i = 0,
duration = 750;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.x, d.y];
});
var svg = d3.select(treeContainerDom).append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData;
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) {
d.y = d.depth * 100;
});
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) {
return d.id || (d.id = ++i);
});
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + source.x0 + "," + source.y0 + ")";
}).on("click", nodeclick);
nodeEnter.append("circle")
.attr("r", 10)
.attr("stroke", function(d) {
return d.children || d._children ? "steelblue" : "#00c13f";
})
.style("fill", function(d) {
return d.children || d._children ? "lightsteelblue" : "#fff";
});
//.attr("r", 10)
//.style("fill", "#fff");
nodeEnter.append("text")
.attr("y", function(d) {
return d.children || d._children ? -18 : 18;
})
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
return d.name;
})
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
//horizontal tree
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
nodeUpdate.select("circle")
.attr("r", 10)
.style("fill", function(d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + source.x + "," + source.y + ")";
})
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) {
return d.target.id;
});
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {
x: source.x0,
y: source.y0
};
return diagonal({
source: o,
target: o
});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {
x: source.x,
y: source.y
};
return diagonal({
source: o,
target: o
});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function nodeclick(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
}