Chart2 Direct Legend Transparency

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_area_transparency_transparency() method can be used to set the transparency of the legend.

Setup

General setup for examples.

from __future__ import annotations
from pathlib import Path
import uno
from ooo.dyn.awt.gradient_style import GradientStyle
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_area_color(StandardColor.GREEN_LIGHT2)
        _ = legend.style_area_transparency_transparency(50)

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

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

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

.. only:: html

    .. cssclass:: tab-none

        .. group-tab:: None

Transparency

Before formatting the chart is seen in Fig. 833.

Setting Transparency

The style_area_transparency_transparency() method can be used to set the transparency of a chart legend.

The Transparency needs a background color in order to view the transparency. See: Chart2 Direct Legend Area.

# ... other code
_ = legend.style_area_color(StandardColor.GREEN_LIGHT2)
_ = legend.style_area_transparency_transparency(50)

The results can bee seen in Fig. 526 and Fig. 527.

Chart with transparency applied to legend

Fig. 526 Chart with transparency applied to legend

Chart Legend Transparency Dialog

Fig. 527 Chart Legend Transparency Dialog

Getting Transparency

# ... other code
f_style = legend.style_area_transparency_transparency_get()
assert f_style is not None

Gradient Transparency

Before formatting the chart is seen in Fig. 833.

Setting Gradient

The ooodev.format.chart2.direct.legend.transparency.Gradient class can be used to set the gradient transparency of a legend.

Like the Transparency the Gradient Transparency needs a background color in order to view the transparency. See: Chart2 Direct Legend Area.

from ooodev.utils.data_type.intensity_range import IntensityRange
# ... other code

_ = legend.style_area_color(StandardColor.GREEN_LIGHT2)
_ = legend.style_area_transparency_gradient(
    angle=90, grad_intensity=IntensityRange(0, 100)
)

The results can bee seen in Fig. 528 and Fig. 529.

Chart with legend gradient transparency

Fig. 528 Chart with legend gradient transparency

Chart Legend Gradient Transparency Dialog

Fig. 529 Chart Legend Gradient Transparency Dialog