d3.js在y轴上需要重复的值 [英] d3.js need repeated values on y axis

查看:93
本文介绍了d3.js在y轴上需要重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

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

我的y轴代码为:

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

它仅在y轴上显示唯一值,我想按原样显示这些值.您能帮我提供正确的代码吗?

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?

推荐答案

此答案提供了基本的解决方案:按序标度,域值唯一地标识相应的范围值.在您的情况下,两个"slot1"输入值都将映射到相同的输出值,因此不会显得唯一.

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.

解决方案是使用数组索引(0, 1, 2...)代替数组值("slot1", "slot2"...)作为您的序数标度输入域:

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);

在这里拨弄: http://jsfiddle.net/henbox/fhf309​​5r/3/

这篇关于d3.js在y轴上需要重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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