Chart2 Direct Legend Area

Overview

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

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_transparency_transparency(0)
        _ = legend.style_area_color(color=StandardColor.GREEN_LIGHT2)

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

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

Color

The style_area_color() method is used to set the background color of the legend. The style_area_transparency_transparency() method is used to set the transparency of the legend.

Before formatting the chart is visible in Fig. 833.

In order for the background color to be visible the transparency of the legend must be set. See also: Chart2 Direct Legend Transparency.

# ... other code
# set the transparency of the legend to 0 and the color to green light2
_ = legend.style_area_transparency_transparency(0)
_ = legend.style_area_color(color=StandardColor.GREEN_LIGHT2)

The results are visible in Fig. 505 and Fig. 506.

Chart with Legend Area Color set

Fig. 505 Chart with Legend Area Color set

Chart Legend Area Color Dialog

Fig. 506 Chart Legend Area Color Dialog

Gradient

The style_area_gradient_from_preset() method is used to set the Legend gradient of a Chart.

Before formatting the chart is visible in Fig. 833.

In order for the gradient to be visible the transparency of the legend must be set. See also: Chart2 Direct Legend Transparency.

Gradient from preset

The PresetGradientKind enum is used to select the preset gradient.

from ooodev.format.inner.preset.preset_gradient import PresetGradientKind
# ... other code

_ = legend.style_area_transparency_transparency(0)
_ = legend.style_area_gradient_from_preset(
    preset=PresetGradientKind.NEON_LIGHT,
)

The results are visible in Fig. 507 and Fig. 508.

Chart with gradient Legend

Fig. 507 Chart with gradient Legend

Chart Area Legend Gradient Dialog

Fig. 508 Chart Area Legend Gradient Dialog

Apply a custom Gradient

Demonstrates how to create a custom gradient.

from ooo.dyn.awt.gradient_style import GradientStyle
from ooodev.utils.data_type.color_range import ColorRange
# ... other code

_ = legend.style_area_transparency_transparency(0)
_ = legend.style_area_gradient(
    step_count=64,
    style=GradientStyle.SQUARE,
    angle=45,
    grad_color=ColorRange(StandardColor.BRICK_LIGHT1, StandardColor.TEAL_DARK1),
)

The results are visible in Fig. 509 and Fig. 510.

Chart Legend area with gradient Legend

Fig. 509 Chart Legend area with gradient Legend

Chart Legend Area Gradient Dialog

Fig. 510 Chart Legend Area Gradient Dialog

Image

The style_area_image_from_preset() method is used to set the background image of the Legend.

Before formatting the chart is visible in Fig. 833.

In order for the image to be visible the transparency of the legend must be set. See also: Chart2 Direct Legend Transparency.

The PresetImageKind enum is used to select an image preset.

from ooodev.format.inner.preset.preset_image import PresetImageKind
# ... other code

_ = legend.style_area_transparency_transparency(0)
_ = legend.style_area_image_from_preset(
    preset=PresetImageKind.PARCHMENT_PAPER,
)

The results are visible in Fig. 511 and Fig. 512.

Chart Legend with background image

Fig. 511 Chart Legend with background image

Chart Area Legend Image Dialog

Fig. 512 Chart Area Legend Image Dialog

Pattern

The style_area_pattern_from_preset() method is used to set the background pattern of a Chart.

Before formatting the chart is visible in Fig. 833.

The PresetPatternKind enum is used to select a pattern preset.

In order for the pattern to be visible the transparency of the legend must be set. See also: Chart2 Direct Legend Transparency.

from ooodev.format.inner.preset.preset_pattern import PresetPatternKind
# ... other code

_ = legend.style_area_transparency_transparency(0)
_ = legend.style_area_pattern_from_preset(
    preset=PresetPatternKind.HORIZONTAL_BRICK,
)

The results are visible in Fig. 513 and Fig. 514.

Chart Legend with pattern

Fig. 513 Chart Legend with pattern

Chart Area Legend Pattern Dialog

Fig. 514 Chart Area Legend Pattern Dialog

Hatch

The style_area_hatch_from_preset() method is used to set the Title and Subtitle hatch of a Chart.

Before formatting the chart is visible in Fig. 833.

The PresetHatchKind enum is used to select a hatch preset.

In order for the hatch to be visible the transparency of the legend must be set. See also: Chart2 Direct Legend Transparency.

from ooodev.format.inner.preset.preset_hatch import PresetHatchKind
# ... other code

_ = legend.style_area_transparency_transparency(0)
_ = legend.style_area_hatch_from_preset(
    preset=PresetHatchKind.YELLOW_45_DEGREES_CROSSED,
)

The results are visible in Fig. 515 and Fig. 516.

Chart Legend with hatch

Fig. 515 Chart Legend with hatch

Chart Title Hatch Dialog

Fig. 516 Chart Title Hatch Dialog