D3:序数比例不适用于对象数组

D3: ordinal scale not working with array of objects

仍在为 d3 苦苦挣扎。

我尝试构建一个序数标度,其中域是一个对象数组。不知何故 rangeBands 不适用于这种类型的数组。它适用于字符串或数字数组。 谁能解释一下为什么?

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]" for all objects。也就是说,所有对象 "look" 相同的顺序尺度。