Chartjs-How can i display multiple years of data from 1 project in Chart.js?

1👍

You haven’t said, or given us a way to easily see, what your problem is or what your code generates, but it looks as if you are passing "labels" an array with 12 values, and passing "data" a much longer array of 12 x number of years of data for that project within 1 dataset, which shouldn’t work.

I think you want to be passing in multiple datasets whose "data" arrays only contains 12 values each, corresponding with your 12 labels). For 1 project/multiple years, "datasets" should be something like:

  [{
        "label": "' . $year . '",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "data": [' . $ertrag['1975'] . '] // has exactly 12 values
    },{
        "label": "' . $year . '",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "data": [' . $ertrag['1976'] . '] // has exactly 12 values
    }...]

If that is the case, I would the $ertrag subroutine generate something that looks more like: $ertrag = [1976=>[123446,123235,345246,123445,123456,...],1977=>[112345,1234567,...]...]. Then you could generate datasets very easily.

Leave a comment