Note that there are some explanatory texts on larger screens.

plurals
  1. POD3.js Barchart: getting x-axis labels to line up with bars, and rotate them slightly
    primarykey
    data
    text
    <p>I've got a d3 vertical bar chart, and I'm trying to get the x-axis labels positioned appropriately. It's <em>almost</em> there, but not quite. The labels end up being off-center from the bars. I'd also like to rotate the labels slightly, so that I can accommodate a larger graph when I need to.</p> <p>Here's my coffee for the chart (there's some Backbone Marionette in there):</p> <pre><code> buildTimeline: -&gt; width = @ui.chartWrap.innerWidth() height = @ui.chartWrap.height() dataLength = @data.values.length x = d3.scale.ordinal() .rangeRoundBands([0, width], 0.1) y = d3.scale.linear() .range([height, 0]) x.domain @data.values.map (d) -&gt; d.x y.domain [0, d3.max(@data.values, (d) -&gt; d.y )] xAxis = d3.svg.axis() .scale(x) .orient("bottom") svg = d3.select(@ui.chartWrap[0]).append('svg') .attr('width', width) .attr('height', height) .append('g') .attr('transform', '0,0') svg.append("g") .attr('id', 'timeline-labels') .attr("transform", "translate(0,#{height-20})") .call(xAxis) # Bars svg.append('g') .attr('id', 'timeline-bars') .selectAll(".bar") .data(@data.values) .enter().append("rect") .attr("class", "bar") .attr("x", (d) -&gt; x(d.x) ) .attr("width", 25) .attr("y", (d) -&gt; y(d.y) - 30 ) .attr("height", (d) =&gt; height - y(d.y) ).attr("data-date", (d) =&gt; d.x ).attr("data-count", (d) =&gt; d.y ) </code></pre> <p>And then here's my sample JSON for the graph:</p> <pre><code>{ "key": "Documents", "values": [ { "x": "1980", "y": 1752 }, { "x": "1981", "y": 0 }, { "x": "1982", "y": 0 }, { "x": "1983", "y": 0 }, { "x": "1984", "y": 0 }, { "x": "1985", "y": 0 }, ], "type": "year" } </code></pre> <p>The final chart was nearly there, but now all the labels are stacking up under one bar:<img src="https://i.stack.imgur.com/yxYHE.png" alt="D3 Chart"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload