javascript D3 - 使用字符串作为轴刻度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11807574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:24:58  来源:igfitidea点击:

D3 - using strings as axis ticks

javascriptd3.js

提问by Gian

Hi have the following domain array=["Col 1", "2012", "total"]and I create the scale this way

嗨,有以下域array=["Col 1", "2012", "total"],我以这种方式创建了比例

x = d3.scale.ordinal()
        .domain(array)
        .rangeRoundBands([0,width], .1);

And I call the axis this way:

我这样称呼轴:

 d3.svg.axis()
        .scale(x);

Unfortunately this creates unstable behaviour and the data swap on redraw as demonstrated here: http://jsbin.com/ibewux/9/edit

不幸的是,这会造成不稳定的行为和重绘时的数据交换,如下所示:http: //jsbin.com/ibewux/9/edit

The only way to have a stable behaviour is to have numeric values for the domain, namely array=[0, 1, 2, 3, ...]

获得稳定行为的唯一方法是为域设置数值,即 array=[0, 1, 2, 3, ...]

How can I display ticks of this format ["Col 1", "2012", "total",..., "any string"] instead of numeric ones (i.e., [0,1,2,...N]) and avoid that the data charted swap (apparently on different random ticks each time) on redraw?

如何显示这种格式的刻度 ["Col 1", "2012", "total",..., "any string"] 而不是数字(即 [0,1,2,...N] ) 并避免重绘时绘制的数据交换(显然每次都在不同的随机刻度上)?

Many thanks

非常感谢

回答by mbostock

It's already the case for ordinal scalesthat the axisticks are the domain values. As shown here:

对于序数刻度刻度是域值已经是这种情况。如图所示:

Setting axis.tickValuesto the scale's domain here is redundant.

在这里将axis.tickValues设置为刻度域是多余的。