Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'legend' of undefined | Angular + ng2-charts

I'm getting a TypeError: Cannot read property 'legend' of undefined when trying to display example data for a chartjs chart in angular.

I've tried using different examples or chart types, but it keeps throwing this error.

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'}
];
like image 266
f5172c0d83892c41b60de3f1fadd89 Avatar asked Jan 27 '23 09:01

f5172c0d83892c41b60de3f1fadd89


2 Answers

Remove [legend]="arbarChartLegend" from your html.

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

like image 104
Chris Avatar answered Apr 24 '23 04:04

Chris


For me it was enough to initialise empty arbarChartOptions like:

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 = {}

to solve the error

like image 42
Constantin Beer Avatar answered Apr 24 '23 04:04

Constantin Beer