Chartjs-Missing colors in my Chart.js, my data looks fine, but no color

1👍

You are setting the data incorrectly.

You should use

data: [
    $arrStemmer[0], 
    $arrStemmer[1], 
    $arrStemmer[2], 
    $Snit_Vaerdi
],

Instead of

data: [
    [$arrStemmer[0]], 
    [$arrStemmer[1]], 
    [$arrStemmer[2]], 
    [$Snit_Vaerdi]
],

0👍

This should work. if the $arrStemmer and $snit_Vaerdi are filled.

let $stemmer = document.getElementById('can_afstemning_graf').getContext('2d');

let myChart = new Chart($stemmer, {
    type: 'bar',
    data: {
        labels: ['Super mad', 'Middel', 'Ikke ok', 'Gennemsnit'],
        datasets: [{
            label: 'Antal stemmer',
            data: [
                $arrStemmer[0],
                $arrStemmer[1],
                $arrStemmer[2],
                $Snit_Vaerdi
            ],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});

Leave a comment