如何在D3中检索嵌套数据集的键值 [英] How to retrieve the key values of a nested data set in D3

查看:89
本文介绍了如何在D3中检索嵌套数据集的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将D3可视化添加到OBIEE,而我需要完成的第一个图形是多系列折线图.数据是直接从OBIEE叙述视图中获取的,格式如下:

I am trying to add D3 visualizations to OBIEE and the first graph I need to accomplish is a multi-series line graph. The data is directly obtained from an OBIEE narrative view in this format:

var data = [ ];

data.push({category:"Cat1",date:"20130101",suma:9.11});
data.push({category:"Cat2",date:"20130101",suma:2.66});
data.push({category:"Cat3",date:"20130101",suma:18.00});
data.push({category:"Cat4",date:"20130101",suma:32.49});
data.push({category:"Cat5",date:"20130101",suma:37.74});

有155条这样的行,它们的日期从2013年到2015年不等.要按类别将它们分隔开,因此我可以为每个类别分配一行和颜色,我将数据嵌套:

There are 155 lines like those, for different dates ranging from 2013 to 2015. To separate them by categories, so I can then assign one line and color to each category, I nest the data in this way:

var dataGroup = d3.nest() 
.key(function(d) {return d.category;}) 
.entries(data);

然后,变量dataGroup是一个由5个对象组成的数组,如下所示:

The variable dataGroup is then an array of 5 objects that looks like this:

0: Object
key: "Cat1"
values: Array[31]
    0: Object
       category: "Cat1"
       date: "20130101"
       suma: 9.11
       __proto__: Object
   ... 
   1: Object
   key: "Cat2"
   values: Array[31]
       0: Object
       category: "Cat2"
       date: "20130101"
       suma: 2.66
       __proto__: Object
   ... ...

下一步,我要为类别指定颜色.

What I am trying to do next is to assign the colors for the categories.

var color = d3.scale.category10();
color.domain(d3.keys(dataGroup).filter(function(key) { return key !== "date"; }));

这是我遇到麻烦的地方.此过滤器功能的结果是:

And here is where I am having trouble. The result of this filter function is:

Array[5]
0: "0"
1: "1"
2: "2"
3: "3"
4: "4"
length: 5
__proto__: Array[0]

而不是我认为的是:

Array[5]
0: "Cat1"
1: "Cat2"
2: "Cat3"
3: "Cat4"
4: "Cat5"
length: 5
__proto__: Array[0]

我尝试了几种方法,但都没有奏效.在这一点上,尽管我真的很想实现这一点,但我将放弃将D3添加到OBIEE中的过程,但是我正在努力理解这一点,而且我似乎完全无法做到.也许我应该首先学习Javascript,也许首先D3似乎需要花很多精力才能理解数据管理.

I have tried several approaches and none of them have worked. At this point I am about to give up on adding D3 to OBIEE, although I really wanted to achieve it, but I am struggling to understand this and I seem to be totally unable. Perhaps I should be learning Javascript first of perhaps all the mind bending that D3 seems to require to understand the data management is too much for me.

在此方面,我将不胜感激.很抱歉,如果在提交问题时遇到任何错误,我试图正确设置其格式,但这是我第一次在此处发布内容.

I would really appreciate any help on this. I am sorry if I made any mistakes in submitting the question, I tried to format it correctly, but this is the first time I post anything here.

非常感谢您, 安娜.

推荐答案

keys()方法(不适用于nest()的一部分)不适用于标准关联数组,而不是嵌套数据.您应该使用dataGroupvalues并遍历每个元素以提取category属性.您的代码中可能还有其他问题,但是请尝试

The keys() method (not when used as part of nest()) is intended for standard associative arrays, not nested data. You should take the values of dataGroup and iterate through each to extract the category property. There may be additional problems in your code, but try

color.domain(d3.values(dataGroup).map(function(d) {
    return d.category; 
}).filter(function(key) { return key !== "date"; }));

开始

这篇关于如何在D3中检索嵌套数据集的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