繁体   English   中英

D3中的arc.centroid返回(NaN,NaN)

[英]arc.centroid returning (NaN, NaN) in D3

合理警告:我是D3新秀。 我正在使用D3构建一个甜甜圈图,到目前为止,一切都很好,只是切片上的标签与切片不对齐。 使用下面的代码,每个切片的标签都呈现在图表的中间,彼此堆叠,因此不可读。 我将arc.centroid放到了我的transform属性中,但是它返回的是“ NaN,NaN”,而不是实际的坐标,而且我无法理解从哪里读取的内容,即找不到数字。 我的innerRadius和externalRadius在arc变量中定义。 有什么帮助吗?

(原谅缺少jsfiddle,但是我在这里从.csv中提取数据)

var width = 300,
    height = 300,
    radius = Math.min(width, height) / 2;

var color = ["#f68b1f", "#39b54a", "#2772b2"];

var pie = d3.layout.pie()
    .value(function(d) { return d.taskforce1; })
    .sort(null);

var arc = d3.svg.arc()
    .innerRadius(radius - 85)
    .outerRadius(radius);

var svg = d3.select("#pieplate").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

d3.csv("data.csv", type, function(error, data) {
  var path = svg.datum(data).selectAll("path")
      .data(pie)
    .enter().append("path")
      .attr("fill", function(d, i) { return color[i]; })
      .attr("d", arc)
      .each(function(d) { this._current = d; }); // store the initial angles

  var text = svg.selectAll("text")
                        .data(data)
                        .enter()
                        .append("text")
                        .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
                        .attr("dy", ".35em")
                        .attr("text-anchor", "middle")
                        .text( function (d) { return d.taskforce1; })
                        .attr("font-family", "sans-serif")
                        .attr("font-size", "20px")
                        .attr("fill", "black");

  d3.selectAll("a")
      .on("click", switcher);

  function switcher() {
    var value = this.id;
    var j = value + 1;
    pie.value(function(d) { return d[value]; }); // change the value function
    path = path.data(pie); // compute the new angles
    path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
    textLabels = text.text( function (d) { return d[value]; });
  }
});

function type(d) {
  d.taskforce1 = +d.taskforce1;
  d.taskforce2 = +d.taskforce2;
  d.taskforce3 = +d.taskforce3;
  return d;
}

// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
  var i = d3.interpolate(this._current, a);
  this._current = i(0);
  return function(t) {
    return arc(i(t));
  };
}

终于明白了。 arc.centroid函数期望数据具有预先计算的startAngle和endAngle,这是pie(data)的结果。 因此以下内容对我有所帮助:

var text = svg.selectAll("text")
                    .data(pie(data))

接下来是其余的通话。 请注意,您可能必须更改访问要显示的文本数据的方式。 您随时可以用

// while adding the text elements
    .text(function(d){ console.log(d); return d.data.textAttribute })

暂无
暂无

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

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