【问题标题】:ngx chart data with pipe带有管道的 ngx 图表数据
【发布时间】:2018-01-19 19:11:03
【问题描述】:

我正在使用带有 ngx-charts 数据数组的过滤器管道。数据按 2 个日期过滤:fromDate 和 toDate。使用使数组更小的日期进行过滤时,管道工作正常,但是当我首先使用较小的日期范围进行过滤,然后再次使范围变大时,管道不适用于原始数组,但适用于已过滤的数组。

我已经做过其他点子,但从未遇到过这个问题,我不确定这里出了什么问题。也许你们中的某个人可以帮助我。

管道:

export class DateInRangePipe implements PipeTransform {
    transform(obj: any[], from: Date, to: Date): any[] {
        if (obj && from && to) {
            obj.forEach(data => {
                data.series = data.series.filter((item: any) => {
                    return this.inRange(item.name, from, to);
                });
            });
        }
        return [...obj];
    }

    inRange(date: Date, from: Date, to: Date): boolean {
        return date.getTime() >= from.getTime() &&
            date.getTime() <= to.getTime() ? true : false;
    }
}

Chart.component.html 部分

<ngx-charts-line-chart
    [view]="view"
    [scheme]="colorScheme"
    [results]="multi | dateinrangePipe: from: to"
    [gradient]="gradient"
    [xAxis]="showXAxis"
    [yAxis]="showYAxis"
    [legend]="showLegend"
    [showXAxisLabel]="showXAxisLabel"
    [showYAxisLabel]="showYAxisLabel"
    [xAxisLabel]="xAxisLabel"
    [yAxisLabel]="yAxisLabel"
    [autoScale]="autoScale"
    (select)="onSelect($event)">
</ngx-charts-line-chart>

【问题讨论】:

    标签: angular angular-pipe ngx-charts


    【解决方案1】:
    `data.series = data.series.filter...` 
    

    是对原始对象的引用。要打破引用,必须在开头创建 obj 数组的克隆。

    【讨论】:

      【解决方案2】:

      管道不会在输入不变的情况下重新计算其值...所以即使参数改变它也会重新计算。

      有一个 StackOverflow 问题反映了这一点。最常见的解决方案是将管道 pureness 更改为 false,并将主机组件设置为 ChangeDetectionStrategy.OnPush。但是还有更多的解决方案。看看这个链接:pipes change detection

      希望这会有所帮助。

      【讨论】:

      • 管道的输入是 2 个日期参数,它们确实发生了变化,并且管道也被调用了,当缩小日期范围时它确实可以正常工作,但是当之后变大时,更改不会生效。
      • from 和 to 不是输入,它们是参数。您确定何时调用参数 from 和 to 更改管道的变换函数? @MaxR。
      • 是的,该做的。我有 2 个日期选择器来设置日期范围,在提交时我将日期选择器日期放入管道的日期参数中,然后数组被日期范围过滤。但是,这仅在使数组更小时才有效,再次增加日期范围时,它没有效果。
      猜你喜欢
      • 2017-02-02
      • 2018-08-23
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      相关资源
      最近更新 更多