如何使用d3.js访问对象中的键值? [英] How to access key values in an object using d3.js?

查看:88
本文介绍了如何使用d3.js访问对象中的键值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为数据集"的数组,其中每个条目都是具有键和值属性的对象,例如:

I have an array named "dataset" where each entry is an object with a key and value attribute, such as the following:

dataset= 
 [
 {"key":"alpha", "value": [ {}, { } ...]},
 {"key":"beta", "value": [ { }, { } ...]},
 {"key":"gamma", "value": [ {}, { } ...]},
 {"key":"delta", "value": [ { }, { } ...]}
 ];
 dataset.domain:[];
 dataset.query:[];
 dataset.range:[];

我正在使用D3.JS,正在使用此数据集在堆积面积图中绘制alpha,beta,gamma和delta值.如何使用color.domain()为每个键"分配不同的颜色?

I am working with D3.JS and I am using this dataset to plot in an stacked area chart the alpha, beta, gamma, and delta values. How can I use color.domain() to assign a different color to each "key"?

推荐答案

要创建一个包含数据数组中每个对象一个属性的数组,请使用

To create an array that contains one property from each object in your data array, use an array mapping function:

dataset=  [
 {"key":"alpha", "value": [ {}, { } ...]},
 {"key":"beta", "value": [ { }, { } ...]},
 {"key":"gamma", "value": [ {}, { } ...]},
 {"key":"delta", "value": [ { }, { } ...]}
 ];

domain = dataset.map(function(o){return o.key});
/* domain is now ["alpha", "beta", "gamma", "delta"] */

这篇关于如何使用d3.js访问对象中的键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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