A custom formatter function that returns text to be displayed inside individual legend items.
var chart = new CanvasJS.Chart("container", { . . legend:{ itemTextFormatter: function ( e ) { return "Custom Legend " + e.dataPoint.label; } }, . . }); chart.render();
By using itemTextFormatter, you can return any value to be displayed inside legendItems.
Below is the structure of object passed as parameter to the function:
e: { // parameter sent to function chart, legend, dataSeries, dataPoint // Available only in case of pie / doughnut }
Also See:
7 Comments
Any chance of adding HTML encoding support to the legendText or itemTextFormatter? I’m trying to render a element icon before each legend item, but it always just outputs the HTML as a part of the string. Another option is to allow custom images/svgs as the dataSeries marker…?
Jake,
Its not possible to add HTML content to legend, as of now.
is it possible support multiple legend box with single legend Text?
i means like following
[REDBOX] [BLUE_BOX] [PINK_BOX] : legendText
Lalit,
Sorry as of now legend supports only one marker. As a workaround you can check this example.
Thanks for fast reply,
i fixed problem but one more thing legendText propertie support html?
i need something like following with column chart
[REDBOX] [BLUE_BOX] [PINK_BOX] : legendText1
[REDBOX] [BLUE_BOX] [PINK_BOX] : legendText2
[REDBOX] [BLUE_BOX] [PINK_BOX] : legendText3
Legend text doesn’t support HTML properties. So, implementing custom legend with HTML you can customize it according to your requirement. Here is an example of custom legend implementation.