Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legends for line charts in Chart.js

Tags:

chart.js

I'd like to customize a legend for line data so that the legend graphic is a line (styled like the actually data line) rather than a box.

As far as I can tell from the source, the graphic can be a point or a box, and the height of the box is fixed to the font size. The 'generateLabels' option does not seem to allow for extending around these contraints.

Version 2.2.1.

Thanks for any help.

like image 388
Joel Skrepnek Avatar asked Aug 25 '16 22:08

Joel Skrepnek


People also ask

What are legends in chart?

Most charts use some kind of a legend to help readers understand the charted data. Whenever you create a chart in Excel, a legend for the chart is automatically generated at the same time. A chart can be missing a legend if it has been manually removed from the chart, but you can retrieve the missing legend.

How do you wrap a legend text in ChartJS?

To make this behave the same as standard Chart. js charts, the function onLegendClicked is invoked when a mouse click occurs on a legend label. This function toggles the hidden state of individual datasets and changes label text style between normal and strike-through.

What are plugins in chart JS?

Plugins are the most efficient way to customize or change the default behavior of a chart. They have been introduced at version 2.1. 0 (global plugins only) and extended at version 2.5. 0 (per chart plugins and options).

What is a chart legend in Excel?

Legends are a small visual representation of the chart's data series to understand each without confusion. Legends are directly linked to the chart data range and change accordingly. In simple terms, if the data includes many colored visuals, legends show what each visual label means.


1 Answers

You can make line legend by changing width of legend box (for example 2px), it will be vertical line but it's looks nice too

plugins: {
   legend: {
       display: true,
       labels: {
           boxWidth: 2
       }
    }
}
like image 164
koa73 Avatar answered Oct 01 '22 02:10

koa73