根据用户的视图更新D3.JS可滚动折线图上的轴

时间:2016-12-30 04:32:53

标签: javascript css d3.js charts nvd3.js

我试图让D3.JS可滚动折线图根据用户的当前视图自动重新调整轴。

举一个例子,可以在Google财经上看到预期的行为。

在第一张图表中,您可以看到价格达到5485附近:

enter image description here

在第二张图表中,我向前滚动,您可以看到图表已自动重新调整以适应5510的新峰值:

enter image description here

请注意,此行为与简单地使图表响应不同,这可以通过将整个图表(包括用户视图之外的数据)放在响应容器中来轻松实现,例如:

.svg-container {
    display: inline-block;
    position: relative;
    width: 100%;
    padding-bottom: 65%;
    vertical-align: top;
    overflow: hidden;
}

为了解决这个问题,我需要区分价格历史的全部和用户当前正在查看的细分,然后设置y轴范围。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解这个问题,但您似乎只想在所选数据范围发生变化时缩放y轴。建立this classic example之后,我对其进行了修改,以便在刷子或缩放时更改y轴域更改以匹配当前选择。关键代码是brushed回调:

// set new y domain based on selection
y.domain([0, d3.max(data, function(d) {
  if (d.x < x.domain()[0] || d.x > x.domain()[1]) return 0;
  else return d.y;
})]);
// redraw y axis
focus.select(".axis--y").call(yAxis);

完整示例:

&#13;
&#13;
<!DOCTYPE html>
<meta charset="utf-8">
<style>
  .area {
    fill: steelblue;
    clip-path: url(#clip);
  }
  
  .zoom {
    cursor: move;
    fill: none;
    pointer-events: all;
  }
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
  var svg = d3.select("svg"),
    margin = {
      top: 20,
      right: 20,
      bottom: 110,
      left: 40
    },
    margin2 = {
      top: 430,
      right: 20,
      bottom: 30,
      left: 40
    },
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

  var x = d3.scaleLinear().range([0, width]),
    x2 = d3.scaleLinear().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]);

  var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

  var brush = d3.brushX()
    .extent([
      [0, 0],
      [width, height2]
    ])
    .on("brush end", brushed);

  var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([
      [0, 0],
      [width, height]
    ])
    .extent([
      [0, 0],
      [width, height]
    ])
    .on("zoom", zoomed);

  var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x(d.x);
    })
    .y0(height)
    .y1(function(d) {
      return y(d.y);
    });

  var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x2(d.x);
    })
    .y0(height2)
    .y1(function(d) {
      return y2(d.y);
    });

  svg.append("defs").append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("width", width)
    .attr("height", height);

  var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

  var data = [];
  for (var i = 0; i < 100; i++) {
    data.push({
      x: i,
      y: i > 50 ? Math.random() * 100 : Math.random() * 50
    });
  }

  x.domain(d3.extent(data, function(d) {
    return d.x;
  }));
  y.domain([0, d3.max(data, function(d) {
    return d.y;
  })]);
  x2.domain(x.domain());
  y2.domain(y.domain());

  focus.append("path")
    .datum(data)
    .attr("class", "area")
    .attr("d", area);

  focus.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

  focus.append("g")
    .attr("class", "axis axis--y")
    .call(yAxis);

  context.append("path")
    .datum(data)
    .attr("class", "area")
    .attr("d", area2);

  context.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height2 + ")")
    .call(xAxis2);

  context.append("g")
    .attr("class", "brush")
    .call(brush)
    .call(brush.move, x.range());

  svg.append("rect")
    .attr("class", "zoom")
    .attr("width", width)
    .attr("height", height)
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .call(zoom);

  function brushed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
    var s = d3.event.selection || x2.range();
    x.domain(s.map(x2.invert, x2));
    focus.select(".area").attr("d", area);
    focus.select(".axis--x").call(xAxis);

    y.domain([0, d3.max(data, function(d) {
      if (d.x < x.domain()[0] || d.x > x.domain()[1]) return 0;
      else return d.y;
    })]);
    focus.select(".axis--y").call(yAxis);

    svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
  }

  function zoomed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
    var t = d3.event.transform;
    x.domain(t.rescaleX(x2).domain());
    focus.select(".area").attr("d", area);
    focus.select(".axis--x").call(xAxis);

    y.domain([0, d3.max(data, function(d) {
      if (d.x < x.domain()[0] || d.x > x.domain()[1]) return 0;
      else return d.y;
    })]);
    focus.select(".axis--y").call(yAxis);

    context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
  }
</script>
&#13;
&#13;
&#13;