简体   繁体   English

无法将刻度线与d3.js对齐

[英]Unable to align ticks with d3.js

Working with one bar chart with d3.js I am unable to align ticks in x axis with bars. 使用d3.js处理一个条形图时,我无法使x轴上的刻度线与条形对齐。

刻度线在X轴的中心区域对齐不良

In left and right verges the ticks are ok, but not in the middle. 在左侧和右侧边缘打勾是可以的,但在中间不是。 Here is the code: 这是代码:

var formatDate = d3.time.format("%e %b");
var height = 325;
var xTimeScale = d3.time.scale()
        .domain([new Date(data[0].date), d3.time.day.offset(new Date(data[data.length - 2].date), 1)])
        .range([30, width]);

var xAxis = d3.svg.axis()
        .scale(xTimeScale)
        .orient("bottom")
        .ticks(d3.time.days, .1)
        .tickFormat(formatDate);

chart.append("g")
        .attr("class", "xaxis axis") 
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

chart.selectAll(".xaxis text") 
        .attr("transform", function(d) {return "translate(" + this.getBBox().height * -2 + "," + this.getBBox().height + ")rotate(-45)";});

What am I missing? 我想念什么? Thanks in advance. 提前致谢。

Update: Here is the jsFiddle updated with chrtan suggestions. 更新:这里是的jsfiddle与更新chrtan建议。 My problem now is to align text with the center of bar and not in left. 我现在的问题是使文本与条的中心对齐,而不是在左侧。

From the looks of your graph, you lost a bar's worth of space. 从图表的外观来看,您损失了一个条形空间。 If all the bars are supposed to be left-aligned against a tick, that final bar you have should be to the right of the January 31st tick. 如果所有条柱都应与刻度线左对齐,则最后一条柱线应位于1月31日刻度线的右侧。

You might need to add the February 1st tick by perhaps changing the [data.length - 2] to [data.length - 1] in the domain() for your xTimeScale. 您可能需要通过在xTimeScale的domain()中将[data.length-2]更改为[data.length-1]来添加2月1日的刻度。

Then for display purposes, you could probably remove the last tick axis text with: 然后出于显示目的,您可以使用以下命令删除最后一个刻度轴文本:

d3.select(chart.selectAll(".xaxis text")[0].pop()).remove();

The inner selectAll should get the array containing your xAxis tick texts and then pop the very last tick. 内部的selectAll应该获取包含xAxis刻度文本的数组,然后弹出最后一个刻度。 This last tick should then be removed by the outer select. 然后应通过外部选择器删除最后一个刻度。

An example with an auto time ticks with d3.js 带有d3.js的自动计时示例

// set domain for axis
var x_domain = d3.extent(data, function(d) { return new Date(d.date); });
//format date
var  date_format = d3.time.format('%Y %B');

var vis = d3.select("#graph")
    .append("svg:svg")
    .attr('class', 'chart')
    .attr("width", width)
    .attr("height", height);

var yScale = d3.scale.linear()
    .domain([0, d3.max(data, function(d) { return d.data; })]).nice()
    .range([height - padding, padding]); 
var xScale = d3.time.scale()
    .domain(x_domain)
    .range([padding, width - padding]);
// define the y axis
var yAxis = d3.svg.axis()
    .orient("left")
    .scale(yScale);
// define the x axis
var xAxis = d3.svg.axis()
    .orient("bottom")
    .scale(xScale)
    .tickFormat(date_format);
// draw y axis with labels and move in from the size by the amount of padding
vis.append("g")
    .attr("class", "axis")
    .attr("transform", "translate("+padding+",0)")
    .call(yAxis);
// draw x axis with labels and move to the bottom of the chart area
vis.append("g")
    .attr("class", "xaxis axis")
    .attr("transform", "translate(0," + (height - padding) + ")")
    .call(xAxis);

// and set data in graph...

a great example : http://bl.ocks.org/phoebebright/3061203 一个很好的例子: http : //bl.ocks.org/phoebebright/3061203

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

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