[Chartjs]-Conditional Series Failing – Chart.js

2πŸ‘

βœ…

What you were attempting was not valid javascript syntax because you cannot put an if statement in the middle of an object declaration. Try something more like this:

var datasets = [
    {
        label: 'Branch '+b7,
        data: B7,
        fill: false,
        backgroundColor: 'rgb(0, 0, 0)',
        borderColor: 'rgb(0, 0, 0)'
    }
];
if(b8) {
    datasets.push({
        label: 'Branch '+b8,
        data: B8,
        fill: false,
        backgroundColor: 'rgb(0, 0, 0)',
        borderColor: 'rgb(0, 0, 0)',
    });
}
if(b9){
    datasets.push({
        label: 'Branch '+b9,
        data: B9,
        fill: false,
        backgroundColor: 'rgb(0, 0, 0)',
        borderColor: 'rgb(0, 0, 0)',
   });
}

new Chart(document.getElementById('chart'),{
    type: 'line',
    data: {
        labels: time,
        datasets: datasets
    }
});

Leave a comment