D3:序数刻度不适用于对象数组 [英] D3: ordinal scale not working with array of objects

查看:44
本文介绍了D3:序数刻度不适用于对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然在和d3挣扎.

我尝试建立一个序数标度,其中域是对象数组.不知何故,rangeBands无法使用这种类型的数组.它适用于字符串或数字数组.有人可以解释为什么吗?

I tried build an ordinal scale where the domain is an array of objects. Somehow rangeBands is not working with this type of array. It works with a string or number array. Can anybody explain why?

var number_data = [1,2,3,4,5];

var string_data = ["1","2","3","4","5"]

var object_data = [{"test":"1" },{"test":"2"},{"test":"3"},{"test":"4"},{"test":"5"}]

console.log("+++ Array of Numbers +++")
var scale= d3.scale.ordinal()
                .domain(number_data)
                .rangeBands([0,100]);

number_data.forEach(function(d){
    console.log(scale(d));
});

console.log("+++ Array of Strings +++")
scale.domain(string_data);

string_data.forEach(function(d){
    console.log(scale(d));
});

console.log("+++ Array of Objects +++")
scale.domain(object_data);

object_data.forEach(function(d){
    console.log(scale(d));
});

http://jsfiddle.net/tgtrtv9e/

推荐答案

您不能使用具有顺序刻度的对象.在内部,从输入到输出的映射使用D3映射,该映射将键(即输入)强制转换为字符串.对于对象,结果是所有对象的"[object Object]" .也就是说,所有对象在序数尺度上看起来"都是相同的.

You cannot use objects with ordinal scales. Internally, the mapping from input to output uses D3 maps, which coerce the keys (i.e. the input) to strings. For objects, the result is "[object Object]" for all objects. That is, all objects "look" the same to an ordinal scale.

这篇关于D3:序数刻度不适用于对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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