Chart2 Direct Series Data Labels

Overview

The Data Labels tab of the Chart Data Labels dialog has many options as see in Fig. 588. The various style_* methods are called to set the same options.

Chart Data Labels Dialog Data Labels Tab

Fig. 588 Chart Data Labels Dialog Data Labels Tab

Setup

General setup for these examples.

from __future__ import annotations
from pathlib import Path
import uno
from ooo.dyn.i18n.number_format_index import NumberFormatIndexEnum
from ooodev.calc import CalcDoc, ZoomKind
from ooodev.loader.lo import Lo
from ooodev.format.inner.preset.preset_gradient import PresetGradientKind
from ooodev.utils.color import StandardColor

def main() -> int:
    with Lo.Loader(connector=Lo.ConnectPipe()):
        fnm = Path.cwd() / "tmp" / "col_chart.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.GREEN_LIGHT3,
            width=0.7,
        )
        _ = chart_doc.style_area_gradient_from_preset(
            preset=PresetGradientKind.NEON_LIGHT,
        )

        chart_doc.first_diagram.wall.style_area_transparency_transparency(60)
        ds = chart_doc.get_data_series()[0]

        ds.style_text_attributes(show_number=True)
        ds.style_numbers_numbers(
            source_format=False,
            num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2,
        )

        # f_style = ds.style_label_border_line_get()
        # assert f_style is not None

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

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

Text Attributes

The text attributes are set using three classes that are covered in this section.

Before formatting the chart is seen in Fig. 829.

Text Attribs

The style_text_attributes() method is called to set the various boolean options in the Text Attributes section of the Chart Data Labels dialog as seen in Fig. 769.

Before formatting the chart is seen in Fig. 829.

Style Data Series

# ... other code

ds = chart_doc.get_data_series()[0]
ds.style_text_attributes(
    show_category_name=True,
    show_legend_symbol=True,
    show_series_name=True,
    auto_text_wrap=True,
)

Running the above code will produce the following output shown in Fig. 589 and Fig. 590.

Chart with formatting applied to data series

Fig. 589 Chart with formatting applied to data series

Chart Format Number Dialog

Fig. 590 Chart Format Number Dialog

Style Data Point

# ... other code
ds = chart_doc.get_data_series()[0]
dp = ds[2]

dp.style_text_attributes(
    show_category_name=True,
    show_legend_symbol=True,
    show_series_name=True,
    auto_text_wrap=True,
)

Running the above code will produce the following output shown in Fig. 591.

Chart with Text Attributes applied to data point

Fig. 591 Chart with Text Attributes applied to data point

Number Format

The style_numbers_numbers() method is used to set the number format of the data labels. This method is used to set the values seen in Fig. 774.

The NumberFormatIndexEnum enum contains the values in API NumberFormatIndex for easy lookup.

To ensure that the effects of style_numbers_numbers() are visible the style_text_attributes() method is called to turn on Value as Number of the dialog seen in Fig. 769.

Before formatting the chart is seen in Fig. 829.

Style Data Series

from ooo.dyn.i18n.number_format_index import NumberFormatIndexEnum
# ... other code

ds.style_text_attributes(show_number=True)
ds.style_numbers_numbers(
    source_format=False,
    num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2,
)

Running the above code will produce the following output shown in Fig. 592 and Fig. 593.

Chart with Text Attributes applied to data series

Fig. 592 Chart with Text Attributes applied to data series

Chart Format Number Dialog

Fig. 593 Chart Format Number Dialog

Style Data Point

# ... other code
ds = chart_doc.get_data_series()[0]
dp = ds[1]
dp.style_text_attributes(show_number=True)
dp.style_numbers_numbers(
    source_format=False,
    num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2,
)

Running the above code will produce the following output shown in Fig. 594.

Chart with Text Attributes applied to data point

Fig. 594 Chart with Text Attributes applied to data point

Percentage Format

