score:1

Accepted answer

I have done a few adjustments to the positional attributes of g elements containing bar rect elements and dx and dy attributes of text elements.

Hope this working code snippet helps.

var margin = {
    top: 20,
    right: 30,
    bottom: 30,
    left: 40
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;
// scale to ordinal because x axis is not numerical
var x = d3.scale.ordinal().rangeRoundBands([0, width], .1);

//scale to numerical value by height
var y = d3.scale.linear().range([height, 0]);

var chart = d3.select("#chart")
  .append("svg") //append svg element inside #chart
  .attr("width", width + margin.left + margin.right) //set width
  .attr("height", height + margin.top + margin.bottom); //set height

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom"); //orient bottom because x-axis will appear below the bars

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left");

var data = [{
  "name": "bill",
  "val": 28
}, {
  "name": "kevin",
  "val": 46
}, {
  "name": "ryan",
  "val": 23
}, {
  "name": "ville",
  "val": 56
}];

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

var barWidth = Math.min.apply(null, [x.rangeBand() - 2, 100]);

var bar = chart.selectAll("g")
  .data(data)
  .enter()
  .append("g")
  .attr("transform", function(d, i) {
    return "translate(" + x(d.name) + ", " + y(d.val) + ")";
  });

bar.append("rect")
  .attr("y", 0)
  .attr("x", barWidth - 10)
  .attr("height", function(d) {
    return height - y(d.val);
  })
  .attr("width", barWidth);

bar.append("text")
  .attr("x", barWidth - 10)
  .attr("y", 0)
  .attr("dx", barWidth / 2)
  .attr("dy", ".75em")
  .text(function(d) {
    return d.val;
  });

chart.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(" + margin.left + "," + height + ")")
  .call(xAxis);

chart.append("g")
  .attr("class", "y axis")
  .attr("transform", "translate(" + margin.left + ",0)")
  .call(yAxis)
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Frequency");

function type(d) {
  d.name = +d.name; // coerce to number
  return d;
}
#chart rect {
  fill: #4aaeea;
}
#chart text {
  fill: white;
  font: 10px sans-serif;
  text-anchor: end;
}
.axis text {
  font: 10px sans-serif;
}
.axis path,
.axis line {
  fill: none;
  stroke: #fff;
  shape-rendering: crispEdges;
}
body {
  background: #1a1a1a;
  color: #eaeaea;
  padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chart" </div>


Related Query