繁体   English   中英

D3.js 错误:<g> 属性变换:预期数字,“translate(NaN,NaN)”</g>

[英]D3.js Error: <g> attribute transform: Expected number, “translate(NaN,NaN)”

我正在尝试使用 d3 构建数据可视化,但在控制台中遇到以下错误。

Error: <g> attribute transform: Expected number, "translate(NaN,NaN)".

我从这个 Plunker 中获得灵感,并调整了一些东西以使其与我自己的数据兼容。

http://plnkr.co/edit/FiUrNHNMAXd7EBpr5uY5?p=preview&preview

任何帮助将不胜感激。

我的 index.html 完全一样。

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.9/d3.js"></script>
</head>

<body>
<script src="script.js"></script>
</body>

</html>

这是我的 script.js

var width = 960,
height = 500,
padding = 1.5, // separation between same-color nodes
clusterPadding = 6, // separation between different-color nodes
maxRadius = 100;
minRadius = 1;


d3.csv("modified_data_happy.csv", function(data) {
m = d3.max(data, function(d){return d.city_index});
console.log("m", m);
color = d3.scale.category10()
.domain(d3.range(m));
clusters = new Array(m);
dataset = data.map(function(d) {
var r = parseInt(d.Happy);
// var r = d.Happy;

var dta = {
  cluster: d.city_index,//group
  name: d.name,//label
  radius: r,//radius
  x: Math.cos(d.city_index / m * 2 * Math.PI) * 100 + width / 2 + Math.random(),
  y: Math.sin(d.city_index / m * 2 * Math.PI) * 100 + height / 2 + Math.random()
  };
  //add the one off the node inside teh cluster
  if (!clusters[d.city_index] || (d.Happy > clusters[d.city_index].radius)) 
  clusters[d.city_index] = dta;

  console.log("dta", dta);

  return dta;
  });
 console.log('dataset', dataset); 
 makeGraph(dataset);
 });


function makeGraph(nodes) {
 var force = d3.layout.force()
 .nodes(nodes)
 .size([width, height])
 .gravity(.02)
 .charge(0)
 .on("tick", tick)
 .start();

  var svg = d3.select("body").append("svg")
   .attr("width", width)
   .attr("height", height);

  var node = svg.selectAll("circle")
   .data(nodes)
   .enter().append("g").call(force.drag);

 node.append("circle")
  .style("fill", function(d) {
   return color(d.cluster);
  }).attr("r", function(d) {
  return d.Happy
  })

 node.append("text")
  .text(function(d) {
  return d.name;
  })
  .attr("dx", -10)
  .attr("dy", ".35em")
  .text(function(d) {
  return d.name
  })
  .style("stroke", "none");

function tick(e) {
  node.each(cluster(10 * e.alpha * e.alpha))
  .each(collide(.5))
  // .attr("transform", functon(d) {});
  .attr("transform", function(d) {
    var k = "translate(" + d.x + "," + d.y + ")";
    return k;
  })

 }

// Move d to be adjacent to the cluster node.
function cluster(alpha) {
return function(d) {
  var cluster = clusters[d.cluster];
  if (cluster === d) return;
  var x = d.x - cluster.x,
    y = d.y - cluster.y,
    l = Math.sqrt(x * x + y * y),
    r = d.Happy + cluster.radius;
  if (l != r) {
    l = (l - r) / l * alpha;
    d.x -= x *= l;
    d.y -= y *= l;
    cluster.x += x;
    cluster.y += y;
  }
  };
 }

 // Resolves collisions between d and all other circles.
 function collide(alpha) {
  var quadtree = d3.geom.quadtree(nodes);
  return function(d) {
  var r = d.Happy + maxRadius + Math.max(padding, clusterPadding),
    nx1 = d.x - r,
    nx2 = d.x + r,
    ny1 = d.y - r,
    ny2 = d.y + r;
  quadtree.visit(function(quad, x1, y1, x2, y2) {
    if (quad.point && (quad.point !== d)) {
      var x = d.x - quad.point.x,
        y = d.y - quad.point.y,
        l = Math.sqrt(x * x + y * y),
        r = d.Happy + quad.point.radius + (d.cluster === quad.point.cluster ? padding : 
       clusterPadding);
      if (l < r) {
        l = (l - r) / l * alpha;
        d.x -= x *= l;
        d.y -= y *= l;
        quad.point.x += x;
        quad.point.y += y;
      }
    }
    return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
  });
  };
  }
  }

我的 csv 文件看起来像这样(我用“city_index”替换了“group”,用“Happy”替换了“radius”)。 而已。

index |       name    | city_index         | Happy -----> Header Row
0     |       Joe     | 3                  | 60   ----->  Data Row

这就是我现在的位置。 我不确定为什么它不起作用,因为我替换示例的数据结构是相同的。 我尝试删除解析器无济于事。 我还尝试修改“转换、翻译”属性。

任何帮助将不胜感激。

问题表明 SVG 元素组 ( tag ) 对于 position 坐标具有 NaN。 我在您的代码中看到了一个问题: .attr("dy", ".35em")尝试摆脱 em 值并使用原始数字。 我不确定它是否会解决您的问题,如果您可以在控制台中发布错误并且它适用的元素我可以为您提供更多帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM