How to change the color of legend in chartjs and be able to add one more legend?
Asked Answered
E

3

7

I want to change the color of legend, because I want different colors to represent different situations.But now the legend uses just the first color I set on the bar chart.

I also want to ask, is it able to set one more legend?Like pink is for situationA, blue for B?

Here is the link:

Code is here Can anyone help?Thank you so much.

Expanded answered 4/6, 2017 at 9:2 Comment(0)
B
15

To set more legends, you need to add multiple datasets. Each dataset will represent one legend and the legend­'s color will automatically be set according to the dataset­'s background color.

Chart.plugins.register({
   beforeDraw: function(c) {
      var legends = c.legend.legendItems;
      legends.forEach(function(e) {
         e.fillStyle = '#07C';
      });
   }
});

var canvas = document.getElementById('myChart');

var data = {
   labels: ["January", "February", "March", "April", "May", "June"],
   datasets: [{
      label: "My First dataset",
      backgroundColor: "rgba(255,99,132,0.4)",
      hoverBorderColor: "rgba(255,99,132,1)",
      data: [65, 59, 20, 81, 56, 55],
   }, {
      label: "My Second dataset",
      backgroundColor: "rgba(25,25,255,0.4)",
      hoverBorderColor: "rgba(255,99,132,1)",
      data: [65, 59, 20, 81, 56, 55],
   }]
};

var option = {
   scales: {
      xAxes: [{
         stacked: true
      }],
      yAxes: [{
         stacked: true,
         gridLines: {
            display: true,
            color: "rgba(255,99,132,0.2)"
         }
      }]
   }
};

var myBarChart = Chart.Bar(canvas, {
   data: data,
   options: option
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>
Barbados answered 4/6, 2017 at 9:20 Comment(4)
Thank you for your answer! But is it able to only change the color of legend by a function? Thanks for your time.Expanded
Yes it's possible. You need to create a chart plugin to accomplish that. Check updated answer.Huberman
Cool, Thank you very much!Expanded
Hi @Barbados . The background of the legend is transparent if you use Stacked bar graph with 3 datasets. Any idea about this?Lolalolande
S
1

To do this in react-chartjs2

        <Line
          data={this.data}
          options={this.options}
          redraw={true}
          plugins={[
            {
              beforeDraw(c) {
                var legends = c.legend.legendItems;
                legends[0].fillStyle = "#103362";
                legends[1].fillStyle = "#3F80D8";
              }
            }
          ]}
        />

       options = {
       responsive: true,
       maintainAspectRatio: false,
       legend: {
         labels: {
           usePointStyle: true,
           boxWidth: 9
         }
       }
      }
Sherwoodsherwynd answered 28/12, 2022 at 5:32 Comment(0)
H
0

To do it with ChartJS v3 (3.7.1 in my case), you can use the legend plugin like that:

{
legend: {
    display: true,
    position: 'top',
    labels: {
        generateLabels: chart => {
            return [
                {
                    datasetIndex: 0,
                    text: name,
                    fontColor: color1,
                    fillStyle: color1,
                    strokeStyle: color1
                },
                {
                    datasetIndex: 0,
                    text: name,
                    fontColor: color2,
                    fillStyle: color2,
                    strokeStyle: color2
                }
            ];
        }
    }
}
}

In this case, the 2 labels will use the same color for the rectangle, the border and the text.

Heckman answered 1/8 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.