简体   繁体   English

d3刻度上的轴两侧的刻度线

[英]Tick marks on both sides of axis on d3 scale

I'm trying to have the tick marks show up on both sides of the y axis. 我正在尝试在y轴的两侧显示刻度线。 As the code is shown below, I'm able to extend the tick marks based on length -width which draws the tick mark from a starting point from left to right. 如下面的代码所示,我能够基于length -width扩展刻度线,从起点到左到右绘制刻度线。

Is it possible to move the starting point of the tick mark further to the left? 是否可以将刻度线的起点进一步向左移动?

My intent is aesthetics, but to have the tick values be directly over a length of the tick marks. 我的意图是美观,但要使刻度值直接在刻度线的长度上。

I have a grid set up, made up of container-length tick marks: 我建立了一个网格,它由容器长度的刻度线组成:

// Axis
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickSize(-height);

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickSize(-width)
    .tickFormat(d3.format("s"));

Based on these scales: 基于这些比例:

// Scale
var x = d3.time.scale()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

My axis are appended to the svg like this: 我的轴像这样附加到svg:

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
    .call(adjustXLabels);

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .call(adjustYLabels);

I'm a little confused about what you're after. 我对您的追求感到困惑。 Do you want the tick labels to overlap with the ticks themselves? 您是否希望刻度标签与刻度本身重叠? If so you can select the text after the axis is drawn and then translate the ticks. 如果是这样,您可以在绘制轴之后选择文本,然后平移刻度线。

See the fiddle here: https://jsfiddle.net/6q3rpw6j/ 在这里查看小提琴: https : //jsfiddle.net/6q3rpw6j/

The key bit is: 关键是:

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .selectAll(".tick text")
    .attr("transform", "translate(15,0)");

EDIT 编辑

To move the ticks themselves: 自己移动刻度线:

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .selectAll(".tick line")
  .attr("transform", "translate(15,0)");

And to remove the top and bottom end ticks, make sure you set the .outerTickSize(0) 要删除顶端和底端的刻度,请确保设置.outerTickSize(0)

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

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