0

我一直致力于使用 D3.js 创建一个圆环图。我一直在关注此处列出的示例。一切都很顺利,直到我到达关于从甜甜圈切片绘制到文本标签的折线部分。我已经完全复制了示例代码,而在他们的示例中,它们得到了很好的线条,我得到了三角形,如图所示

所以,除了它不能正常工作之外,我必须承认我并不真正理解代码的折线部分。这使得我什至不知道从哪里开始寻找问题的原因。下面是我项目中的折线绘图代码。任何帮助或见解表示赞赏。

var polyline = chart.select(".lines").selectAll("polyline")
                        .data(pie(data), key);

                    polyline.enter()
                        .append("polyline");

                    polyline.transition().duration(1000)
                        .attrTween("points", function(d){
                            this._current = this._current || d;
                            var interpolate = d3.interpolate(this._current, d);
                            this._current = interpolate(0);
                            return function(t) {
                                var d2 = interpolate(t);
                                var pos = outerArc.centroid(d2);
                                pos[0] = radius * 0.95 * (midAngle(d2) < Math.PI ? 1 : -1);
                                return [arc.centroid(d2), outerArc.centroid(d2), pos];
                            };          
                        });

                    polyline.exit().remove();

编辑

所以,像往常一样,一旦我屈服并提出这个问题,我就发现出了什么问题。在我链接到的示例中,他们使用样式表将填充属性设置为无。虽然这可行,但我宁愿它留在 D3 代码中。对于任何感兴趣的人,这就是代码的样子。

var polyline = chart.select(".lines").selectAll("polyline")
                        .data(pie(data), key);

                    polyline.enter()
                        .append("polyline")
                        .style("fill", "none")
                        .style("stroke-width", "2px")
                        .style("stroke", "black")
                        .style("opacity", "0.4");

                    polyline.transition().duration(500)
                        .attrTween("points", function(d){
                            this._current = this._current || d;
                            var interpolate = d3.interpolate(this._current, d);
                            this._current = interpolate(0);
                            return function(t) {
                                var d2 = interpolate(t);
                                var pos = outerArc.centroid(d2);
                                pos[0] = radius * 0.95 * (midAngle(d2) < Math.PI ? 1 : -1);
                                return [arc.centroid(d2), outerArc.centroid(d2), pos];
                            };          
                        });

                    polyline.exit().remove();
4

1 回答 1

0

So, as per usual once I gave in and asked this question I found out what was wrong. In the example I linked to they use a stylesheet to set the fill property to none. While that works I would rather it stayed in the D3 code. For any interested this is what the code would look like.

var polyline = chart.select(".lines").selectAll("polyline")
                        .data(pie(data), key);

                    polyline.enter()
                        .append("polyline")
                        .style("fill", "none")
                        .style("stroke-width", "2px")
                        .style("stroke", "black")
                        .style("opacity", "0.4");

                    polyline.transition().duration(500)
                        .attrTween("points", function(d){
                            this._current = this._current || d;
                            var interpolate = d3.interpolate(this._current, d);
                            this._current = interpolate(0);
                            return function(t) {
                                var d2 = interpolate(t);
                                var pos = outerArc.centroid(d2);
                                pos[0] = radius * 0.95 * (midAngle(d2) < Math.PI ? 1 : -1);
                                return [arc.centroid(d2), outerArc.centroid(d2), pos];
                            };          
                        });

                    polyline.exit().remove();
于 2014-06-23T14:09:48.570 回答