简体   繁体   English

D3刻度值,刻度值的左右边距

[英]D3 ordinal scale, left and right margins for tickvalues

I have a x-axis with ordinal scale just like in this example: http://bl.ocks.org/mbostock/3259783 我有一个具有顺序刻度的x-axis ,就像在此示例中一样: http : //bl.ocks.org/mbostock/3259783

As you can see the first tick named 'apple' starts from the beginning of the x axis, and the last one 'grapefruit' is at the end. 如您所见,第一个名为“ apple”的刻度是从x轴的起点开始的,最后一个“ grapefruit”是在终点的。 What I want is some padding left for the first ticks and padding right for the last ticks. 我想要的是在最初的刻度上留一些填充,在最后的刻度上留一些填充。

This how I would like to have the axis (also keep in mind that this is an ordinal scale, and I think it's a bit different than time scale or linear) 这就是我想要的轴(还要记住,这是一个有序的刻度,我认为它与时间刻度或线性刻度有些不同) 在此处输入图片说明

This is my code: 这是我的代码:

var x = d3.scale.ordinal()
          .domain(["apple", "orange", "banana", "grapefruit"])
          .rangePoints([0, width]);

var xAxis = d3.svg.axis()
              .scale(x)
              .orient("bottom");

Per the documenatation https://github.com/mbostock/d3/wiki/Ordinal-Scales you can specify the padding at the beginning and end of the ordinal scale "as a multiple of the spacing between points" . 根据文档https://github.com/mbostock/d3/wiki/Ordinal-Scales,您可以在序数标度的开头和结尾指定填充, “作为点之间间距的倍数”

Try: 尝试:

var x = d3.scale.ordinal()
          .domain(["apple", "orange", "banana", "grapefruit"])
          .rangePoints([0, width], 1.0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM