d3js迁移映射-问题重现示例

时间:2018-09-01 14:06:06

标签: javascript d3.js mapping

我正在尝试重新创建此示例: http://bl.ocks.org/cingraham/7663357#index.html

问题是我的结果如图所示。 Image

我得到的错误是:

  

TypeError:theState [0] [0]为空

但是由于我只是复制代码,所以为什么我什至会收到此错误?谁能帮我找出解决方法?

谢谢

编辑:

代码在这里(在下面)产生问题... 具体不要输入.attr("stroke-width", function(d, i) {

function clicked(selected) {

    var selname = selected.id;
    var homex = path.centroid(selected)[0];
    var homey = path.centroid(selected)[1];
    g.selectAll(".goingline")
        .attr("stroke-dasharray", 0)
        .remove()
    g.selectAll(".goingline")
        .data(going)
        .enter().append("path")
        .attr("class", "goingline")
        .attr("d", function(d, i) {
            var abb = d.abbrev;     
            var finalval = coming[i][selname] - going[i][selname];
            var theState = d3.select("#" + abb); 
            if (!isNaN(finalval)) {
                var startx = path.centroid(theState[0][0].__data__)[0];
                var starty = path.centroid(theState[0][0].__data__)[1];
                if (finalval > 0) {
                    return "M" + startx + "," + starty + " Q" + (startx + homex) / 2 + " " + (starty + homey) / 1.5 + " " + homex + " " + homey;
                } 
                else {
                    return "M" + homex + "," + homey + " Q" + (startx + homex) / 2 + " " + (starty + homey) / 2.5 + " " + startx + " " + starty;
                }
            }
        })
        .call(transition)
        .attr("stroke-width", function(d, i) {
            var finalval = coming[i][selname] - going[i][selname]; console.log("test1")
                return lineSize(parseFloat(Math.abs(finalval)));
        })
        .attr("stroke", function(d, i) {
            var finalval = coming[i][selname] - going[i][selname]; console.log("test1")
            if (finalval > 0) {
                return "#65a89d";
            }
            else {
                return "#a96a46";
            }
        })
        .attr("fill", "none")
        .attr("opacity", 0.5)
        .attr("stroke-linecap", "round")
        .on("mouseover", function(d){
            return toolOver2(d, this);
        })
        .on("mousemove", function(d, i){
            var m = d3.mouse(this);
            mx = m[0];
            my = m[1];
            return toolMove2(mx, my, selname, d.state, coming[i][selname],     going[i][selname]);
        })
        .on("mouseout", function(d) {
            return toolOut2(d, this);
        });
}

0 个答案:

没有答案