Skip to content

Non-standard legend alignment

You can use position to place chart Legend on top, bottom, left, or right. In all those cases, Legend will center across the edge it is placed on. This tutorial will show some other alignment options.

When legend position is set to either "left" or "right", it is aligned vertically to be aligned to middle of the chart.

Or, more precisely, its valign property is set to "middle".

If we want it to rather stick to the top or bottom, we need to update its valign accordingly, e.g.:

chart.legend.position = "right";chart.legend.valign = "top";
  • Pie chart with the legend to its right, aligned to the top
  • Pie chart with the legend to its right, vertically centered
  • Pie chart with the legend to its right, aligned to the bottom

Whenever legend is placed on top or bottom, fuzzy logic behind it automatically sets its width to 100%.

This makes intuitively using align pretty useless, unless you want to set width to the legend as well, which might not be a trivial task.

In this case we have another setting: contentAlign. It sets alignment of elements (legend items) in a container. By default it’s set to "center", hence top/bottom legends coming across as centered.

Therefore, to set alignment of such Legend, we need to set contentAlign:

chart.legend.position = "top";chart.legend.contentAlign = "right";
  • Pie chart with the legend above it, aligned left
  • Pie chart with the legend above it, centered
  • Pie chart with the legend above it, aligned right
Open in:New tab
aligning legend