TypeError: Не удается прочитать «легенду» свойства неопределенного | Angular + ng2-диаграммы

Я получаю TypeError: Cannot read property 'legend' of undefined при попытке отобразить пример данных для диаграммы chartjs в angular.

Я пробовал использовать разные примеры или типы диаграмм, но эта ошибка продолжает выдаваться.

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'}
];

person f5172c0d83892c41b60de3f1fadd89    schedule 03.05.2019    source источник
comment
попробуйте legend=true вместо [legend]=arbarChartLegend   -  person Chris    schedule 03.05.2019
comment
пробовал, та же ошибка   -  person f5172c0d83892c41b60de3f1fadd89    schedule 03.05.2019
comment
удалить [legend]="arbarChartLegend" из html. В ТС arbarChartOptions: any = { legend: { display: true, labels: { fontColor: 'black' } }   -  person Chris    schedule 03.05.2019
comment
Это сработало, большое спасибо!   -  person f5172c0d83892c41b60de3f1fadd89    schedule 03.05.2019
comment
Могу ли я добавить это как ответ?   -  person Chris    schedule 06.05.2019
comment
Можете ли вы принять мой ответ. Спасибо   -  person Chris    schedule 06.05.2019


Ответы (2)


Удалите [legend]="arbarChartLegend" из вашего html.

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

person Chris    schedule 06.05.2019
comment
Я решил свою проблему, установив пакет ng2-charts-x, который используется для решения некоторых проблем ng2-chart. - person Amir Choubani; 21.05.2019

Для меня было достаточно инициализировать пустой 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 = {}

решить ошибку

person Constantin Beer    schedule 09.03.2020