d3.js - evenly spaced bars on a time scale

后端 未结 3 1362

I\'m building a bar plot in d3.js in which each bar represents total TB cases during a month. The data essentially consists of a date (initially strings in %Y-%m format, bu

3条回答
  •  花落未央
    2021-01-03 21:18

    I had the same problem, I've ended up accepting that some months are longer than others and adjusting the column bar width so that the gap between bars remains constant. So tweaking the barPath function in the crossfilter home page demo (http://square.github.com/crossfilter/ - uses d3) I got something like this:

    var colWidth = Math.floor(x.range()[1] / groups.length) - 1;//9;
    if (i < n - 1) {
         //If there will be column on the right, end this column one pixel to the left
          var nextX = x(groups[i+1].key)
          colWidth = nextX - x(d.key) - 1;
    }
    path.push("M", x(d.key), ",", height, "V", yVal, "h",colWidth,"V", height);
    

提交回复
热议问题