Getting “Error, too late ” for D3 transition

巧了我就是萌 提交于 2019-12-13 17:37:09

问题


I am trying to do three transitions in onClick event of a Pie Chart. The first transition works but the second and the third one fail. I understood from Mike Bostocks comment on a similar question that "This means you are trying to modify a transition that has already started, or derive a transition from one that has ended. Please read the API Reference section on transition life cycles."

I still cant seem to understand the reason why this is happening. Here is the relevant code:

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);
        }
    });

What is the issue here?


回答1:


The problem was that the primaryLabel and secondaryLabel I was trying to do transition on were not assigned correctly. I had the following assignment:

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);
        }
    });

As a result of this, the variables contained the reference to the transitions and not the labels themselves as when you chain transitions it gets changed to a transition type object. So I changed that to this:

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);
        }
    });

It all works now! :)



来源:https://stackoverflow.com/questions/45223993/getting-error-too-late-for-d3-transition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!