Chartjs-Create legend from colors saved in const

0πŸ‘

βœ…

To achieve what you are trying to do you need to separate datasets ie. one for negative values and other for positive values.

var positiveData = datas.filter(function (value) { return value > 0; });
var negativeData = datas.filter(function (value) { return value < 0; });

var chart = new Chart(ctx, {
     type: 'bar',
     data: {
        labels: label,
        datasets: [{
          label: 'positive', //legend to change
          borderColor: 'green', 
          backgroundColor: 'green',
          data: positiveData 
        },
        {
          label: 'negative', //legend to change
          borderColor: 'red', 
          backgroundColor: 'red',
          data: negativeData 
        }]
     }
});

Leave a comment