[Chartjs]-Chart.js dynamically set values of suggestedMin and suggestedMax based on values of array

1👍

Changing the values on minMaxSeries does not alter lineChartOptions. If you add console.log(this.lineChartOptions); at the end of ngOnInit(), you will see that suggestedMin and suggestedMax still have value zero.

To solve your problem, you should set the options directly on lineChartOptions as follows:

this.lineChartOptions.scales.yAxes[0].ticks.suggestedMin = min;
this.lineChartOptions.scales.yAxes[0].ticks.suggestedMax = max;

The ngOnInit() method could be simplified as shown below.

ngOnInit(): void {
  this.lineChartOptions.scales.yAxes[0].ticks.suggestedMin = Math.floor(Math.min(...this.seriesB));
  this.lineChartOptions.scales.yAxes[0].ticks.suggestedMax = Math.ceil(Math.max(...this.seriesB));
}  

Please have a look at your amended StackBlitz.

Leave a comment