如何从按钮运行<script>

时间:2017-12-03 02:07:11

标签: javascript d3.js

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

&#xA;&#xA;

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

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

3 个答案:

答案 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);
  }
}