d3.js need repeated values on y axis

血红的双手。 提交于 2019-12-23 05:44:19

问题


I have an array:

taskTypes = [slot1, slot2, slot3,slot4,slot1,slot2,slot6];

my code for y-axis is:

var y = d3.scale.ordinal()
    .domain(taskTypes)
    .rangeRoundBands([ 0, height - margin.top - margin.bottom ], .1);

It is showing only unique values on y-axis, I want to show the values as they are. Can you help me out with the correct code?


回答1:


This answer gives the basic solution: Using the ordinal scale, domain values uniquely identify corresponding range values. In your case both the '"slot1"' input values will map to the same output value, so will not appear unique.

The solution is to use array indices (0, 1, 2...) instead of array values ("slot1", "slot2"...) as your ordinal scale input domain:

var taskTypes = ["slot1", "slot2", "slot3","slot4","slot1","slot2","slot6"];

var y = d3.scale.ordinal()
     .domain(d3.range(0, taskTypes.length))
     .rangeRoundBands([ 0, height - margin.top - margin.bottom ], .1);

Fiddle here: http://jsfiddle.net/henbox/fhf3095r/3/



来源:https://stackoverflow.com/questions/30049087/d3-js-need-repeated-values-on-y-axis

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