1

The problem:

I'm new to D3.js and I've created a bar chart that gets data from an array. I placed text on top of the bars and the text is wider than the bars themselves. I want to rotate it vertical so that the text goes through the middle of the bar.

I know .attr("transform", "rotate(180)") won't work because it rotates my text off-screen. I found an answer from Mike Bostock here, but it doesn't seem to work for me, I don't know what I'm doing wrong. He says I need to change the rotation point of origin, but I can't get it to work.

Any help is appreciated, you can find the code below:

var label = svg.selectAll("text")
              .data(data)
              .enter()
              .append("text")
              .text(function(d){
                return d;
              })
              .attr("x", function(d, i){
                xText = i*(w/data.length);
                return xText;
              })
              .attr("y", function(d){
                yText = h - yScale(d);
                return yText;
              })
              .attr("transform", "translate(" + xText + "," + yText + ") rotate(90)")
              .attr("fill", "black")
              .attr("font-family", "sans-serif")
              .attr("font-size", "11px");

Thanks!

Community
  • 1
  • 1
codeWolf
  • 165
  • 3
  • 14

2 Answers2

6

You are setting both the x and y attributes and translated the text. With the rotate, you only want to use the translate:

.attr("transform", function(d,i){
  var xText = i * (w / data.length);
  var yText = h - yScale(d.v);
  return "translate(" + xText + "," + yText + ") rotate(90)";
})

Also, I find your example code confusing, I was assuming that your data was an array of strings since you do:

.text(function(d) {
  return d;
})

but that would make yScale(d), return NaN.

Here's an example that works nicely:

<html>

<head>
  <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
</head>

<body>
  <script>
    var w = 300,
      h = 300,
      data = [{
          t: "Now",
          v: 50
        },{
          t: "is",
          v: 25
        },{
          t: "the",
          v: 10
        },{
          t: "winter",
          v: 30
        }];

    var svg = d3.select('body')
      .append('svg')
      .attr('width', w)
      .attr('height', h);

    var yScale = d3.scale.linear()
      .range([0, h])
      .domain([60, 0]);

    var label = svg.selectAll("text")
      .data(data)
      .enter()
      .append("text")
      .text(function(d) {
        return d.t;
      })
      .attr("transform", function(d,i){
        var xText = i * (w / data.length);
        var yText = h - yScale(d.v);
        return "translate(" + xText + "," + yText + ") rotate(90)";
      })
      .attr("fill", "black")
      .attr("font-family", "sans-serif")
      .attr("font-size", "11px");
  </script>
</body>

</html>
Mark
  • 106,305
  • 20
  • 172
  • 230
  • Hey Mark, thanks. That works, I didn't realize I had to remove the x and y attributes, it makes sense now. Thanks for your help P.S. My data is an array of integers, like so: var data = [ 100, 200, 300, 400, 500, etc ]; – codeWolf Aug 09 '15 at 07:29
1

<!DOCTYPE html>
<meta charset="utf-8">
<style>
  .bar {
    fill: steelblue;
  }
  .bar:hover {
    fill: brown;
  }
  .axis {
    font: 10px sans-serif;
  }
  .axis path,
  .axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
  }
  .x.axis path {
    display: none;
  }
</style>

<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
  <script>
    var data = [{
      "letter": "A",
      "frequency": 0.08167
    }, {
      "letter": "B",
      "frequency": 0.01492
    }, {
      "letter": "C",
      "frequency": 0.02782
    }, {
      "letter": "D",
      "frequency": 0.04253
    }, {
      "letter": "E",
      "frequency": 0.12702
    }];

    var margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 40
      },
      width = 500 - margin.left - margin.right,
      height = 200 - margin.top - margin.bottom;

    var x = d3.scale.ordinal()
      .rangeRoundBands([0, width], .1);

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

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

    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .ticks(10, "%");

    var svg = d3.select("body").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");



    x.domain(data.map(function(d) {
      return d.letter;
    }));
    y.domain([0, d3.max(data, function(d) {
      return d.frequency;
    })]);

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

    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Frequency");

    svg.selectAll(".bar")
      .data(data)
      .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) {
        return x(d.letter);
      })
      .attr("width", x.rangeBand())
      .attr("y", function(d) {
        return y(d.frequency);
      })
      .attr("height", function(d) {
        return height - y(d.frequency);
      });



    function type(d) {
      d.frequency = +d.frequency;
      return d;
    }

    var text = svg.selectAll(".text").data(data);
    text.enter()
      .append("text")
      .attr("class", "text")
      .attr("x", function(d, i) {
        return x(d.letter);
      })
      .attr("y", function(d) {
        return y(d.frequency);
      })
      .text(function(d) {
        return d.letter;
      })

    .attr("transform", function(d) {
      return "rotate(90 " + x(d.letter) + "," + (y(d.frequency) - d.letter.length) + ")"
    });
  </script>

You can try out this, and as a base you can modify this as per your need. I hope it helps.

Rudra
  • 1,678
  • 16
  • 29