The style_numbers_percent() method is called to set the number format of the data labels. This class is used to set the values seen in Fig. 774.

The NumberFormatIndexEnum enum contains the values in API NumberFormatIndex for easy lookup.

To ensure that the effects of style_numbers_percent() are visible the style_text_attributes() method is called to turn on Value as Percentage of the dialog seen in Fig. 769.

Before formatting the chart is seen in Fig. 829.

Style Data Series

from ooo.dyn.i18n.number_format_index import NumberFormatIndexEnum
# ... other code

ds = chart_doc.get_data_series()[0]
ds.style_text_attributes(show_number_in_percent=True)
ds.style_numbers_percent(
    source_format=False,
    num_format_index=NumberFormatIndexEnum.PERCENT_DEC2,
)

Running the above code will produce the following output shown in Fig. 595 and Fig. 596.

Chart with formatting applied to data series

Fig. 595 Chart with formatting applied to data series

Chart Format Number Dialog

Fig. 596 Chart Format Number Dialog

Style Data Point

from ooo.dyn.i18n.number_format_index import NumberFormatIndexEnum
# ... other code
ds = chart_doc.get_data_series()[0]
dp = ds[3]
dp.style_text_attributes(show_number_in_percent=True)
dp.style_numbers_percent(
    source_format=False,
    num_format_index=NumberFormatIndexEnum.PERCENT_DEC2,
)

Running the above code will produce the following output shown in Fig. 597.

Chart with formatting applied to data point

Fig. 597 Chart with formatting applied to data point

Attribute Options

The style_attribute_options() method is called to set the Options data labels. This class is used to set the values seen in the Attribute Options section of Fig. 769.

The PlacementKind enum is used to look up the placement.

Before formatting the chart is seen in Fig. 829.

Style Data Series

from ooodev.format.inner.direct.chart2.series.data_labels.data_labels.attrib_options import PlacementKind
# ... other code

ds = chart_doc.get_data_series()[0]
ds.style_attribute_options(placement=PlacementKind.INSIDE)

Running the above code will produce the following output shown in Fig. 598 and Fig. 599.

Chart with formatting applied to data series

Fig. 598 Chart with formatting applied to data series

Chart Format Number Dialog

Fig. 599 Chart Format Number Dialog

Style Data Point

from ooodev.format.inner.direct.chart2.series.data_labels.data_labels.attrib_options import PlacementKind
# ... other code
ds = chart_doc.get_data_series()[0]
dp = ds[-1]  # get the last data point
dp.style_attribute_options(placement=PlacementKind.INSIDE)

Running the above code will produce the following output shown in Fig. 600.

Chart with formatting applied to data point

Fig. 600 Chart with formatting applied to data point

Rotate Text

The style_orientation() method is called to set the rotation of data labels. This class is used to set the values seen in the Rotate Text section of Fig. 769.

The DirectionModeKind enum is used to look up the text direction.

Before formatting the chart is seen in Fig. 829.

Style Data Series

from ooodev.format.inner.direct.chart2.title.alignment.direction import DirectionModeKind
# ... other code
ds = chart_doc.get_data_series()[0]
ds.style_orientation(angle=60, mode=DirectionModeKind.LR_TB, leaders=True)

Running the above code will produce the following output shown in Fig. 601 and Fig. 602.

Chart with formatting applied to data series

Fig. 601 Chart with formatting applied to data series

Chart Format Number Dialog

Fig. 602 Chart Format Number Dialog

Style Data Point

from ooodev.format.inner.direct.chart2.title.alignment.direction import DirectionModeKind
# ... other code
ds = chart_doc.get_data_series()[0]
dp = ds[2]
dp.style_orientation(angle=60, mode=DirectionModeKind.LR_TB, leaders=True)

Running the above code will produce the following output shown in Fig. 603

Chart with formatting applied to data point

Fig. 603 Chart with formatting applied to data point