Chart2 Direct Legend Area (Static)

Overview

The ooodev.format.chart2.direct.legend.area module is used to format the Legend parts of a Chart.

Calls to the Chart2.style_legend() and method is used to style legend.

Setup

General setup for examples.

import uno
from ooodev.format.chart2.direct.legend.area import Color as LegendAreaColor
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
from ooodev.format.chart2.direct.general.borders import LineProperties as ChartLineProperties
from ooodev.format.chart2.direct.general.area import Gradient as ChartGradient
from ooodev.format.chart2.direct.general.area import GradientStyle, ColorRange
from ooodev.office.calc import Calc
from ooodev.office.chart2 import Chart2
from ooodev.utils.color import StandardColor
from ooodev.gui import GUI
from ooodev.utils.kind.zoom_kind import ZoomKind
from ooodev.loader.lo import Lo


def main() -> int:
    with Lo.Loader(connector=Lo.ConnectPipe()):
        doc = Calc.open_doc("pie_chart.ods")
        GUI.set_visible(True, doc)
        Lo.delay(500)
        Calc.zoom(doc, ZoomKind.ZOOM_100_PERCENT)

        sheet = Calc.get_active_sheet()

        Calc.goto_cell(cell_name="A1", doc=doc)
        chart_doc = Chart2.get_chart_doc(sheet=sheet, chart_name="pie_chart")

        chart_bdr_line = ChartLineProperties(color=StandardColor.BRICK, width=1)
        chart_grad = ChartGradient(
            chart_doc=chart_doc,
            step_count=64,
            style=GradientStyle.SQUARE,
            angle=45,
            grad_color=ColorRange(StandardColor.GREEN_DARK4, StandardColor.TEAL_LIGHT2),
        )
        Chart2.style_background(chart_doc=chart_doc, styles=[chart_grad, chart_bdr_line])

        legend_color_style = LegendAreaColor(color=StandardColor.GREEN_LIGHT2)
        legend_bg_transparency_style = LegendTransparency(0)
        Chart2.style_legend(
            chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_color_style]
        )

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


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

Color

The ooodev.format.chart2.direct.legend.area.Color class is used to set the background color of a Chart 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.

from ooodev.format.chart2.direct.legend.area import Color as LegendAreaColor
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
# ... other code

legend_color_style = LegendAreaColor(color=StandardColor.GREEN_LIGHT2)
legend_bg_transparency_style = LegendTransparency(0)
Chart2.style_legend(
    chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_color_style]
)

The results are visible in Fig. 684 and Fig. 685.

Chart with Legend Area Color set

Fig. 684 Chart with Legend Area Color set

Chart Legend Area Color Dialog

Fig. 685 Chart Legend Area Color Dialog

Gradient

The ooodev.format.chart2.direct.legend.area.Gradient class 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.chart2.direct.legend.area import Gradient as LegendAreaGradient
from ooodev.format.chart2.direct.legend.area import Gradient as PresetGradientKind
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
# ... other code

legend_area_gradient_style = LegendAreaGradient.from_preset(
    chart_doc=chart_doc, preset=PresetGradientKind.NEON_LIGHT
)
legend_bg_transparency_style = LegendTransparency(0)
Chart2.style_legend(
    chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_area_gradient_style]
)

The results are visible in Fig. 686 and Fig. 687.

Chart with gradient Legend

Fig. 686 Chart with gradient Legend

Chart Area Legend Gradient Dialog

Fig. 687 Chart Area Legend Gradient Dialog

Apply a custom Gradient

Demonstrates how to create a custom gradient.

from ooodev.format.chart2.direct.legend.area import Gradient as LegendAreaGradient
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
# ... other code

legend_area_gradient_style = LegendAreaGradient(
    chart_doc=chart_doc,
    step_count=64,
    style=GradientStyle.SQUARE,
    angle=45,
    grad_color=ColorRange(StandardColor.BRICK_LIGHT1, StandardColor.TEAL_DARK1),
)
legend_bg_transparency_style = LegendTransparency(0)
Chart2.style_legend(
    chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_area_gradient_style]
)

The results are visible in Fig. 688 and Fig. 689.

Chart Legend area with gradient Legend

Fig. 688 Chart Legend area with gradient Legend

Chart Legend Area Gradient Dialog

Fig. 689 Chart Legend Area Gradient Dialog

Image

The ooodev.format.chart2.direct.legend.area.Img class 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.chart2.direct.legend.area import Img as LegendAreaImg, PresetImageKind
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
# ... other code

legend_img_style = LegendAreaImg.from_preset(
    chart_doc=chart_doc, preset=PresetImageKind.PARCHMENT_PAPER
)
legend_bg_transparency_style = LegendTransparency(0)
Chart2.style_legend(
    chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_img_style]
)

The results are visible in Fig. 690 and Fig. 691.

Chart Legend with background image

Fig. 690 Chart Legend with background image

Chart Area Legend Image Dialog

Fig. 691 Chart Area Legend Image Dialog

Pattern

The ooodev.format.chart2.direct.legend.area.Pattern class 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.chart2.direct.legend.area import Pattern as LegendAreaPattern
from ooodev.format.chart2.direct.legend.area import PresetPatternKind
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
# ... other code

legend_pattern_style = LegendAreaPattern.from_preset(
    chart_doc=chart_doc, preset=PresetPatternKind.HORIZONTAL_BRICK
)
legend_bg_transparency_style = LegendTransparency(0)
Chart2.style_legend(
    chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_pattern_style]
)

The results are visible in Fig. 692 and Fig. 693.

Chart Legend with pattern

Fig. 692 Chart Legend with pattern

Chart Area Legend Pattern Dialog

Fig. 693 Chart Area Legend Pattern Dialog

Hatch

The ooodev.format.chart2.direct.legend.area.Hatch class 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.chart2.direct.legend.area import Hatch as LegendAreaHatch, PresetHatchKind
from ooodev.format.chart2.direct.legend.transparency import Transparency as LegendTransparency
# ... other code

legend_hatch_style = LegendAreaHatch.from_preset(
    chart_doc=chart_doc, preset=PresetHatchKind.YELLOW_45_DEGREES_CROSSED
)
legend_bg_transparency_style = LegendTransparency(0)
Chart2.style_legend(
    chart_doc=chart_doc, styles=[legend_bg_transparency_style, legend_hatch_style]
)

The results are visible in Fig. 694 and Fig. 695.

Chart Legend with hatch

Fig. 694 Chart Legend with hatch

Chart Title Hatch Dialog

Fig. 695 Chart Title Hatch Dialog