score:0

This one should work :

svg.selectAll("rect")
    .data(dataset)
    .enter()
    .append("rect")
    .attr("x", function (d, i) {
      return i + (20 + barPadding);
    })
    .attr("y", function (d,i) {

      return heigth - d["T" + (i+1)]
    })
    .attr("width", 20)
    .attr("heigth", function (d,i) {
      return heigth - d["T" + (i+1)]
    })
    .attr("fill", "teal")

score:1

Here's the codepen and here's the code for testing on your local:

<!DOCTYPE html>
<meta charset="utf-8">

<head>
    <style>

  .bar{
    fill: steelblue;
  }

  .bar:hover{
    fill: brown;
  }

    .axis {
      font: 10px sans-serif;
    }

    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }

    </style>
</head>

<body>

<script src="http://d3js.org/d3.v3.min.js"></script>

<script>
// set the dimensions of the canvas
var margin = {top: 20, right: 20, bottom: 70, left: 40},
    width = 600 - margin.left - margin.right,
    height = 300 - margin.top - margin.bottom;


// set the ranges
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

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

// define the axis
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")


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


// add the SVG element
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 + ")");


// load the data
d3.json("data.json", function(error, data) {


  // scale the range of the data
  x.domain(data.map(function(d) { return Object.keys(d); }));
  y.domain([0, d3.max(data, function(d) { return +d[Object.keys(d)]; })]);

  // add axis
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .selectAll("text")
      .style("text-anchor", "end")
      .attr("dx", "-.8em")
      .attr("dy", "-.55em")
      .attr("transform", "rotate(-90)" );

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

  // Add bar chart
  svg.selectAll("bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(Object.keys(d)); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(+d[Object.keys(d)]); })
      .attr("height", function(d) { return height - y(+d[Object.keys(d)]); });

});

</script>

</body>

I replaced all references for the y axis with the json keys using Object.keys() and got the values for these keys to put into the x axis. You could also use for... in loop to do the same thing.

To test it locally, you will have to host both files on a local server or else you will get CORS errors. You could include a parsed inline json to avoid CORS altogether.


Related Query

More Query from same tag