d3.js version 7 – pie and arc – Error: <path> attribute d: Expected number, "…LNaN,NaNZ"

huangapple go评论63阅读模式
英文:

d3.js version 7 - pie and arc - Error: <path> attribute d: Expected number, "…LNaN,NaNZ"

问题

I'm trying to do a spin wheel with d3.js. Everything worked fine with the old version 3. The pies and arcs didn't have any problem. When switching to d3.js v5 or later, I've got an omnipresent NaN in path attribute "d".

here's a correctly working fiddle with d3.js v3

and the same code in v7 (only calls for "arc" and "pie" are replaced as per documentation).

here's broken paths with d3.js v7

I can't figure out what's wrong, and I need at least the 5th version for using advanced features.

Any help, please?

英文:

I'm trying to do a spin wheel with d3.js. Everything worked fine with the old version 3. The pies and arcs didn't have any problem. When switching to d3.js v5 or later, i've got an omnipresent NaN in path attribute "d".

here's a correctly working fiddle with d3.js v3

and the same code in v7 (only calls for "arc" and "pie" are replaced as per documentation).

here's broken paths with d3.js v7

I can't figure out what's wrong and i need at least 5th version for using advanced features.

any help please?

var w = 540,
    h = 540,
    r = Math.min(w, h) / 2;

var data = [
  { &quot;label&quot;: &quot;iPhone 14 Pro Max&quot;, &quot;value&quot;: 1, },
    { &quot;label&quot;: &quot;100B&quot;, &quot;value&quot;: 2, },
    { &quot;label&quot;: &quot;Galaxy S23 Ultra&quot;, &quot;value&quot;: 3, },
    { &quot;label&quot;: &quot;100B&quot;, &quot;value&quot;: 4, },
    { &quot;label&quot;: &quot;Galaxy Buds Pro&quot;, &quot;value&quot;: 5, },
    { &quot;label&quot;: &quot;100B&quot;, &quot;value&quot;: 6, },
    { &quot;label&quot;: &quot;Apple Watch Series 8&quot;, &quot;value&quot;: 7, },
    { &quot;label&quot;: &quot;Apple Airpods 3&quot;, &quot;value&quot;: 8, },
    { &quot;label&quot;: &quot;iPhone 14 Pro&quot;, &quot;value&quot;: 9, },
    { &quot;label&quot;: &quot;100B&quot;, &quot;value&quot;: 10, },
    { &quot;label&quot;: &quot;iPad 11&quot;, &quot;value&quot;: 11, },
    { &quot;label&quot;: &quot;100B&quot;, &quot;value&quot;: 12, }
];
var svg = d3.select(&#39;#chart&#39;)
    .append(&quot;svg&quot;)
    .data([data]);
svg.attr(&quot;width&quot;, w)
    .attr(&quot;height&quot;, h)
    .style({ &quot;width&quot;: w + &quot;px&quot;, &quot;height&quot;: h + &quot;px&quot; });

var container = svg.append(&quot;g&quot;)
    .attr(&quot;class&quot;, &quot;chartholder&quot;)
    .attr(&quot;width&quot;, w)
    .attr(&quot;height&quot;, h)
    .attr(&quot;transform&quot;, &quot;translate(&quot; + (w / 2) + &quot;,&quot; + (h / 2) + &quot;)&quot;);
var vis = container
    .append(&quot;g&quot;)
    .attr(&quot;class&quot;, &quot;chartInner&quot;);


var increase = Math.PI * 2 / data.length; //length in radians of each slice/field
var half = increase / 2; //half length for offset



var pie = d3.pie().sort(null).value(function(d) { return 1; });
// declare an arc generator function
var arc = d3.arc()
    .outerRadius(r)
    .startAngle(function(d, i) {
        return (i * increase) - half;
    })
    .endAngle(function(d, i) {
        return (increase * (i + 1)) - half;
    });


var pies = vis.selectAll(&quot;g.pie-slice&quot;)
    .data(pie)
    .enter()
    .append(&quot;g&quot;)
    .attr(&quot;class&quot;, &quot;pie-slice&quot;)


pies.append(&quot;path&quot;)    
    .attr(&quot;fill&quot;, function(d, i) { return i % 2 ? &quot;red&quot; : &quot;blue&quot;; })
    .attr(&quot;d&quot;, function(d, i) { return arc(d, i); }); //returns NaN in the end
   

i'm expecting normally shaped pies on the spin wheel with d3.js version5/7

答案1

得分: 2

It seems like you need to set an innerRadius specifically now.

var arc = d3.arc()
    .innerRadius(0)
    .outerRadius(r)
    .startAngle(function(d, i) {
        return (i * increase) - half;
    })
    .endAngle(function(d, i) {
        return (increase * (i + 1)) - half;
    });

For more details, you can refer to the documentation here: d3.arc.

You can also check out this JSFiddle for an example: JSFiddle.

英文:

Looks like you need to specifically set an innerRadius now

var arc = d3.arc()
.innerRadius(0)
.outerRadius(r)
.startAngle(function(d, i) {
    return (i * increase) - half;
})
.endAngle(function(d, i) {
    return (increase * (i + 1)) - half;
});

https://github.com/d3/d3-shape#arcs

https://jsfiddle.net/Ltk2ueas/

huangapple
  • 本文由 发表于 2023年4月19日 16:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定