【问题标题】:D3 Pie Error: Invalid value for <text> attribute transform="translate(NaN,NaN)"D3 饼图错误:<text> 属性 transform="translate(NaN,NaN)" 的值无效
【发布时间】:2016-02-10 01:08:28
【问题描述】:

不断收到控制台错误 错误:属性 transform="translate(NaN,NaN)" 的值无效

在此处重新创建错误。 http://jsfiddle.net/9f9wonoc/

var width = 360;
var height = 360;
var radius = Math.min(width, height) / 2;
var color = {
'Pass': '#66B51B',
'Fail': '#d03324'
}

var data = [
{ label: 'Pass', count: 12 },
{ label: 'Fail', count: 10 },
];

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

var arc = d3.svg.arc()
.outerRadius(radius);

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

var path = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");

path.append("path")
.attr("d", arc)
.style("fill", function(d) { return color[d.data.label]; });

path.append("text")
.attr("transform", function(d, i) {
return "translate(" + arc.centroid(d, i) + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.data.count; });

我做错了什么?

【问题讨论】:

    标签: d3.js


    【解决方案1】:

    你错过了一件小事:

    您需要将内半径设置为 0,如下所示:

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

    工作代码here

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2015-06-26
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 2021-07-25
      • 2013-08-18
      相关资源
      最近更新 更多