简体   繁体   English

更新交叉轴上的 D3 多折线图

[英]D3 multiline chart on update crosses axis

I am trying to plot a multiline d3 chart.我正在尝试绘制多行 d3 图表。 I have created a method which should take a new dataset and try to plot it in the same d3 frame for new data update changes (possibly filters).我创建了一个方法,它应该采用一个新的数据集,并尝试在同一个 d3 帧中绘制它以进行新的数据更新更改(可能是过滤器)。

  1. The first draw works fine but the next draw (mocked data: which is a slice of the previous data and few manipulated values) is not showing correct is crossing the x axis.第一次绘制工作正常,但下一次绘制(模拟数据:它是先前数据的一部分和少量操纵值)未正确显示穿过 x 轴。

    [See Image below] [见下图]

  2. Also the starting origin is missing a tick which should also be 2010 in this example此外,起始原点缺少一个刻度,在本例中也应该是 2010

  3. I also want to create few more lines if there is more datapoints in the future which should be dynamic.如果将来有更多应该是动态的数据点,我还想创建更多行。 Current model is {date, actual, projected}, More expected is mean or difference which will only be shown on trigger.当前模型是{日期,实际,预计},更多预期是平均值或差异,只会在触发时显示。

Any help would be appreciated.任何帮助,将不胜感激。

在此处输入图片说明 Here is a Stackblitz https://stackblitz.com/edit/angular-rhr39p这是一个 Stackblitz https://stackblitz.com/edit/angular-rhr39p

References:参考:

  1. Animated line chart: http://bl.ocks.org/atmccann/8966400动画折线图: http : //bl.ocks.org/atmccann/8966400
  2. Multiline chart: https://bl.ocks.org/larsenmtl/e3b8b7c2ca4787f77d78f58d41c3da91多线图: https : //bl.ocks.org/larsenmtl/e3b8b7c2ca4787f77d78f58d41c3da91
  3. Dataset updates: https://bl.ocks.org/d3noob/7030f35b72de721622b8数据集更新: https : //bl.ocks.org/d3noob/7030f35b72de721622b8

Please keep just one problem per question here at Stack Overflow.请在 Stack Overflow 上为每个问题保留一个问题。 This answer will deal with problem #1, consider asking separate questions for the other problems.这个答案将处理问题#1,考虑为其他问题提出单独的问题。

The issue here is just your enter/update methodology, that is not correct and.这里的问题只是您的输入/更新方法,这是不正确的。 Stick with the idiomatic D3, which is along these lines:坚持使用惯用的 D3,它是这样的:

const update =  this.svg.selectAll('.records')
    .data(records);

const enter = update.enter().append('g')
    .attr('class', 'records'); 

Then, you append new paths using enter and update those paths using update .然后,您使用enter附加新路径并使用update更新这些路径。

You can also ditch the groups and create enter/update/exit selections for the paths directly.您还可以放弃组并直接为路径创建进入/更新/退出选择。 That will make your code simpler.这将使您的代码更简单。

Here is the forked code: https://stackblitz.com/edit/angular-lyd79t?file=src%2Fapp%2Flinechart%2Flinechart.component.ts这是分叉的代码: https : //stackblitz.com/edit/angular-lyd79t? file = src%2Fapp%2Flinechart%2Flinechart.component.ts

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

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