D3: ordinal scale not working with array of objects

人走茶凉 提交于 2021-02-08 11:42:25

问题


still struggeling with d3.

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/


回答1:


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.



来源:https://stackoverflow.com/questions/28819363/d3-ordinal-scale-not-working-with-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!