1

I have bar chart that's very similar to Mike Bostock's Let's Make A Bar Chart.

My data set is a set of years between 1970 and 2015 with a number attached to it. Porting my data to the example code renders:

ordinal scale

Yikes! I want a bit less ticks, so decided to add a ticks count to the x axis:

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

However, this doesn't work because apparently we can only specify the number of ticks we want on linear scales. So I decided to use a time scale, converting all my year numbers to a Javascript Date object (set to January 1):

time scale

Now I can limit the number of ticks I get, but

  • the ticks are placed to the left of my bars, and I think this does not look right.
  • I can't use x.rangeBand() anymore to determine the width of my bars

To get the tick aesthetic of figure 1, but the tick count of figure 2, do I need to do all these bar coordinate / width calculations by myself? Should I use the ordinal scale or another scale?

Maarten
  • 6,894
  • 7
  • 55
  • 90
  • 1
    may be you can show text vertically on x axis see this link http://stackoverflow.com/questions/15437256/vertical-text-in-d3-not-rotated – somename Oct 12 '15 at 17:20

1 Answers1

4

Couple options here.

1.) You could keep datetime axis but move your date +1.577e+10 milliseconds (6 months) so it occurs ticks mid-year.

2.) The solution I like is to keep your ordinal scale but with a custom formatter:

 var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    // show every other label
    .tickFormat(function(d,i){
      if (i % 2) return d;
      else return null;
    }); 

Here's an example of the second approach:

<!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 margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 40
      },
      width = 500 - margin.left - margin.right,
      height = 500 - 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")
      .tickFormat(function(d, i) {
        if (i % 2) return d;
        else return null;
      });

    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 + ")");

    var data = [{
      "letter": "1970",
      "frequency": 0.08167
    }, {
      "letter": "1971",
      "frequency": 0.01492
    }, {
      "letter": "1972",
      "frequency": 0.02782
    }, {
      "letter": "1973",
      "frequency": 0.04253
    }, {
      "letter": "1974",
      "frequency": 0.12702
    }, {
      "letter": "1975",
      "frequency": 0.02288
    }, {
      "letter": "1976",
      "frequency": 0.02015
    }, {
      "letter": "1977",
      "frequency": 0.06094
    }, {
      "letter": "1978",
      "frequency": 0.06966
    }, {
      "letter": "1979",
      "frequency": 0.00153
    }, {
      "letter": "1980",
      "frequency": 0.00772
    }, {
      "letter": "1981",
      "frequency": 0.04025
    }, {
      "letter": "1982",
      "frequency": 0.02406
    }, {
      "letter": "1983",
      "frequency": 0.06749
    }, {
      "letter": "1984",
      "frequency": 0.07507
    }, {
      "letter": "1985",
      "frequency": 0.01929
    }, {
      "letter": "1986",
      "frequency": 0.00095
    }, {
      "letter": "1987",
      "frequency": 0.05987
    }, {
      "letter": "1988",
      "frequency": 0.06327
    }, {
      "letter": "1989",
      "frequency": 0.09056
    }, {
      "letter": "1990",
      "frequency": 0.02758
    }, {
      "letter": "1991",
      "frequency": 0.00978
    }, {
      "letter": "1992",
      "frequency": 0.0236
    }, {
      "letter": "1993",
      "frequency": 0.0015
    }, {
      "letter": "1994",
      "frequency": 0.01974
    }, {
      "letter": "1995",
      "frequency": 0.00074
    }];

    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;
    }
  </script>
Mark
  • 106,305
  • 20
  • 172
  • 230