Skip to content

Right-to-left support

This tutorial shows how to lay out labels, legends and other containers for right-to-left (RTL) languages, such as Arabic or Hebrew.

Since version 5.22.0, set direction when creating the root element:

const root = am5.Root.new("chartdiv", {
direction: "rtl"
});
  • Text with no direction of its own is drawn right to left.
  • Legends lay out from the right: the first item is at the top right, and each item has its marker on the right of its name.
  • "ltr" keeps everything left to right, e.g. for a chart in English on a page with dir="rtl".
  • Not set, text follows the direction of the chart’s element, e.g. a page with dir="rtl", and legends stay left to right.

The direction can only be set when the root element is created.

Open in:New tabChart Maker (opens in a new tab)
Right-to-left chart

To enable RTL on a single label, set its direction to "rtl". With the root’s direction: "rtl", that’s the default for every label without a direction of its own.

Let’s take this label definition (used for a pie chart slice):

pieSeries.labels.template.setAll({
text: "[bold]فاكهة[/]\n{category}: {value.percent.formatNumber('#.#')}%"
});

When the browser’s rendering engine encounters RTL text within regular text, its own ordering rules kick in, and the label comes out in the wrong order:

Pie chart with Arabic labels, the percent sign misplaced before the category name
Incorrect result (direction = 'ltr' - default)

Enabling RTL fixes it by properly laying out the labels:

pieSeries.labels.template.setAll({
text: "[bold]فاكهة[/]\n{category}: {value.percent.formatNumber('#.#')}%",
direction: "rtl"
});
Pie chart with Arabic labels laid out right to left in the correct order
Correct result (direction = 'rtl')

A label with a width aligns its text inside that box with textAlign. "left" and "right" are the box’s sides. "start" and "end" follow the direction:

textAlign direction: "ltr" direction: "rtl"
"left" Left Left
"right" Right Right
"start" Left Right
"end" Right Left
"center" Center Center

As we can see from the above screenshots, enabling RTL reverses order of blocks, including putting the "%" sign to the other side of the number.

This is true for the mixed-language content only.

If our labels would contain only Latin (left-to-right) text or numbers/symbols, it would remain as is: rtl setting would have no effect over it.

Pie chart with percentage-only labels, the percent sign after the number
For labels with just numbers and symbols direction setting is ignored

Another thing that does not respect rtl setting is inline formatting block syntax.

One might expect the opening and closing blocks of an RTL label to work from right to left as well, e.g. ... [/]xxxxx[bold] ... to make “xxxxx” bold.

That is not how it works. The formatting block (i.e. [bold]) always affects text after it, until it encounters closing block ([/]).

So, the correct usage is: ... [bold]xxxxx[/] ....

Since version 5.22.0, text with no direction of its own follows the direction of the chart’s element. On a page with dir="rtl", it’s drawn right to left, in its place. To keep it left to right, set the root’s direction to "ltr".

With the root’s direction: "rtl", legends need no settings of their own. See “Whole chart”.

Since version 5.22.0, a single legend can be set up for an RTL language with its own direction setting:

let legend = chart.children.push(am5.Legend.new(root, {
centerX: am5.p50,
x: am5.p50,
direction: "rtl"
}));
legend.itemContainers.template.set("direction", "rtl");
legend.labels.template.set("direction", "rtl");
legend.valueLabels.template.set("textAlign", "right");
legend.data.setAll(chart.series.values);
Setting Effect
Legend’s direction: "rtl" Items go from right to left. The first one is at the top right, and a part-filled last row is on the right.
itemContainers.template direction: "rtl" In each item, the marker is on the right, then the name, then the value.
labels.template direction: "rtl" Needed for names in an RTL language, unless the page has dir="rtl". See “Labels”.
valueLabels.template textAlign: "right" Keeps each value next to its name. Without it, the values line up at the left.

direction works on any container with a horizontal or grid layout, not only legends. See “Containers: Right to left”.