简体   繁体   English

D3:使用多个折线图缩放以某种方式组合它们的数据数组

[英]D3: Zoom with multiple line charts somehow combines their data arrays

I've got a chart with some random data.我有一个包含一些随机数据的图表。 On button click I'd like to add another chart on top of the first one.单击按钮时,我想在第一个图表的顶部添加另一个图表。 That works fine.这很好用。 I've also included zoom and it works fine with only the first chart.我还包括了缩放,它只适用于第一个图表。 Zooming with both charts somehow copies the data from the second chart to the first one.使用两个图表进行缩放会以某种方式将数据从第二个图表复制到第一个图表。

Have a look at the example.看看这个例子。 You should be able to see the blue chart.您应该能够看到蓝色图表。 Click the button to add the green one.单击按钮添加绿色。 Now try to zoom in. The blue one disappears.现在尝试放大。蓝色的消失了。 However it is not gone.然而它并没有消失。 It is simply hidden behind the green one.它只是隐藏在绿色的后面。 They are perfectly on top of each other although they should have different values.尽管它们应该具有不同的值,但它们完美地重叠在一起。

https://codesandbox.io/s/31lz6zrln5 https://codesandbox.io/s/31lz6zrln5

Any ideas?有任何想法吗?

Best regards, Mirco最好的问候,米尔科

In the button callback you modify the data elements.在按钮回调中,您可以修改数据元素。

  filtered = () => {
    const values = [...data].map(d => {
      d.value = rnd(25, 75);
      return d;
    });
    this.chart.filtered(values);
  };

You should return new objects based on the fields of the other objects您应该根据其他对象的字段返回对象

  filtered = () => {
    const values = [...data].map(d => {
      return { timestamp: d.timestamp, value: rnd(25, 75) };
    });
    this.chart.filtered(values);
  };

Also update the filtered path in the zoom callback同时更新缩放回调中的filtered路径

  public zoom = () => {
    const newXScale = event.transform.rescaleX(this.xScale);
    const newYScale = event.transform.rescaleY(this.yScale);

    this.xAxisGroup.call(this.xAxis.scale(newXScale));
    this.yAxisGroup.call(this.yAxis.scale(newYScale));

    this.xGridGroup.call(this.xGrid.scale(newXScale));
    this.yGridGroup.call(this.yGrid.scale(newYScale));

    this.line.x(d => newXScale(d.timestamp)).y(d => newYScale(d.value));

    this.lineGroup.attr("d", this.line as any);
    this.lineFiltered.attr("d", this.line as any); // removed the comment of this line
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM