我正在尝试在d3.js中构建一个矩形网格.
网格为7行(一周中的天数)和24列(一天中的小时数).
以下代码仅绘制(行:列):day0:hour0,day1:hour1,day2:hour2,day3:hour3,day4:hour4,day5:hour5,day6:hour6,day7:hour7
问题:为什么以下代码不起作用的任何想法?
/**
* calendarWeekHour Setup a week-hour grid:
* 7 Rows (days), 24 Columns (hours)
* @param id div id tag starting with #
* @param width width of the grid in pixels
* @param height height of the grid in pixels
* @param square true/false if you want the height to
* match the (calculated first) width
*/
function calendarWeekHour(id, width, height, square)
{
var calData = randomData(width, height, square);
var grid = d3.select(id).append("svg")
.attr("width", …Run Code Online (Sandbox Code Playgroud) 我有一个二维数据数组,我想将其可视化为 D3 中的热图。
从我看到的例子中,包括:
看起来您必须将行和列编码为每个数据元素的属性。有没有办法利用数据的结构(即它是一个可以用 [i][j] 符号索引的二维数组)来避免对位置信息进行冗余编码?
如果没有,是否有一种简洁和/或有效的方法来生成对位置信息进行编码的数据结构?我的意思是嵌套 for 循环以外的其他内容:
myData = [[1, 2], [3, 4]]
myDataWithPos = []
nRow = myData.length
nCol = myData[0].length
for (i = 0; i < nRow; i++){
for(j = 0; j < nCol; j++){
myDataWithPos.push({val: myData[i][j], row: i, col: j})
}
}
Run Code Online (Sandbox Code Playgroud)