Legend Configuration

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

    Position

    Position of the legend. Options are:

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

    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
    String"'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 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 fontSize, boxWidth is not used in this case).

    Legend Item Interface

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

    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:

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

    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.