Since I was not able to get one of the above solutions to work properly, I want to contribute to the solution if someone stumbles on this post and is also stuck with existing approaches.
I have HTML similar to @ mustafa918:
<div> <canvas #canvas id="canvas" baseChart [datasets]="lineChartData" [labels]="lineChartLabels" [colors]="lineChartColors" [options]="lineChartOptions" [chartType]="lineChartType" [legend]="lineChartLegend" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)"> </canvas> </div>
And to initialize these diagrams in typing, I have:
public lineChartData: Array<any> = [ { data: this.heights, label: 'Heights History', type: 'line', fill: false}, { data: this.widths, label: 'Widths History', type: 'line', fill: false }];
And for me it worked only by setting data and labels at the same time and did not use chart.update () - chart is a reference to BaseChartDirective.
I preloaded the relevant data and labels so that in this.heights, this.width and this.lineChartLabels match the data.
For example: records at heights [i], widths [i] and lineChartLabels [i] coincide with the element in my elementArray at index i => element = {"height": 30, "width": 20, "label": "box "}
setDatasets() { //store data in chart references var arrHeights = []; for (var i in this.heights) { arrHeights.push({ x: this.lineChartLabels[i], y: this.heights[i] }); } var arrWidths= []; for (var i in this.widths) { arrWidths.push({ x: this.lineChartLabels[i], y: this.widths[i] }); } this.lineChartData[0].data = arrHeights; this.lineChartData[1].data = arrWidths;
}
Hope this helps someone :) Good luck!
luminous_koyote
source share