Chart2 Direct Legend Position

Overview

The Legend parts of a Chart can be styled using the various style_* methods of the ChartLegend class.

Here we will see how the style_position() method can be used to set the position of the legend.

Setup

General setup for this example.

from __future__ import annotations
from pathlib import Path
import uno
from ooo.dyn.awt.gradient_style import GradientStyle
from ooo.dyn.chart2.legend_position import LegendPosition
from ooodev.loader.lo import Lo
from ooodev.utils.color import StandardColor
from ooodev.utils.data_type.color_range import ColorRange
from ooodev.calc import CalcDoc, ZoomKind
from ooodev.format.inner.direct.chart2.title.alignment.direction import DirectionModeKind

def main() -> int:
    with Lo.Loader(connector=Lo.ConnectPipe()):
        fnm = Path.cwd() / "tmp" / "piechart.ods"
        doc = CalcDoc.open_doc(fnm=fnm, visible=True)
        Lo.delay(500)
        doc.zoom(ZoomKind.ZOOM_100_PERCENT)

        sheet = doc.sheets[0]
        sheet["A1"].goto()
        chart_table = sheet.charts[0]
        chart_doc = chart_table.chart_doc
        _ = chart_doc.style_border_line(
            color=StandardColor.BRICK,
            width=1,
        )
        _ = chart_doc.style_area_gradient(
            step_count=64,
            style=GradientStyle.SQUARE,
            angle=45,
            grad_color=ColorRange(
                StandardColor.GREEN_DARK4,
                StandardColor.TEAL_LIGHT2,
            ),
        )
        legend = chart_doc.first_diagram.get_legend()
        if legend is None:
            raise ValueError("Legend is None")

        _ = legend.style_position(
            pos=LegendPosition.PAGE_END,
            no_overlap=True,
            mode=DirectionModeKind.LR_TB,
        )

        Lo.delay(1_000)
        doc.close()
    return 0

if __name__ == "__main__":
    SystemExit(main())

Setting the Legend Position

from ooo.dyn.chart2.legend_position import LegendPosition
from ooodev.format.inner.direct.chart2.title.alignment.direction import DirectionModeKind
# ... other code

_ = legend.style_position(
    pos=LegendPosition.PAGE_END,
    no_overlap=True,
    mode=DirectionModeKind.LR_TB,
)

The results are visible in Fig. 524 and Fig. 525.

Chart with Legend Set to Bottom

Fig. 524 Chart with Legend Set to Bottom

Chart Legend Position Dialog

Fig. 525 Chart Legend Position Dialog