简体   繁体   中英

d3.js arc.centroid(d) : Error: Invalid value for <text> attribute transform=“translate(NaN,NaN)”

I'm trying to center the arc labels with the centroid() function as described in D3 documentation .

arcs.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.text(function(d) { return "labels"; });

but I'm getting an error on the translate css property :

Error: Invalid value for attribute transform="translate(NaN,NaN)"

one of the console.log of the (d) objects in :

.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })

returns this:

data: 80
endAngle: 1.9112350744272506
padAngle: 0
startAngle: 0
value: 80
__proto__: Object

Here's my code:

HTML:

<div id="firstChart"></div>

JS:

var myData = [80,65,12,34,72];
var nbElement = myData.length;
var angle = (Math.PI * 2) / nbElement ;
var myColors = ["#e7d90d", "#4fe70d", "#0de7e0", "#f00000", "#00DC85"];
var width = 1500;
var height = 1500;
var radius = 200;

var canvas = d3.select("#firstChart")
              .append("svg")
              .attr("height", height)
              .attr("width", width);

var group = canvas.append("g")
                .attr("transform","translate(310, 310)");

var arc = d3.svg.arc()
                .innerRadius(0)       
                .outerRadius(function (d,i) { return (myData[i]*2); })
                .startAngle(function (d,i) { return (i*angle); })
                .endAngle(function (d,i) { return (i*angle)+(1*angle); });

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

var arcs = group.selectAll(".arc")
            .data(pie(myData))
            .enter()
            .append("g")
            .attr("class", "arc");

arcs.append("path")
    .attr("d", arc)
    .attr("fill", function (d, i) { return ( myColors[i] ); });

arcs.append("text")
    .attr("transform", function(d) {console.log(d); return "translate(" + arc.centroid(d) + ")"; })
    .text(function(d) { return d.data; });

var outlineArc = d3.svg.arc()
        .innerRadius(0)
        .outerRadius(radius)
        .startAngle(function (d,i) { return (i*angle); })
        .endAngle(function (d,i) { return (i*angle)+(1*angle); });

var outerPath = group.selectAll(".outlineArc")
      .data(pie(myData))
    .enter().append("path")
      .attr("fill", "none")
      .attr("stroke", "black")
      .attr("stroke-width", "2")
      .attr("class", "outlineArc")
      .attr("d", outlineArc);   

You need to pass the index of the element to centroid as well as the datum:

    .attr("transform", function(d, i) { return "translate(" + arc.centroid(d, i) + ")"; })

During the call to arc.centroid , D3 calls the arc functions you have created for outerRadius , startAngle and endAngle . However, if you don't pass the index to arc.centroid , D3 has got no index to pass on to the arc functions, all of which use the index. Hence the centroid calculations end up with NaN s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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