为什么 “d” 在 attrTween 中未定义?

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

Why "d" is undefined in attrTween?

问题

以下是您代码的翻译:

我正在尝试在d3上创建一个界面我找到了弧形动画代码http://bl.ocks.org/mbostock/5100636),我想稍微修改它(不使用arcTween()函数)。
这是我的代码的一部分
```javascript
var arc3 = d3.arc().innerRadius(77).outerRadius(90).startAngle(0);
var foregroundArc3 = imgs3
  .append('path')
  .attr('d', arc3)
  .attr('stroke', 'white')
  .attr('fill', 'white')
  .style('fill-opacity', 0.5)
  .attr('transform', 'translate(' + (146.5) + ',' + (150.9) + ')')
  .datum({ endAngle: 0.0628 * 90, newAngle: 0.0628 * 270 })
  .transition()
  .duration(1500)
  .attrTween('d', (d) => {
    return (t) => {
      const interpolate = d3.interpolate(d.endAngle, d.newAngle);
      d.endAngle = interpolate(t);
      return arc3(d);
    };
  });

但出现错误:类型“{ endAngle: number; newAng: number; }”的参数无法分配给类型“DefaultArcObject”。类型“{ endAngle: number; newAng: number; }”中缺少属性“innerRadius”。

并且在浏览器的控制台中,“d”未定义。我尝试寻找其他可行的解决方案,但没有成功。


希望这有助于您解决代码中的问题。

<details>
<summary>英文:</summary>

I am trying to create an interface on d3. I found the arc animation code  (http://bl.ocks.org/mbostock/5100636), and I want to change it a bit (without function arcTween()). 
Here is part of my code:
        var arc3 = d3.arc().innerRadius(77).outerRadius(90).startAngle(0);
        var foregroundArc3 = imgs3
          .append(&#39;path&#39;)
          .attr(&#39;d&#39;, arc3)
          .attr(&#39;stroke&#39;, &#39;white&#39;)
          .attr(&#39;fill&#39;, &#39;white&#39;)
          .style(&#39;fill-opacity&#39;, 0.5)
          .attr(&#39;transform&#39;, &#39;translate(&#39;+(146.5)+&#39;,&#39;+(150.9)+&#39;)&#39;)
          .datum({endAngle: 0.0628*90, newAngle: 0.0628*270})
          .transition()
          .duration(1500)
          .attrTween(&#39;d&#39;,  (d) =&gt; {
            return (t) =&gt; {
              const interpolate = d3.interpolate(d.endAngle, d.newAngle);
                d.endAngle = interpolate(t);
                return arc3(d);
              };
      });
But there error: Argument of type &#39;{ endAngle: number; newAng: number; }&#39; is not assignable to parameter of type &#39;DefaultArcObject&#39;. Property &#39;innerRadius&#39; is missing in type &#39;{ endAngle: number; newAng: number; }&#39;. 

And in browser&#39;s console &quot;d&quot; is undefinded. I tried to find any other working solutions, but to no avail.




</details>


# 答案1
**得分**: 1

When you do this...

.attr('d', arc3)

... the second argument of `attr`, which is the `arc3` function, gets passed the bound datum, the index, and the node's group.

However, there is no bound datum, since in your code the `datum` method comes **after** that.

The simplest solution is just moving the `datum` to before the `.attr('d', arc3)` line:

```javascript
var svg = d3.select("svg");
var arc3 = d3.arc().innerRadius(77).outerRadius(90).startAngle(0);
var foregroundArc3 = svg
  .append('path')
  .datum({
    endAngle: 0.0628 * 90,
    newAngle: 0.0628 * 270
  })
  .attr('d', arc3)
  .attr('fill', 'teal')
  .style('fill-opacity', 0.5)
  .attr('transform', 'translate(' + (146.5) + ',' + (150.9) + ')')
  .transition()
  .duration(1500)
  .attrTween('d', (d) => {
    return (t) => {
      const interpolate = d3.interpolate(d.endAngle, d.newAngle);
      d.endAngle = interpolate(t);
      return arc3(d);
    };
  });
<svg></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
英文:

When you do this...

.attr(&#39;d&#39;, arc3)

... the second argument of attr, which is the arc3 function, get's passed the bound datum, the index and the node's group.

However, there is no bound datum, since in your code the datum method comes after that.

The simplest solution is just moving the datum to before the .attr(&#39;d&#39;, arc3) line:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

var svg = d3.select(&quot;svg&quot;);
var arc3 = d3.arc().innerRadius(77).outerRadius(90).startAngle(0);
var foregroundArc3 = svg
  .append(&#39;path&#39;)
  .datum({
    endAngle: 0.0628 * 90,
    newAngle: 0.0628 * 270
  })
  .attr(&#39;d&#39;, arc3)
  .attr(&#39;fill&#39;, &#39;teal&#39;)
  .style(&#39;fill-opacity&#39;, 0.5)
  .attr(&#39;transform&#39;, &#39;translate(&#39; + (146.5) + &#39;,&#39; + (150.9) + &#39;)&#39;)
  .transition()
  .duration(1500)
  .attrTween(&#39;d&#39;, (d) =&gt; {
    return (t) =&gt; {
      const interpolate = d3.interpolate(d.endAngle, d.newAngle);
      d.endAngle = interpolate(t);
      return arc3(d);
    };
  });

<!-- language: lang-html -->

&lt;svg&gt;&lt;/svg&gt;
&lt;script src=&quot;https://d3js.org/d3.v5.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 22:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613768.html
匿名

发表评论

匿名网友

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

确定