【问题标题】:style transitions don't seem to work when called from a setInterval function (D3.js)从 setInterval 函数 (D3.js) 调用时,样式转换似乎不起作用
【发布时间】:2018-08-23 20:27:36
【问题描述】:
function updateGrid() {
//this for loop moves the matrix and sets a position 
    for (var row = row_start; row < row_end; row+=5) {
        for (var i = 0; i < 5; i++) {
            //these for loops call each individual rect on the grid by a unique id.
            d3.select("#x-"+row+"y-"+(i*20))
                .transition()
                .style("fill", function(d) {
                    var alt = 0;
                    switch (i) {
                        //ignore this switch statement. It's just reversing the i because the tag is backwards.
                        case 0: alt = 4; break;
                        case 1: alt = 3; break;
                        case 3: alt = 1; break;
                        case 4: alt = 0; break;
                        default: alt = 2; break;
                    }
                    //color_matrix is an object. row is the key, and alt is the position of the array that is the key value.
                    if (color_matrix[row][alt] == 0) return colors[0]; 
                    if (color_matrix[row][alt] == 1) return colors[1]; 
                    if (color_matrix[row][alt] == 2) return colors[2]; 
                    if (color_matrix[row][alt] == 3) return colors[3]; 
                    if (color_matrix[row][alt] >= 4) return colors[4]; 
                })
                .duration(750);
        }
    }
    //this moves the matrix by one column. console.log() shows that it works.
    row_start += 5;
    row_end += 5;
}

var run = window.setInterval(function() { updateGrid() }, 5000);

所以我似乎无法弄清楚这一点。首先要注意几点:
1. 颜色在矩形初始化中完美运行。
2. 过渡第一次也能正常工作。
3. 函数调用正确,因为我用 console.log() 测试过。
4. 矩阵/对象也正确循环。
5. 持续时间的位置似乎并不重要。

唯一不能正常工作的是颜色变化。此更新函数将每五秒调用一次以更改矩形的颜色。但是,它似乎不起作用。调用转换的方式有问题吗?

【问题讨论】:

    标签: javascript d3.js transition


    【解决方案1】:

    原来矩阵毕竟没有正确循环。 for 循环试图在没有的地方循环 rect,为了修复它,我添加了一个单独的增量。

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 2015-06-29
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多