2

Does anyone have an example for using multiple canvas in the same page?

I have something like this in the HTML:

<div style="height: 138px" *ngFor="let item of listItems; let i = index">
    <canvas #pieCanvas id="pieCanvas{{i}}" style="width: 100px !important; height: 100px !important"></canvas>
  </div>

In the .ts file:

@ViewChild("pieCanvas") pieCanvas: ElementRef;

for (var j = 0; j < chartcount; j++)
{

   let htmlRef = document.getElementById('pieCanvas'+j);
   this.pieCanvas = new Chart(htmlRef, piechartdata);

}

Getting always null is not an object (evaluating 'item.length') error.

With only one chart it works perfect, but there I use sth. like

this.barCanvas = new Chart(this.barCanvas.nativeElement......

I Googled, but couldn't find a solution.

Thanks for your help!

0

1 Answer 1

1

I have found the solution....finally!!!

In html:

<canvas #barCanvas id="barCanvaslist{{i}}"></canvas>

Then in ts: @ViewChildren('barCanvas') Canvaslist: QueryList; charts: any;

and afterwards:

this.Canvaslist.changes.subscribe(c => 
{ c.toArray().forEach(item => 
  { 
    this.Canvaslist = new Chart(item.nativeElement, pieData[j]);
    j = j+1;
  }) 
});

this does the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.