8

I have a d3 bar chart with, and the axis tick marks are centered below the bars (as I would expect). In this case, I would actually like to left align the axis ticks and have the ticks under the left edge of the bar. Is that possible?enter image description here

EDIT: Javascript for constructing the axis

// note width is just the width of the chart
var xScale = d3.scale.ordinal().rangeRoundBands([0,width], .1); 
var xAxis = d3.svg.axis().scale(xScale).orient("bottom");
4

3 回答 3

3

这是一个左对齐刻度的示例。

http://bl.ocks.org/mbostock/6186172

这个想法是,在创建刻度后,您收集它们并对齐它们:

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
  .selectAll("text")
    .attr("y", 6)
    .attr("x", 6)
    .style("text-anchor", "start");
于 2014-12-29T18:18:27.270 回答
2

为什么不创造一个新的规模?

var padding = .1;

var axisScale = d3.scale.linear()
  .domain([0, n])
  .range([0 + padding, WIDTH + padding]);

n直方图中的条数在哪里。

于 2012-12-03T06:24:35.813 回答
2

我认为当您将轴添加到图表时应该对此进行调整。

尝试(假设您的图表称为 svg):

var bandSize = xScale.rangeBand();

svg.append("svg:g")
 .attr("transform", "translate(-" + bandSize + "," + h + ")")
 .call(xAxis)
 .selectAll("text")
   .style("text-anchor", "start")
   .attr("transform", "translate(" + bandSize + ", 0)");

在我的实验中,这会将刻度移动到每个波段的开头,并保留每个波段下方居中的文本。

于 2014-05-07T21:02:10.820 回答