Update the y-axis of a brushed area chart

强颜欢笑 提交于 2019-12-03 02:50:27

I came up with a solution.

I used the brush-filtered x.domain to filter down my original data set. This new filtered data set has only the values that fall within the brush:

// Use x.domain to filter the data, then find the max and min duration of this new set, then set y.domain to that
  x.domain(brush.empty() ? x2.domain() : brush.extent());
  var dataFiltered = data.filter(function(d, i) {
    if ( (d.date >= x.domain()[0]) && (d.date <= x.domain()[1]) ) {
      return d.duration;
    }
  })
  y.domain([0, d3.max(dataFiltered.map(function(d) { return d.duration; }))]);

Finally, be sure to redraw the y-axis as well as the x-axis:

focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
focus.select(".y.axis").call(yAxis);

A shorter possibility would be to use d3.scale.invert() on your brush.extent() like so:

var domainExtent = brush.extent().map(function(d){return scale.invert(d);});
var filteredData = data.filter(function(d){return ((d <= domainExtent[1]) && (d >= domainExtent[0]));});

However, by now d3 has gained d3.brushX(), which only allows brushing in East/West direction by design.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!