错误:<路径>属性d:期望的数字“ M30,NaNC120,NaN 120,…”

时间:2019-06-28 07:06:34

标签: javascript

我的代码应该创建一个自定义窗口小部件,但是会崩溃,从而在控制台中留下以下错误:

 Error: <path> attribute d: Expected number, "M30,NaNC120,NaN 120,…".

 // tree like structure is not getting its showing some error Error:

 <path> attribute d: Expected number,
 "….95212837925925,NaNC15.021854435…". d3-v2-min.js?63697

 Error: <path> attribute d: Expected
 number"….95212837925925,NaNC15.021854435…".

我的小部件代码:

var i = 0,
  duration = 750,
  rectW = 60,
  rectH = 30,
  width = 960 - margin.right - margin.left,
  height = 500 - margin.top - margin.bottom;

var zm;

var tree = d3.layout.tree().size(height, width);
var diagonal = d3.svg.diagonal()
  .projection(function(d) {
    return [d.y + rectW / 2, d.x + rectH / 2];
  });
console.info("passed var diagonal")
var svg = d3.select("body").append("svg").attr("width", 1000).attr("height", 1000)
  .call(zm = d3.behavior.zoom().scaleExtent([1, 3]).on("zoom", redraw)).append("g")
  .attr("transform", "translate(" + 350 + "," + 20 + ")");
console.info("passed var diagonal")
//necessary so that zoom knows where to zoom and unzoom from
zm.translate([350, 20]);



root.x0 = 0;
root.y0 = height / 2;

console.info("passed root x and y")

function collapse(d) {
  if (d.children) {
    d._children = d.children;
    d._children.forEach(collapse);
    d.children = null;
  }
}

console.info("passed function collapse diagonal")

root.children.forEach(collapse);
update(root);


console.info("passed function update root")

d3.select("body").style("height", "800px");

console.info("passed function d3.select")

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
    links = tree.links(nodes);

  console.info("passed function var nodes")

  // Normalize for fixed-depth.
  nodes.forEach(function(d) {
    d.y = d.depth * 180;
  });

  // Update the nodes…
  var node = svg.selectAll("g.node")
    .data(nodes, function(d) {
      return d.id || (d.id = ++i);
    });


  console.info("passed function g.node")

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
    .attr("class", "node")
    .attr("transform", function(d) {
      return "translate(" + source.y0 + "," + source.x0 + ")";
    })
    .on("click", click);

  console.info("passed function node enter")

  nodeEnter.append("rect")
    .attr("width", rectW)
    .attr("height", rectH)
    .attr("stroke", "black")
    .attr("stroke-width", 1)
    .style("fill", function(d) {
      return d._children ? "lightsteelblue" : "#fff";
    });

  console.info("passed function d._children")

  nodeEnter.append("text")
    .attr("x", rectW / 2)
    .attr("y", rectH / 2)
    .attr("dy", ".35em")
    .attr("text-anchor", "middle")
    .text(function(d) {
      return d.name;
    });

  console.info("passed function d3.name")

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
    .duration(duration)
    .attr("transform", function(d) {
      return "translate(" + d.y + "," + d.x + ")";
    });

  console.info("passed function node update")

  nodeUpdate.select("rect")
    .attr("width", rectW)
    .attr("height", rectH)
    .attr("stroke", "black")
    .attr("stroke-width", 1)
    .style("fill", function(d) {
      return d._children ? "lightsteelblue" : "#fff";
    });

  console.info("passed function node update.select")

  nodeUpdate.select("text")
    .style("fill-opacity", 1);

  console.info("passed function node nodeupdate.select")

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
    .duration(duration)
    .attr("transform", function(d) {
      return "translate(" + source.y + "," + source.x + ")";
    })
    .remove();

  console.info("passed function node exit")

  nodeExit.select("rect")
    .attr("width", rectW)
    .attr("height", rectH)
    .attr("stroke", "black")
    .attr("stroke-width", 1);

  nodeExit.select("text");

  console.info("passed function node exit")

  // Update the links…
  var link = svg.selectAll("path.link")
    .data(links, function(d) {
      return d.target.id;
    });

  console.info("passed function link")

  // Enter any new links at the parent's previous position.
  link.enter().insert("path", "g")
    .attr("class", "link")
    .attr("x", rectW / 2)
    .attr("y", rectH / 2)
    .attr("d", function(d) {
      var o = {
        x: source.x0,
        y: source.y0
      };
      return diagonal({
        source: o,
        target: o
      });
    });


  console.info("passed function node link enter")

  // Transition links to their new position.
  link.transition()
    .duration(duration)
    .attr("d", diagonal);

  console.info("passed function node link transition")

  // 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();

  console.info("passed function node link exit transition")

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.y0 = d.y;
    d.x0 = d.x;
  });
}
console.info("passed function node for each")

// Toggle children on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}

console.info("passed function click(d)")

//Redraw for zoom
function redraw() {
  svg.attr("transform",
    "translate(" + d3.event.translate + ")" +
    " scale(" + d3.event.scale + ")");
}

console.info("passed function redraw for zoom")

// this._executeCallback(callback, "_updateRendering");
},

我期望树状结构,但是它在创建结构时显示了一些错误,但是它传递了全部功能

0 个答案:

没有答案