问题
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