Chart2 Direct Legend Font Only

Overview

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

Here we will see how to set options that are seen in Fig. 702.

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 ooodev.format.inner.direct.write.char.font.font_effects import FontLine
from ooodev.calc import CalcDoc, ZoomKind
from ooodev.loader.lo import Lo
from ooodev.utils.color import StandardColor
from ooodev.utils.data_type.color_range import ColorRange

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_font(name="Liberation Sans Narrow", size=13)

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

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

Apply the Font

Before formatting the chart is visible in Fig. 833.

# ... other code
_ = legend.style_font(name="Liberation Sans Narrow", size=13)

Running the above code will produce the following output shown in Fig. 522 and Fig. 523.

Chart with Title Font set

Fig. 522 Chart with Title Font set

Chart Data Labels Dialog Font

Fig. 523 Chart Data Labels Dialog Font

Get the font for the Legend

# ... other code

f_style = legend.style_font_get()
assert f_style is not None