Legend Configuration

The chart legend displays data about the datasets that are appearing on the chart.

Configuration options

The legend configuration is passed into the options.legend namespace. The global options for the chart legend is defined in Chart.defaults.global.legend.

NameTypeDefaultDescription
displaybooleantrueIs the legend shown?
positionstring‘top’Position of the legend. more…
alignstring‘center’Alignment of the legend. more…
fullWidthbooleantrueMarks that this box should take the full width of the canvas (pushing down other boxes). This is unlikely to need to be changed in day-to-day use.
onClickfunctionA callback that is called when a click event is registered on a label item.
onHoverfunctionA callback that is called when a ‘mousemove’ event is registered on top of a label item.
onLeavefunctionA callback that is called when a ‘mousemove’ event is registered outside of a previously hovered label item.
reversebooleanfalseLegend will show datasets in reverse order.
labelsobjectSee the Legend Label Configuration section below.
rtlbooleantrue for rendering the legends from right to left.
textDirectionstringcanvas’ defaultThis will force the text direction 'rtl'</td><td>'ltr on the canvas for rendering the legend, regardless of the css specified on the canvas

Position

Position of the legend. Options are:

  • 'top'
  • 'left'
  • 'bottom'
  • 'right'

Align

Alignment of the legend. Options are:

  • 'start'
  • 'center'
  • 'end'

Defaults to 'center' for unrecognized values.

Legend Label Configuration

The legend label configuration is nested below the legend configuration using the labels key.

NameTypeDefaultDescription
boxWidthnumber40Width of coloured box.
fontSizenumber12Font size of text.
fontStylestring‘normal’Font style of text.
fontColorColor‘#666’Color of text.
fontFamilystring“‘Helvetica Neue’, ‘Helvetica’, ‘Arial’, sans-serif”Font family of legend text.
paddingnumber10Padding between rows of colored boxes.
generateLabelsfunctionGenerates legend items for each thing in the legend. Default implementation returns the text + styling for the color box. See Legend Item for details.
filterfunctionnullFilters legend items out of the legend. Receives 2 parameters, a Legend Item and the chart data.
usePointStylebooleanfalseLabel style will match corresponding point style (size is based on the mimimum value between boxWidth and fontSize).

Legend Item Interface

Items passed to the legend onClick function are the ones returned from labels.generateLabels. These items must implement the following interface.

  1. {
  2. // Label that will be displayed
  3. text: string,
  4. // Fill style of the legend box
  5. fillStyle: Color,
  6. // If true, this item represents a hidden dataset. Label will be rendered with a strike-through effect
  7. hidden: boolean,
  8. // For box border. See https://developer.mozilla.org/en/docs/Web/API/CanvasRenderingContext2D/lineCap
  9. lineCap: string,
  10. // For box border. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
  11. lineDash: number[],
  12. // For box border. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
  13. lineDashOffset: number,
  14. // For box border. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin
  15. lineJoin: string,
  16. // Width of box border
  17. lineWidth: number,
  18. // Stroke style of the legend box
  19. strokeStyle: Color,
  20. // Point style of the legend box (only used if usePointStyle is true)
  21. pointStyle: string | Image,
  22. // Rotation of the point in degrees (only used if usePointStyle is true)
  23. rotation: number
  24. }

Example

The following example will create a chart with the legend enabled and turn all of the text red in color.

  1. var chart = new Chart(ctx, {
  2. type: 'bar',
  3. data: data,
  4. options: {
  5. legend: {
  6. display: true,
  7. labels: {
  8. fontColor: 'rgb(255, 99, 132)'
  9. }
  10. }
  11. }
  12. });

Custom On Click Actions

It can be common to want to trigger different behaviour when clicking an item in the legend. This can be easily achieved using a callback in the config object.

The default legend click handler is:

  1. function(e, legendItem) {
  2. var index = legendItem.datasetIndex;
  3. var ci = this.chart;
  4. var meta = ci.getDatasetMeta(index);
  5. // See controller.isDatasetVisible comment
  6. meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
  7. // We hid a dataset ... rerender the chart
  8. ci.update();
  9. }

Lets say we wanted instead to link the display of the first two datasets. We could change the click handler accordingly.

  1. var defaultLegendClickHandler = Chart.defaults.global.legend.onClick;
  2. var newLegendClickHandler = function (e, legendItem) {
  3. var index = legendItem.datasetIndex;
  4. if (index > 1) {
  5. // Do the original logic
  6. defaultLegendClickHandler(e, legendItem);
  7. } else {
  8. let ci = this.chart;
  9. [
  10. ci.getDatasetMeta(0),
  11. ci.getDatasetMeta(1)
  12. ].forEach(function(meta) {
  13. meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
  14. });
  15. ci.update();
  16. }
  17. };
  18. var chart = new Chart(ctx, {
  19. type: 'line',
  20. data: data,
  21. options: {
  22. legend: {
  23. onClick: newLegendClickHandler
  24. }
  25. }
  26. });

Now when you click the legend in this chart, the visibility of the first two datasets will be linked together.

HTML Legends

Sometimes you need a very complex legend. In these cases, it makes sense to generate an HTML legend. Charts provide a generateLegend() method on their prototype that returns an HTML string for the legend.

To configure how this legend is generated, you can change the legendCallback config property.

  1. var chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. legendCallback: function(chart) {
  6. // Return the HTML string here.
  7. }
  8. }
  9. });

Note that legendCallback is not called automatically and you must call generateLegend() yourself in code when creating a legend using this method.