Chartjs-Problem with saving data in array o Angular and Typescript

0๐Ÿ‘

โœ…

I just tried to set the declaration of the array outside of the function for some clean code and performance reason ,

const benchmarkDates: any[] = null;
const benchmarkValues: any[] = null;

onGetResults() {
  this.accountService.getBenchmark(this.user.id)
    .pipe(first())
    .subscribe(
      benchmarks => {
        this.benchmarks = benchmarks;
     
        this.benchmarks.forEach(data => {
          benchmarkDates.push(data.createdAt;);
          benchmarkValues.push(data.benchmarkResults);
        });
        const allData = {
          label: benchmarkDates,
          data: benchmarkValues,
          fill: true
        }
        this.benchmarkChart[0] = new Chart({
          type: 'radar',
          data: allData,
          options: {
            elements: {
              line: {
                borderWidth: 3
              }
            }
          }
        });
      }
    );
}

Leave a comment