Search code examples
jsonangularchart.jsbar-chartangular7

Map JSON for Chartjs with Angular 7


Im trying to map JSON Data to show it in a Bar-Chart. The final Array I need has to look like this:[883, 5925, 17119, 27114, 2758]. Actually, the Array I want to use to set the barChartData (dringlichkeitenValues[])seems to be empty. Sorry for my bad coding skills. Can anyone show me how to solve this Problem?

JSON:

[{
    "id": 1,
    "value": 883
},
{
    "id": 2,
    "value": 5925
},
{
    "id": 3,
    "value": 17119
},
{
    "id": 4,
    "value": 27144
},
{
    "id": 5,
    "value": 2758
}]

api.service.ts

getDringlichkeiten(): Observable<IDringlichkeit[]> {
    return this.http.get<IDringlichkeit[]>(this.ROOT_URL + '/aufenthalte/dringlichkeit');}

dringlichkeit.ts

  export interface IDringlichkeit {
  id: number;
  value: number;
}

bar-chart.component.ts

   export class BarChartComponent implements OnInit {

  public dringlichkeitValues:number[] = [];
  public dringlichkeiten: IDringlichkeit[];
  public barChartLabels:String[] = ["1", "2", "3", "4", "5"];
  public barChartData:number[] = this.dringlichkeitValues;
  public barChartType:string = 'bar';

  constructor(private aufenthaltService: AufenthaltService) {

  }

  ngOnInit() {
    this.loadData();
    this.getDringlichkeitValues();
  }

  loadData(){

    this.aufenthaltService.getDringlichkeiten()
    .subscribe( data => this.dringlichkeiten = data);
  }

  getDringlichkeitValues(){
    let dringlichkeitValues:number[]=[];
    this.dringlichkeiten.forEach(dringlichkeit=>{
      dringlichkeitValues.push(dringlichkeit.value)
      this.dringlichkeitValues = dringlichkeitValues;
    });
    return this.dringlichkeitValues;
  }

}

UPDATE: I updated my component but now my Array is still empty after subscribing to the Observable.

bar-chart.component.ts

chart: Chart;
  dringlichkeiten: IDringlichkeit[] = [];


  constructor(private aufenthaltService: AufenthaltService) {
  }

  ngOnInit() {

    this.aufenthaltService.getDringlichkeiten()
    .subscribe( data => {
      this.dringlichkeiten = data;

      //dringlichkeiten-Array full
      console.log(this.dringlichkeiten);
    });

    //dringlichkeiten-Array empty
    console.log(this.dringlichkeiten);


    this.chart = new Chart('canvas', {
      type: 'bar',
      data: {
        labels: this.dringlichkeiten.map(x => x.id),
        datasets: [
          {
            label: 'Dringlichkeiten',
            data: this.dringlichkeiten.map(x => x.value),
            backgroundColor: ['#FF6384', '#4BC0C0', '#FFCE56', '#E7E9ED', '#36A2EB']
          }
        ]
      },
    });


  }

Solution

  • To get the "values" from your JSON array, you can use:

    dringlichkeiten.map(x => x.value)
    

    This will get you an array you require, i.e.:

    [883, 5925, 17119, 27114, 2758]
    

    You can then pass this array to chartJS for it to render you a chart like so:

    this.chart = new Chart('canvas', {
      type: 'bar',
      data: {
        labels: dringlichkeiten.map(x => x.id),
        datasets: [
          {
            label: 'My Bar Chart',
            data: dringlichkeiten.map(x => x.value),
            backgroundColor: ['red', 'green', 'yellow', 'blue', 'orange']
          }
        ]
      },
    });
    

    Take a look at this simplified working SlackBlitz example.

    Hope this helps!