【发布时间】: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