我正在尝试在饼图的onClick事件中进行三次转换。第一次转换有效但第二次和第三次转换失败。我理解Mike Bostocks对类似问题的评论“这意味着您正在尝试修改已经开始的转换,或者从已经结束的转换中获得转换。请阅读有关转换生命周期的API参考部分。 “
我似乎无法理解为什么会这样。 以下是相关代码:
self.primaryLabelText = self.arc.append("text")
.on("click", function (d: any) {
console.log("About to send::::" + getStudyLabel(d.index));
self.selectedIndustryTypeService.sendMessage(getStudyLabel(d.index));
self.showDialog();
// The amount we need to rotate:
var rotate = 180-(d.startAngle + d.endAngle)/2 / Math.PI * 180;
// Transition the pie chart
g.transition()
.attr("transform", "translate(" + self.width / 2 + "," + self.height / 2 + ") rotate(" + rotate + ")")
.duration(1000);
// Τransition the labels:
self.primaryLabelText.transition()
.attr("transform", function(dd: any) {
return "translate(" + label.centroid(dd) + ") rotate(" + (-rotate) + ")"; })
.duration(1000);
self.secondaryLabelText.transition()
.attr("transform", function(dd: any) {
return "translate(" + label.centroid(dd) + ") rotate(" + (-rotate) + ")"; })
.duration(1000);
})
.transition()
.duration(750)
.attr("transform", function (d: any) {
return "translate(" + label.centroid(d) + ")";
})
.attr("dy", "-0.75em")
.attr("font-family", "sans-serif")
.attr("font-size", "15px")
.attr("text-anchor", "middle")
.attr("class", "sponsor-pie-widget-label")
.text(function (d: any) {
if (d.endAngle - d.startAngle < 0.3) {
return "";
} else {
return getStudyLabel(d.index);
}
});
这是什么问题?
答案 0 :(得分:4)
问题是我尝试进行转换的primaryLabel和secondaryLabel没有正确分配。我有以下任务:
self.primaryLabelText = self.arc.append("text")
.on("click", function (d: any) {
self.rotateChart(d);
}).transition()
.duration(750)
.attr("transform", function (d: any) {
return "translate(" + self.label.centroid(d) + ")";
})
.attr("dy", "-0.75em")
.attr("font-family", "sans-serif")
.attr("font-size", "15px")
.attr("text-anchor", "middle")
.attr("class", "sponsor-pie-widget-label")
.text(function (d: any) {
if (d.endAngle - d.startAngle < 0.3) {
return "";
} else {
return getStudyLabel(d.index);
}
});
因此,变量包含对过渡的引用而不是标签本身,因为当链转换时它变为过渡类型对象。所以我改变了这个:
self.primaryLabelText = self.arc.append("text")
.on("click", function (d: any) {
self.rotateChart(d);
});
self.primaryLabelText.transition()
.duration(750)
.attr("transform", function (d: any) {
return "translate(" + self.label.centroid(d) + ")";
})
.attr("dy", "-0.75em")
.attr("font-family", "sans-serif")
.attr("font-size", "15px")
.attr("text-anchor", "middle")
.attr("class", "sponsor-pie-widget-label")
.text(function (d: any) {
if (d.endAngle - d.startAngle < 0.3) {
return "";
} else {
return getStudyLabel(d.index);
}
});
现在一切正常! :)