简体   繁体   English

在d3中访问不同级别的嵌套列表

[英]Access different levels of nested lists in d3

Suppose I have a list with the following format: 假设我有一个具有以下格式的列表:

dataset = [["10", "asdf"], ["20", "jkl"], ["30", "bla"]]

I want to append text for each individual character in the second element of each nested list. 我想在每个嵌套列表的第二个元素中为每个单独的字符添加文本。 I also want the x position of each letter to be determined by the first element of the list. 我还希望每个字母的x位置由列表的第一个元素确定。 So, for example, the x position of a should be 10 , for s - 11 , d - 12 ... for j - 20 , for k - 21 , and so forth. 因此,例如,中的x位置a应该是10 ,对于s - 11d - 12 ...为j - 20 ,用于k - 21 ,等等。

In the code I have so far, I only show the first letter, because I'm not iterating through the correct portion of the list: 在到目前为止的代码中,我只显示第一个字母,因为我没有遍历列表的正确部分:

chartBody.selectAll("exampletext")
                       .data(dataset)
                       .enter()
                       .append("text")
                       .attr("class", "exampletext")
                       .text(function(d) {
                            for (var i=0; i <= d[1].length; i++)
                                {
                                    return d[1][i];
                                }
                       })
                       .attr("x", function(d) {
                            for (var i=0; i <= d[1].length; i++)
                                {
                                    return xScale(+d[0]) + i; 
                                }
                       })
                       .attr("y", h/2);

I don't know why you need this (it's really hard to read that output) but here it is: http://jsfiddle.net/uNQA9/ . 我不知道您为什么需要它(很难读取该输出),但是这里是: http : //jsfiddle.net/uNQA9/

The important change was to build the list of letters and positions prior to feeding to d3: 重要的更改是在馈入d3之前建立字母和位置的列表:

var dataset, h, i, letter, pair, r, viewport, xScale, _i, _j, _len, _len1, _ref;

dataset = [["10", "asdf"], ["20", "jkl"], ["30", "bla"]];

r = [];    
i = 0;

/*loop each pair, push into a new array */


for (_i = 0, _len = dataset.length; _i < _len; _i++) {
  pair = dataset[_i];
  i = 0;
  _ref = pair[1];
  for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
    letter = _ref[_j];
    r.push([+pair[0] + i, letter]);
    i++;
  }
}

That JS is ugly (but uber compliant) because I got it from this bit of (nicely readable) coffeescript: 该JS丑陋(但符合uber规范),因为我是从这部分(易读的)coffeescript中获得的:

dataset = [["10", "asdf"], ["20", "jkl"], ["30", "bla"]]
r = []
i = 0 
#loop each pair, push into a new array
for pair in dataset
  i = 0
  for letter in pair[1]
    r.push([+(pair[0]) + i, letter]) 
    i++ 

I made a slightly more readable version here: http://jsfiddle.net/UtUeP/ . 我在这里制作了一个更具可读性的版本: http : //jsfiddle.net/UtUeP/ If you want to pump up the difference between the letters, check out the d3 colors functions like: d3.scale.category10() : https://github.com/mbostock/d3/wiki/Ordinal-Scales 如果您想增加字母之间的差异,请查看d3颜色函数,例如: d3.scale.category10()https : //github.com/mbostock/d3/wiki/Ordinal-Scales

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

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