TypeError:无法读取未定义的属性“ legend”角+ ng2-图

时间:2019-05-03 01:46:47

标签: angular chart.js ng2-charts

尝试以角度显示chartjs图表的示例数据时,我得到了TypeError: Cannot read property 'legend' of undefined

我尝试使用不同的示例或图表类型,但它总是抛出此错误。

HTML:

<div class="chartwrapper">
  <canvas baseChart class="chart"
          [datasets]="arbarChartData"
          [labels]="arbarChartLabels"
          [options]="arbarChartOptions"
          [legend]="arbarChartLegend"
          [chartType]="arbarChartType">
  </canvas>
</div>


TS:

public arbarChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
public arbarChartType = 'bar';
public arbarChartLegend = true;
public arbarChartData: ChartDataSets[] = [
  {data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A'},
  {data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'}
];

2 个答案:

答案 0 :(得分:1)

对我来说,初始化空的arbarChartOptions就足够了:

public arbarChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
public arbarChartType = 'bar';
public arbarChartLegend = true;
public arbarChartData: ChartDataSets[] = [
  {data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A'},
  {data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'}
];
public arbarChartOptions: ChartOptions = {}

解决错误

答案 1 :(得分:0)

从您的html中删除[legend]="arbarChartLegend"

在TS中,arbarChartOptions: any = { legend: { display: true, labels: { fontColor: 'black' } }