title: D3.js bar chart - axis and labels not working / transitioning
body: <p>first time using D3.js. I am stuck at getting my bar chart to display value labels on the bars and transitioning the x/y axis when data is refreshed. I get no errors and the code came from putting a couple of examples together. Code is below, <a href="http://jsfiddle.net/TheRealPapa/h8Rsg/32/" rel="nofollow">jfiddle here</a> for working example. In the real code I am pulling data using d3.json, in the jsfiddle I am using static vars, fyi. Any help is appreciated.</p>
  
  <p>Code below:</p>
  
  <pre><code>var data;
  
  var margin = {
      top: 20,
      right: 0,
      bottom: 30,
      left: 30
  },
  width = 838 - margin.left - margin.right,
      height = 300 - margin.top - margin.bottom;
  
  var x = d3.scale.ordinal()
      .rangeRoundBands([0, width], 0.05);
  
  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("#daychart")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  
  
  d3.json("http://10.0.0.13/sma/sma-php/inverterdata.php?var=CDAY&amp;id=C1200031", function (error, data) {
  data.forEach(function (d) {
      d.max_energy = +d.max_energy;
  });
  
  x.domain(data.map(function (d) {
      return d.xaxis;
  }));
  y.domain([0, d3.max(data, function (d) {
      return d.max_energy;
  })]);
  
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
      .append("text")
      .attr("transform", "rotate(0)")
      .attr("y", 23)
      .attr("x", 340)
      .attr("dy", ".71em")
      .style("text-anchor", "bottom")
      .text("Timeline");
  
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(0)")
      .attr("y", -15)
      .attr("x", -25)
      .attr("dy", ".71em")
      .style("text-anchor", "top")
      .text("Energy - KWh");
  
  svg.selectAll(".bar")
      .data(data)
      .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function (d) {
      return x(d.xaxis);
  })
      .attr("width", x.rangeBand())
      .attr("y", function (d) {
      return y(d.max_energy);
  })
      .transition().delay(function (d, i) {
      return i * 10;
  }).duration(10)
      .attr("height", function (d) {
      return height - y(d.max_energy);
  });
  
  //Create labels
  svg.selectAll("text")
      .data(data)
      .enter()
      .append("text")
      .text(function (d) {
      return parseFloat(Math.round(d.max_energy * 100) / 100).toFixed(1);
  })
      .attr("x", function (d) {
      return x(d.xaxis) + x.rangeBand() / 2;
  })
      .attr("y", function (d) {
      return height - y(d.max_energy) + 14;
  })
      .attr("text-anchor", "middle")
      .attr("font-family", "sans-serif")
      .attr("font-size", "11px")
      .attr("fill", "black");
  
  //});
  
  //On click, update with new data            
  d3.select("p").on("click", function () {
  
      // Get the data again
          d3.json("http://10.0.0.13/sma/sma-php/inverterdata.php?var=PDAY&amp;id=P100023", function (error, data) {
      data.forEach(function (d) {
          d.max_energy = +d.max_energy;
      });
  
      // Scale the range of the data again 
      x.domain(data.map(function (d) {
          return d.xaxis;
      }));
      y.domain([0, d3.max(data, function (d) {
          return d.max_energy;
      })]);
  
      // Make the changes
      svg.selectAll(".bar") // change the bar
      .data(data) // Update the data within.
      .transition().delay(function (d, i) {
          return i / data.length * 1000;
      })
          .duration(500)
          .attr("x", function (d) {
          return x(d.xaxis);
      })
          .attr("y", function (d) {
          return y(d.max_energy);
      })
          .attr("width", x.rangeBand())
          .attr("height", function (d) {
          return height - y(d.max_energy);
      });
  
      //Update all labels
      svg.selectAll("text")
          .data(data).transition()
          .delay(function (d, i) {
          return i / data.length * 1000;
      })
          .duration(500)
          .text(function (d) {
          return parseFloat(Math.round(d.max_energy * 100) / 100).toFixed(1);
      })
          .attr("x", function (d, i) {
          return x(i) + x.rangeBand() / 2;
      })
          .attr("y", function (d) {
          return height - y(d.max_energy) + 24;
      });
  
      });
  
  });
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;p&gt;CLICK ME&lt;/p&gt;
  &lt;svg id="daychart"&gt;&lt;/svg&gt;
  </code></pre>