Chart2 Direct Series Data Labels (Static)
Overview
The Data Labels tab of the Chart Data Labels dialog has many options as see in Fig. 769.
The ooodev.format.chart2.direct.series.data_labels.data_labels
module has various classes to set the same options.
Calls to the Chart2.style_data_series()
and Chart2.style_data_point()
methods are used to set the data labels options of a Chart.
Fig. 769 Chart Data Labels Dialog Data Labels Tab
See also
Setup
General setup for these examples.
import uno
from ooodev.format.chart2.direct.series.data_labels.data_labels import PercentFormat
from ooodev.format.chart2.direct.series.data_labels.data_labels import TextAttribs
from ooodev.format.chart2.direct.series.data_labels.data_labels import NumberFormatIndexEnum
from ooodev.format.chart2.direct.general.area import Gradient as ChartGradient, PresetGradientKind
from ooodev.format.chart2.direct.general.borders import LineProperties as ChartLineProperties
from ooodev.format.chart2.direct.wall.transparency import Transparency as WallTransparency
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.loader.lo import Lo
def main() -> int:
with Lo.Loader(connector=Lo.ConnectPipe()):
doc = Calc.open_doc("col_chart.ods")
GUI.set_visible(True, doc)
Lo.delay(500)
Calc.zoom(doc, GUI.ZoomEnum.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="col_chart")
chart_bdr_line = ChartLineProperties(color=StandardColor.GREEN_LIGHT3, width=0.7)
chart_grad = ChartGradient.from_preset(chart_doc, PresetGradientKind.NEON_LIGHT)
Chart2.style_background(chart_doc=chart_doc, styles=[chart_grad, chart_bdr_line])
wall_transparency = WallTransparency(value=60)
Chart2.style_wall(chart_doc=chart_doc, styles=[wall_transparency])
text_attribs = TextAttribs(show_number=True)
format_number = NumberFormat(
chart_doc=chart_doc,
source_format=False,
num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2,
)
Chart2.style_data_series(chart_doc=chart_doc, styles=[text_attribs, format_number])
Lo.delay(1_000)
Lo.close_doc(doc)
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 TextAttribs
class is used 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
from ooodev.format.chart2.direct.series.data_labels.data_labels import TextAttribs
# ... other code
text_attribs = TextAttribs(
show_category_name=True,
show_legend_symbol=True,
show_series_name=True,
auto_text_wrap=True,
)
Chart2.style_data_series(chart_doc=chart_doc, styles=[text_attribs])
Running the above code will produce the following output shown in Fig. 770 and Fig. 771.
Fig. 770 Chart with formatting applied to data series
Fig. 771 Chart Format Number Dialog
Style Data Point
Chart2.style_data_point(chart_doc=chart_doc, series_idx=0, idx=2, styles=[text_attribs])
Running the above code will produce the following output shown in Fig. 772.
Fig. 772 Chart with Text Attributes applied to data point
Number Format
The NumberFormat
class is used 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 NumberFormat
are
visible the TextAttribs
class is used 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 ooodev.format.chart2.direct.series.data_labels.data_labels import TextAttribs
from ooodev.format.chart2.direct.series.data_labels.data_labels import NumberFormat
from ooodev.format.chart2.direct.series.data_labels.data_labels import NumberFormatIndexEnum
# ... other code
text_attribs = TextAttribs(show_number=True)
format_number = NumberFormat(
chart_doc=chart_doc,
source_format=False,
num_format_index=NumberFormatIndexEnum.CURRENCY_1000DEC2,
)
Chart2.style_data_series(chart_doc=chart_doc, styles=[text_attribs, format_number])
Running the above code will produce the following output shown in Fig. 773 and Fig. 774.
Fig. 773 Chart with Text Attributes applied to data series
Fig. 774 Chart Format Number Dialog
Style Data Point
# ... other code
Chart2.style_data_point(
chart_doc=chart_doc, series_idx=0, idx=1, styles=[text_attribs, format_number]
)
Running the above code will produce the following output shown in Fig. 775.
Fig. 775 Chart with Text Attributes applied to data point
Percentage Format
The PercentFormat
class is used 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 PercentFormat
are
visible the TextAttribs
class is used 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 ooodev.format.chart2.direct.series.data_labels.data_labels import PercentFormat
from ooodev.format.chart2.direct.series.data_labels.data_labels import NumberFormatIndexEnum
# ... other code
text_attribs = TextAttribs(show_number_in_percent=True)
format_percent = PercentFormat(
chart_doc=chart_doc,
source_format=False,
num_format_index=NumberFormatIndexEnum.PERCENT_DEC2,
)
Chart2.style_data_series(chart_doc=chart_doc, styles=[text_attribs, format_percent])
Running the above code will produce the following output shown in Fig. 776 and Fig. 777.
Fig. 776 Chart with formatting applied to data series
Fig. 777 Chart Format Number Dialog
Style Data Point
# ... other code
Chart2.style_data_point(
chart_doc=chart_doc, series_idx=0, idx=3, styles=[text_attribs, format_percent]
)
Running the above code will produce the following output shown in Fig. 778.
Fig. 778 Chart with formatting applied to data point
Attribute Options
The AttribOptions
class is used 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.chart2.direct.series.data_labels.data_labels import AttribOptions
from ooodev.format.chart2.direct.series.data_labels.data_labels import PlacementKind
# ... other code
attrib_opt = AttribOptions(placement=PlacementKind.INSIDE)
Chart2.style_data_series(chart_doc=chart_doc, styles=[attrib_opt])
Running the above code will produce the following output shown in Fig. 779 and Fig. 780.
Fig. 779 Chart with formatting applied to data series
Fig. 780 Chart Format Number Dialog
Style Data Point
Chart2.style_data_point(chart_doc=chart_doc, series_idx=0, idx=-1, styles=[attrib_opt])
Running the above code will produce the following output shown in Fig. 781.
Fig. 781 Chart with formatting applied to data point
Rotate Text
The Orientation
class is used 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.chart2.direct.series.data_labels.data_labels import Orientation
from ooodev.format.chart2.direct.series.data_labels.data_labels import DirectionModeKind
# ... other code
rotation = Orientation(angle=60, mode=DirectionModeKind.LR_TB, leaders=True)
Chart2.style_data_series(chart_doc=chart_doc, idx=0, styles=[rotation])
Running the above code will produce the following output shown in Fig. 782 and Fig. 783.
Fig. 782 Chart with formatting applied to data series
Fig. 783 Chart Format Number Dialog
Style Data Point
Chart2.style_data_point(chart_doc=chart_doc, series_idx=0, idx=2, styles=[rotation])
Running the above code will produce the following output shown in Fig. 784
Fig. 784 Chart with formatting applied to data point