Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 778
Jane Avatar asked Jun 04 '17 09:06

Jane


People also ask

How do you customize a legend in ChartJS?

Adding a legend in chartJS 3.0+ requires the use of plugins. Basically you can create a custom plugin when you instantiate the chart. For example: const context = document.

How do you hide a legend in ChartJS?

To remove legend on charts with Chart. js v2 and JavaScript, we can set the options. legend to false . const chart1 = new Chart(canvas, { type: "pie", data: data, options: { legend: { display: false, }, tooltips: { enabled: false, }, }, });

How do I create a chart with the legend enabled?

The following example will create a chart with the legend enabled and turn all of the text red in color. var chart = new Chart (ctx, { type: 'bar', data: data, options: { legend: { display: true, labels: { fontColor: 'rgb (255, 99, 132)' } } } }); Custom On Click Actions

How do you sort legend items in a chart?

Sorts legend items. Type is : sort (a: LegendItem, b: LegendItem, data: ChartData): number;. Receives 3 parameters, two Legend Items and the chart data. The return value of the function is a number that indicates the order of the two legend item parameters.

How do I change the color of a chart in JavaScript?

You can specify the color as a string in hexadecimal, RGB, or HSL notations. If a color is needed, but not specified, Chart.js will use the global default color. There are 3 color options, stored at Chart.defaults, to set: Background color.

How do I set more than one legend?

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.


1 Answers

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>
like image 155
ɢʀᴜɴᴛ Avatar answered Oct 06 '22 16:10

ɢʀᴜɴᴛ