简体   繁体   English

使用D3.js绘制方形矩阵

[英]Drawing A square Matrix using D3.js

I have some data that looks like [ [30 elements], [30 elements], [30 elements]... ] this array can contain any number of arrays of 1-31 elements. 我有一些看起来像[[30个元素],[30个元素],[30个元素] ......的数据...这个数组可以包含1-31个元素的任意数量的数组。 The inner arrays will always be the same as the represent the number of days that will be processed. 内部数组将始终与表示将要处理的天数相同。 Each inner array will also have its on label, so it would like like 每个内部数组也都有它的on标签,所以它就像

A | A | 30 element array 30个元素数组
B | B | 30 element array 30个元素数组
C | C | 30 element array 30个元素数组

The 30 element Array will just be svg rects i draw on the screen, so really it will look like 30元素阵列将只是我在屏幕上绘制的svg rects,所以它看起来真的很像

A | A | rect rect rect rect... (1-31 rects) 直肠直肠...(1-31岁)
B | B | rect rect rect rect... (1-31 rects) 直肠直肠...(1-31岁)
C | C | rect rect rect rect... (1-31 rects) 直肠直肠...(1-31岁)

naturally I'm new to d3.js so I'm having problems and I think its because I don't understand how to use data() properly. 自然我是d3.js的新手,所以我遇到了问题而我认为是因为我不理解如何正确使用data() Right now I'm doing something like this 现在我正在做这样的事情

.data(d3.range( d3.range(d[0].length).length) * d3.range(d.length).length )
//this is to know how many rectangles I need to draw

so the first term is the number of elements for an inner array and the second term is the number of inner arrays. 所以第一项是内部数组的元素数,第二项是内部数组的数。

I'm not using scales because I could not position the rects properly, so I guess I don't understand those either. 我没有使用秤,因为我无法正确定位,所以我想我也不理解它们。 to give you and idea on what I'm doing to position them for the x and y attribute I'm doing stuff like this. 给你一个想法,我正在做什么来定位他们的x和y属性我正在做这样的事情。

daysProcessed = d[0].length
uniqueLabels  = d3.range(d.length).length

x = svgwidth/(daysProcessed +xmargin) * i%dayProcessed +xmargin // i is from  .attr("x",function(d, i)


y = rectSize *(i%uniqueLables)

so as you can imagine this is a mess can anyone help me with a simpler way and tell me the proper way to use .data on a square matrix? 所以你可以想象这是一团糟,任何人都可以用更简单的方式帮助我,并告诉我在方阵上使用.data的正确方法吗?

I think that generating the data before trying to draw the rectangles is a better approach. 我认为在尝试绘制矩形之前生成数据是一种更好的方法。 You can use d3.range to generate the data matrix: 您可以使用d3.range生成数据矩阵:

// Setup
var width = 300,
    height = 300,
    div = d3.select('#chart'),
    svg = div.append('svg')
        .attr('width', width)
        .attr('height', height),
    rw = 95,
    rh = 95;  

Where rw and rh are the width and height of the rectangles. 其中rw和rh是矩形的宽度和高度。 Then, you can create the data matrix: 然后,您可以创建数据矩阵:

var data = [];
for (var k = 0; k < 3; k += 1) {
    data.push(d3.range(3));
}

To actually draw the rectangles, you can bind the elements of the data array (the inner arrays) to groups in svg, and translate the groups based in the row number, adding a small margin: 要实际绘制矩形,您可以将数据数组的元素(内部数组)绑定到svg中的组,并根据行号转换组,添加一个小的边距:

// Create a group for each row in the data matrix and translate the 
// group vertically
var grp = svg.selectAll('g')
    .data(data)
    .enter()
    .append('g')
    .attr('transform', function(d, i) {
        return 'translate(0, ' + (rh + 5) * i + ')';
    });

Then, for each group, you can add the rectangles, adjusting the horizontal position only (the vertical position was already set in the containing group). 然后,对于每个组,您可以添加矩形,仅调整水平位置(垂直位置已在包含组中设置)。 Here, you need to bind the inner array elements (the group datum) to each rectangle: 在这里,您需要将内部数组元素(组基准)绑定到每个矩形:

// For each group, create a set of rectangles and them to the inner array
// (the inner array is binded to the group)
grp.selectAll('rect')
    .data(function(d) { return d; })
    .enter()
    .append('rect')
        .attr('x', function(d, i) { return (rw + 5) * i; })
        .attr('width', rw)
        .attr('height', rh);

I would recomend you to read How Selections Work . 我建议你阅读选择如何工作 Also, I wrote a small jsfidlle with this code: http://jsfiddle.net/pnavarrc/Sg3BY/1/ 另外,我用这段代码写了一个小jsfidlle: http//jsfiddle.net/pnavarrc/Sg3BY/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM