[Chartjs]-Chart.js โ€“ Styling Legend

39๐Ÿ‘

โœ…

No need to use legendCallback function. You can set usePointStyle = true to turn that rectangle into a circle.

Chart.defaults.global.legend.labels.usePointStyle = true;

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: 'Link One',
            data: [1, 2, 3, 2, 1, 1.5, 1],
            backgroundColor: [
                '#D3E4F3'
            ],
            borderColor: [
                '#D3E4F3',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        legend: {
            display: true,
            position: 'bottom',
            labels: {
                fontColor: '#333'
            }
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<div class="container">
  <canvas id="myChart"></canvas>
</div>

22๐Ÿ‘

for angular4-chart.js you could use the options attribute like so:

options = {
      legend:{
        display: true,
        labels: {
          usePointStyle: true,
        }
      }
}

12๐Ÿ‘

Step 1:
Change options to this:

options: {
    legend: {
        display: false,
    }
}

Step 2:
Append to your canvas this code (just after canvas):

<div id='chartjsLegend' class='chartjsLegend'></div> //Or prepend to show the legend at top, if you append then it will show to bottom.

Step 3:
Generate this legend instead of default with this (just after mychart):

document.getElementById('chartjsLegend').innerHTML = myChart.generateLegend();

Step 4:
Make css so it generates as circle:

.chartjsLegend li span {
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 5px;
    border-radius: 25px;
}

Step 5:
Change css with what ever you feel like should be better.
Time for some chimichangas now.

7๐Ÿ‘

Use usePointStyle:true

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: 'Link One',
        data: [1, 2, 3, 2, 1, 1.5, 1],
        backgroundColor: [
            '#D3E4F3'
        ],
        borderColor: [
            '#D3E4F3',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)',
            'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1
    }]
},
options: {
    legend: {
        display: true,
      position: 'bottom',
        labels: {
            fontColor: '#333',
            usePointStyle:true
        }
    }
}

});

4๐Ÿ‘

Additional information on @GRUNT โ€˜s answer
I assign usePointStyle inside legend.labels not what @GRUNT did Chart.defaults.global.legend.labels.usePointStyle = true;

This is useful when you are using react

legend: {
    labels: {
      usePointStyle: true,
    },
  }

more info here
Display point style on legend in chart.js

2๐Ÿ‘

add this in your options

 legend: {
display: true,
          position: 'bottom',
          labels: {
boxWidth: 9, 
            fontColor: '#474747',
            fontFamily: '6px Montserrat',
          },
        },

for example :

 this.testChart = new Chart('testPie', {
      type: 'doughnut',
      data: {
        labels: [ 'Success','Failure','Aborted'],
        datasets: [
          {
            data: [],
            backgroundColor: [ '#45D78F', '#F58220','#FFD403'],
          },
        ],
      },
      options: {
        maintainAspectRatio: false,
        cutoutPercentage: 50,    // for thikness of doughnut
        title: {
          display: false,
          text: 'Runs',
          fontSize: 14,
          fontFamily: 'Roboto',
          fontColor: '#474747',
        },
        legend: {
          display: true,
          position: 'bottom',
          labels: {
            boxWidth: 9,
            fontColor: '#474747',
            fontFamily: '6px Montserrat',
          },
        },
      },
    });

1๐Ÿ‘

all the above answers work with older chart js.
for a newer version, put the legend (and btw title) definitions into options.plugins {} and not in the options {}.

Leave a comment