0

I am getting the data and labels for my ChartJs chart via ajax and I create an array out of this.

When I make a console.log, the array looks like it would be working, but the chart has no labels and no data.

I've found this question here, but it doesn't solved the problem.

Here is my code for the chartJs:

               config = {
                    type: 'bar',
                    data: {
                        labels: labels,
                        datasets: [{
                            label: "Aufrufe",
                            data: dataarray,
                            backgroundColor: 'rgba(0, 188, 212, 0.8)'
                        }]
                    },
                    options: {
                        responsive: true,
                        legend: false
                    }
                }

and the arrays that I get from console.log:

enter image description here

That's how I create the array:

/* create a labels array */
                        while(year !== (new Date).getFullYear() || month !== enddate) {
                            console.log(year, month);
                            labels.push(month + " " + year);
                            month++;
                            if(month === 13) {
                                year++;
                                month = 1;
                            }

                        }

But the chart is simply an empty chart...

Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25

1 Answers1

0

The issue was that the Ajax request was async. So the chart was drawn without any data.

The ajax request has to have async: false, so the data can be loaded and after that the chart draws with the data.

Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25