简体   繁体   English

使用d3.js处理多维数据数组

[英]Processing a multidimensional data array with d3.js

Currently I am learning some "D3.js" and attempting to get my head around the way data is processed and selected. 目前,我正在学习一些“ D3.js”,并试图了解处理和选择数据的方式。

I'm stuck on the following task I've created for myself. 我坚持自己为自己创建的以下任务。

Ideally, I want something that is functionally equivalent to: 理想情况下,我需要功能上等效于:

    <svg>
        <circle r=​"20.5" cx=​"100" cy=​"200">​</circle>​
        <circle r=​"20.5" cx=​"300" cy=​"10">​</circle>​
    </svg>

What I have currently (with my logic) is: 我目前(根据​​我的逻辑)是:

    var matrix = [ [{ "x": 100, "y": 200 }], [{ "x": 300, "y": 10 }]];

    var result = d3.select("body").append("svg")  // Append SVG to end of Body
       .data(matrix) // select this data
       .selectAll("g") //g is a svg grouping tag
       .data(function (d) { return d; }) //Unwrap the first part of the array
       .enter() // Grab all the data that is new to the selection in each array
       .selectAll("g") 
       .data(function (d) { return d;}) // foreach each item inside the 1D array
       .enter() // For all the data that doesn't exist already in the SVG
       .append("circle") // Append Circle to the DOM with the following attributes
       .attr("r", 20.5)
       .attr("cx", function (d) { return d.x; })
       .attr("cy", function (d) { return d.y; });
    };

Weirdly enough the following : 以下内容很奇怪:

 var result = d3.select("body").append("svg")
        .data(matrix)
        .selectAll("g")         
        .enter()            
        .append("circle")
        .attr("r", 20.5)
        .attr("cx", function (d) { return d.x; })
        .attr("cy", function (d) { return d.y; });
    };

Seems somehow able to get the first item in the array but doesn't iterate correctly. 似乎能够以某种方式获取数组中的第一项,但未正确迭代。 I'm not quite sure how it's entering the array. 我不太确定它是如何进入数组的。

D3 seems to be quite a big step away from the programming paradigms I'm used to, and more difficult to debug so it would be awesome if someone could explain where I'm going wrong. D3似乎比我惯用的编程范例大了一步,并且调试起来更加困难,因此如果有人可以解释我要去哪里,那真是太棒了。

Oh, and while the example is quite useless and I could flatten it using the merge command - for the purposes of fully understanding D3 manipulation. 哦,虽然该示例没有用,但我可以使用merge命令将其展平-以便完全理解D3操作。 I'd like to draw the couple of circles without the merge :) 我想画几个圆圈而不合并:)

Thanks! 谢谢!

Seeing you mention that you're new to d3 I'll make a few comments on the basics. 看到您提到您是d3的新手,我将在基础知识上做一些评论。

The first is that we're trying to place some svg elements on the DOM, so first we have to have a svg canvas to work on. 首先是我们要在DOM上放置一些svg元素,因此首先我们必须有一个svg画布来处理。 Typically its set up early in the code and looks something like this: 通常,它是在代码的早期设置的,看起来像这样:

var svg = d3.select("body")
             .append("svg")
             .attr("width", 350)
             .attr("height", 250);

Note that it would be best to define variables for height and width (but I'm being lazy). 请注意,最好为高度和宽度定义变量(但是我很懒)。

So now you have your canvas lets look at how d3 iterates. 现在,让您的画布让我们看看d3的迭代方式。 d3 iterates over an array, so you don't have to have an array within an array for your example as in: d3遍历数组,因此您的示例中不必在数组中有一个数组,如下所示:

 var matrix = [ { "x": 100, "y": 200 }], [{ "x": 300, "y": 10 }];

Now you're second block of code is almost there, with just a bit of rearrangement. 现在,您几乎已经有了第二个代码块,仅需重新排列一下。 The first thing we need to do is t create placeholders for the circles in your svg canvas using svg.selectAll("circle") . 我们需要做的第一件事是使用svg.selectAll("circle")在svg画布中为圆创建占位符。 Next we introduce the data to the empty placeholders using data(matrix) and this is bound using 'enter()`. 接下来,我们使用data(matrix)将数据介绍给空的占位符,并使用'enter()`将其绑定。 Now all we have to do is append the circles and give them some attributes which is all the rest of the code does 现在,我们要做的就是追加圆圈,并为它们提供一些属性,其余的代码都会这样做

svg.selectAll("circle")
     .data(matrix)
     .enter()
     .append("circle")
     .attr("r", 20.5)
     .attr("cx", function (d) {
         return d.x;
     })
     .attr("cy", function (d) {
         return d.y;
     });

You can see this all put together in this fiddle with some minor changes. 你可以看到这一切放在一起在这个捣鼓了一些细微的变化。

If you want to get to know d3 I'd really recommend get Scott Murray book on d3 it's an excellent introduction 如果您想了解d3,我真的建议您阅读d3上的Scott Murray书,这是一个很好的介绍

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

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