angular - ng2-charts 空初始数据集

标签 angular charts ng2-charts

我正在试验 ng2-charts使用 Angular 2。我首先按照示例创建折线图。它使用数据集初始化图表,如下所示:

  public lineChartData:Array<any> = [
    {data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A'},
    {data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'},
    {data: [18, 48, 77, 9, 100, 27, 40], label: 'Series C'}
  ];

我想通过从服务器检索数据来进行处理 - 这可行,但我不再需要硬编码的初始数据集。如果我将 lineChartData 更改为空,则会出现以下错误:

ERROR Error: ng-charts configuration error,
      data or datasets field are required to render char line
Stack trace:
../../../../ng2-charts/charts/charts.js/BaseChartDirective.prototype.getDatasets@http://localhost:4200/vendor.bundle.js:31447:19
../../../../ng2-charts/charts/charts.js/BaseChartDirective.prototype.getChartBuilder@http://localhost:4200/vendor.bundle.js:31375:24
../../../../ng2-charts/charts/charts.js/BaseChartDirective.prototype.refresh@http://localhost:4200/vendor.bundle.js:31457:22
../../../../ng2-charts/charts/charts.js/BaseChartDirective.prototype.ngOnInit@http://localhost:4200/vendor.bundle.js:31346:13

我能够解决这个问题的唯一方法是在 lineChartData 中添加空对象。但这限制了我可以返回多少数据。目前,我不确定需要返回多少数据,或者这是否是任意的。

有没有一种方法可以在从服务器检索数据时填充一个空的初始数据集?

最佳答案

当动态数据进入“barChartData”数组时使用 *ngIf 它将起作用。

<div style="display: block" *ngIf="barChartData">
                              <canvas baseChart
                                      [datasets]="barChartData"
                                      [labels]="barChartLabels"
                                      [options]="barChartOptions"
                                      [legend]="barChartLegend"
                                      [chartType]="barChartType"
                                      (chartHover)="chartHovered($event)"
                                      (chartClick)="chartClicked($event)"></canvas>
                            </div>

关于angular - ng2-charts 空初始数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45324585/

相关文章:

angular - 为什么数据初始化不应该在 Angular 组件的构造函数中完成?

javascript - 更改 Flot 条形图上的字体系列

javascript - Google Visualization Chartwrapper 选择图表元素

c# - 使用没有控件的 Microsoft .NET 图表控件库生成图像

angular - 如何在 ngOnChanges 中处理异步

javascript - Angular 4 路由奇怪的行为 : not rendering child module unless on page refresh

chart.js - 如何设置折线图的chart.js网格颜色

javascript - 将 ng2-charts 与加载程序和 AfterViewInit 一起使用

Angular 8 & ChartJs 在饼图中改变颜色

angular - Ngif 用于空字符串验证 Angular 5