本文介绍了d3.js使用rangeBands缩放比例不均匀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制带宽不均匀的序数轴.我知道有一种方法可以在此处中指定范围,但是没有似乎可以使用rangeBands或rangeRoundBands.

I'm trying to draw an ordinal axis with uneven band widths. I know there's a way to do it with range, as specified here, but it doesn't seem to work with rangeBands or rangeRoundBands.

var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"];
var custom_range = [0, 19, 78, 97, 116, 135, 154, 173, 192, 211, 230, 249, 268];

var y_scale = d3.scale.ordinal().domain(letters).rangeRoundBands(custom_range);

console.log(y_scale.range());

结果标度为:

[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

如何使用rangeBands或rangeRoundBands实现自定义范围?我尝试使用范围,但它没有按照我需要的方式绘制轴.

How might I achieve a custom range with rangeBands or rangeRoundBands? I tried using range, but it doesn't draw the axis the way I need.

谢谢.

推荐答案

让我详细说明Lars的答案... erm ..我的意思是评论.

Let me elaborate a bit more on the Lars' answer... erm.. I mean comment.

您的范围将被划分为n个带,每个带的宽度(范围的大小/带数= n),其中n是给定数据集中的基准数.因此,如果您有100px和10个基准,则将有100px/10个基准= 10px宽的条形.这就是为什么不能有宽度不均匀的带子"的原因-Lars< 3

Your range will be divided into n bands each of width (size of range / number of bands = n) where n is the number of datum within the given data set. So if you have 100px and 10 datum you'll have that 100px/10 datum = 10px wide bars. That's why "You can't have bands with uneven width" - Lars <3

您可以在此处阅读有关rangeBands和rangeRoundBands的更多信息: https://github.com/mbostock/d3/wiki/Ordinal-Scales#ordinal_rangeBands

You can read more about rangeBands and rangeRoundBands here: https://github.com/mbostock/d3/wiki/Ordinal-Scales#ordinal_rangeBands

这篇关于d3.js使用rangeBands缩放比例不均匀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 22:15