Vertical legend holder example
You may wish to display the legends vertically, for example to the right postItemContent: (legendName, legendIndex, color, colorIndex) => { // Vous pouvez utiliser tous les paramètres pour personnaliser le contenu switch(legendName) { case 'Sales': return `<div class="legend-note sales-note" style="color: ${color}"> Legend ${legendIndex + 1} - Including taxes </div>`; case 'Profit': return `<div class="legend-note profit-note"> Using color ${colorIndex + 1} - After adjustments </div>`; default: return ''; } }ph.
To do this, you can use the Apache ECharts legend.orient option:
legend: {
data: ['label 0', 'label 1', 'label 2'],
orient: 'vertical',
}
NB: This orientation could also be provided through
externalizeLegends() method providing in place of the CSS selector for the caption container an object including the selector and the orientation { legendHolderSelector: string; orientation: 'vertical' | 'horizontal';} :
themeManager.externalizeLegends(myChart, {legendHolderSelector: '#barChartSH_legend', orientation: 'vertical'});
In the example below, we prefer the Apache ECharts legend.orient method.
Title
Sub-Title
Specific legend labels
It is possible to use something else than the serie name as label in legends generated by ODS Charts.
To do this, you must use the Apache ECharts legend.formatter option:
legend: {
formatter: (name)=> name + ' zone'
}
Title
Sub-Title
Legends holders for stacked bars example
In the following example, we want to display the legends for a first set of stacked bars separately from those for a second set of stacked bars and the legend for a curve.
To do this, we pass as a parameter to the externalizeLegends() method not a CSS selector of the legend container but an array of containers defined by the {legendHolderSelector: string; orientation?: 'vertical' | 'horizontal'; seriesRef?: string[]} object.
legendHolderSelectoris the CSS selector of the legend holder.orientationoptionally indicates whether captions are displayed horizontally or vertically. By default, the value of thelegend.orientoption in Apache Echarts will be used if it exists, otherwise it will be'horizontal'.-
seriesRefis an array referencing the series whose legends are to be displayed in this legend container. The reference can be the series name, the series label or simply the stack name in the case of stacked bars.
IfseriesRefis absent, this container will be the default container for non-referenced legends.
We'll then have the following code to externalize our three types of legend into three different containers:
themeManager.externalizeLegends(
myChart,
[{ legendHolderSelector: '#results_stack_legend', seriesRef: ['result'], orientation: 'vertical' },
{ legendHolderSelector: '#goals_stack_legend', seriesRef: ['goals'], orientation: 'vertical' },
{ legendHolderSelector: '#line_legend'
}]
);
Title
Sub-Title
Legend holder with custom content example
In this example, we show a very simple example on how to add custom HTML content after the legend items using the postItemContent option.
The postItemContent property allows you to inject HTML content in three ways:
- As a function: content will be customized for each legend label
- As an object: content will be mapped directly to series names or legend labels
- As an array: content will be matched with legends by position (first array element for first legend, etc.)
For full illustration, have a look to the next use case, here will illustrate usage of the object mapping:
themeManager.externalizeLegends(
myChart,
{
legendHolderSelector: '#legend_with_custom_content',
postItemContent: {
'Label 1': '<small>(partial result)</small>'
}
}
);
Title
Sub-Title
Legend holder with custom content complex example
In this example, we demonstrate how to add custom HTML content to your legends using both postItemContent and afterLegendContent options.
There are two ways to add custom content to your legends:
- Using
postItemContentto add content after each individual legend item:- As a function: customize content for each legend label
- As an object: map series names or legend labels directly to their custom content
- Using
afterLegendContentto add content that appears after all legend items:- As a string: single piece of content displayed at the bottom of the legend group
Here's how to use both options:
// Example 1: Function-based postItemContent - Customize content per legend
themeManager.externalizeLegends(myChart, [
{
// Example 1: Function-based content - Dynamic content based on legend label
legendHolderSelector: '#legend_with_custom_content',
orientation: 'horizontal',
seriesRef: ['Revenue', 'Costs', 'Net Profit'],
postItemContent: (legendLabel, legendName, legendIndex, color, colorIndex) => {
switch(legendLabel) {
case 'Revenue':
return `<span class="metric-note revenue-note">
Gross revenue including taxes (${legendName} at index ${legendIndex + 1})
</span>`;
case 'Costs':
return `<span class="metric-note profit-note">
Operating expenses only (Using color ${color})
</span>`;
case 'Net Profit':
return `<span class="metric-note conversion-note">
After all deductions (Color palette index ${colorIndex + 1})
</span>`;
default:
return '';
}
}
]);
// Example 2: Object-based postItemContent - Map labels to content
themeManager.externalizeLegends(
myChart,
{
legendHolderSelector: '#legend_with_object_content',
orientation: 'horizontal',
seriesRef: ['Sales 2025', 'Sales 2024', 'Growth Rate'],
postItemContent: {
'Sales 2025': '<span class="metric-note revenue-note">Projected data</span>',
'Sales 2024': '<span class="metric-note profit-note">Historical data</span>',
'Growth Rate': '<span class="metric-note conversion-note">Year-over-year change</span>'
}
}
);
// Example 3: Array-based postItemContent - Position-based content
themeManager.externalizeLegends(
myChart,
{
legendHolderSelector: '#legend_with_object_content',
orientation: 'horizontal',
seriesRef: ['Sales 2025', 'Sales 2024', 'Growth Rate'],
postItemContent: {
'Sales 2025': '<span class="metric-note revenue-note">Projected data</span>',
'Sales 2024': '<span class="metric-note profit-note">Historical data</span>',
'Growth Rate': '<span class="metric-note conversion-note">Year-over-year change</span>'
}
}
);
// Example 4: Using afterLegendContent - Add global content after all legends
themeManager.externalizeLegends(
myChart,
{
legendHolderSelector: '#legend_with_string_content',
orientation: 'horizontal',
seriesRef: ['Site Visits', 'Conversions', 'Success Rate'],
afterLegendContent: '<div class="global-note">Data from our analytics platform - Updated daily</div>'
}
);