gpt4 book ai didi

javascript - Angular D3 理解 attrTween 函数

转载 作者:搜寻专家 更新时间:2023-10-30 21:33:56 26 4
gpt4 key购买 nike

我想了解如何在 D3 中使用函数 attrTween。我正在尝试根据以下示例实现饼图:http://bl.ocks.org/mbostock/5100636 .

但是当我到达过渡部分时遇到了问题。

  private populateGauge(): void {
const innerRadius = this.radius - 50;
const outerRadius = this.radius - 10;
const arc = d3
.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius)
.startAngle(0);

const background = this.svg
.append('path')
.datum({ endAngle: this.tau })
.style('fill', '#ddd')
.attr('d', arc);
this.myEndAngle = { endAngle: (this.gaugeData.value / 100) * this.tau };
const foreground = this.svg
.append('path')
.datum(this.myEndAngle)
.style('fill', 'orange')
.attr('d', arc);

foreground
.transition()
.duration(1500)
.attrTween('d', function(newAngle) {
return function(d) {
const interpolate = d3.interpolate(d.endAngle, newAngle);
return function(t) {
d.endAngle = interpolate(t);
return arc(d);
};
};
});
}

error

我一直在尝试使用简单的基本情况,并且只在插值函数中使用 0,但我无法摆脱最后一个 return 语句抛出的最终错误 (return arc(d));

Argument of type 'number' is not assignable to parameter of type 'DefaultArcObject'.

我如何解决这些问题?如果您需要提供更多信息,请告诉我。

最佳答案

attrTween('d',...) 采用返回另一个函数的单个函数。您将一个函数传递给它,该函数以当前数据索引 和当前节点 作为参数调用。此函数应返回以时间 值调用的插值函数。

当我查看您的源代码时。您有 3 个嵌套函数,这是不正确的。

您需要将开始结束 Angular 作为基准 的值,并且不应改变补间函数中的数据

我更喜欢在 tween 函数之外创建一个 arc 函数,然后将其用于我的插值。这更有效,因为您不是每次都创建一个新的 arc 函数。

const myArc = d3.arc();
// ^^^ call methods of arc() to configure settings that won't animate.

foreground
.transition()
.duration(1500)
.attrTween('d', (d) => {
return (t) => {
const angle = d3.interpolate(d.endAngle, d.newAngle)(t);
// ^^^ interpolate datum values using time.
myArc.startAngle(angle);
// ^^^ call methods of arc() to configure what you need.
return myArc(null);
// ^^^ call arc function to render "d" attribute.
};
};
});

我发现使用节点包“@types/d3”更容易

npm install @types/d3 --save-dev

然后您可以将这些类型导入到 TypeScript 文件中

import * as d3 from 'd3';

如果你有像 WebStorm 这样的 IDE。您可以在 D3 函数上按 CTRL+CLICK 并查看类型定义和注释。

关于javascript - Angular D3 理解 attrTween 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54852791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com