Format Presentation Charts in Python
Overview
This article explains how to format charts in PowerPoint presentations by using Aspose.Slides. It shows how to customize key chart elements such as axes, grid lines, titles, legends, the plot area, and wall fills to improve the appearance and readability of chart data.
It also demonstrates how to set font properties for chart text, apply preset and custom numeric formats to chart data, and enable rounded corners for the chart area. Together, these examples show how to control both the visual style and data presentation of charts in a presentation.
Format Chart Entities
Aspose.Slides for Python via Java lets developers add custom charts to their slides from scratch. This article explains how to format different chart entities including the category and value axes.
Aspose.Slides for Python via Java provides a simple API for managing different chart entities and formatting them using custom values:
- Create an instance of the Presentation class.
- Access a slide by its index.
- Add a chart of the desired type with default data (this example uses ChartType.LineWithMarkers).
- Access the chart value axis and set the following properties:
- Set Line format for value axis major grid lines.
- Set Line format for value axis minor grid lines.
- Set Number Format for the value axis.
- Set minimum, maximum, major, and minor units for the value axis.
- Set Text Properties for the value axis data.
- Set Title for the value axis.
- Access the chart category axis and set the following properties:
- Set Line format for category axis major grid lines.
- Set Line format for category axis minor grid lines.
- Set Text Properties for the category axis data.
- Set Title for the category axis.
- Set Label Positioning for the category axis.
- Set Rotation Angle for the category axis labels.
- Access the chart legend and set its text properties.
- Show the chart legend without overlapping the chart.
- Access the chart secondary value axis and set the following properties:
- Enable the secondary value axis.
- Set Line Format for the secondary value axis.
- Set Number Format for the secondary value axis.
- Set minimum, maximum, major, and minor units for the secondary value axis.
- Plot the first chart series on the secondary value axis.
- Set the chart back wall fill color.
- Set the chart plot area fill color.
- Write the modified presentation to a PPTX file.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, DisplayUnitType, FillType, FontData, LineDashStyle, LineStyle, NullableBool, Presentation, PresetColor, SaveFormat, TickLabelPositionType
Color = jpype.JClass("java.awt.Color")
nullable_true = NullableBool.True_
# Create an instance of the Presentation class
presentation = Presentation()
try:
# Access the first slide
slide = presentation.getSlides().get_Item(0)
# Add the sample chart
chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 50, 50, 500, 400)
# Set Chart Title
chart.setTitle(True)
chart.getChartTitle().addTextFrameForOverriding("")
chart_title = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0)
chart_title.setText("Sample Chart")
chart_title.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
chart_title.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
chart_title.getPortionFormat().setFontHeight(20)
chart_title.getPortionFormat().setFontBold(nullable_true)
chart_title.getPortionFormat().setFontItalic(nullable_true)
# Set Major grid lines format for value axis
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid)
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().setWidth(5)
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().setDashStyle(LineDashStyle.DashDot)
# Set Minor grid lines format for value axis
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid)
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED)
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().setWidth(3)
# Set value axis number format
chart.getAxes().getVerticalAxis().setNumberFormatLinkedToSource(False)
chart.getAxes().getVerticalAxis().setDisplayUnit(DisplayUnitType.Thousands)
chart.getAxes().getVerticalAxis().setNumberFormat("0.0%")
# Set chart maximum, minimum values
chart.getAxes().getVerticalAxis().setAutomaticMajorUnit(False)
chart.getAxes().getVerticalAxis().setAutomaticMaxValue(False)
chart.getAxes().getVerticalAxis().setAutomaticMinorUnit(False)
chart.getAxes().getVerticalAxis().setAutomaticMinValue(False)
chart.getAxes().getVerticalAxis().setMaxValue(15)
chart.getAxes().getVerticalAxis().setMinValue(-2)
chart.getAxes().getVerticalAxis().setMinorUnit(0.5)
chart.getAxes().getVerticalAxis().setMajorUnit(2.0)
# Set Value Axis Text Properties
value_axis_text = chart.getAxes().getVerticalAxis().getTextFormat().getPortionFormat()
value_axis_text.setFontBold(nullable_true)
value_axis_text.setFontHeight(16)
value_axis_text.setFontItalic(nullable_true)
value_axis_text.getFillFormat().setFillType(FillType.Solid)
value_axis_text.getFillFormat().getSolidFillColor().setPresetColor(PresetColor.DarkGreen)
value_axis_font = FontData("Times New Roman")
value_axis_text.setLatinFont(value_axis_font)
# Set value axis title
chart.getAxes().getVerticalAxis().setTitle(True)
chart.getAxes().getVerticalAxis().getTitle().addTextFrameForOverriding("")
value_axis_title = chart.getAxes().getVerticalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0)
value_axis_title.setText("Primary Axis")
value_axis_title.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
value_axis_title.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
value_axis_title.getPortionFormat().setFontHeight(20)
value_axis_title.getPortionFormat().setFontBold(nullable_true)
value_axis_title.getPortionFormat().setFontItalic(nullable_true)
# Set Major grid lines format for Category axis
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid)
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GREEN)
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().setWidth(5)
# Set Minor grid lines format for Category axis
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid)
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.YELLOW)
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().setWidth(3)
# Set Category Axis Text Properties
category_axis_text = chart.getAxes().getHorizontalAxis().getTextFormat().getPortionFormat()
category_axis_text.setFontBold(nullable_true)
category_axis_text.setFontHeight(16)
category_axis_text.setFontItalic(nullable_true)
category_axis_text.getFillFormat().setFillType(FillType.Solid)
category_axis_text.getFillFormat().getSolidFillColor().setColor(Color.BLUE)
category_axis_font = FontData("Arial")
category_axis_text.setLatinFont(category_axis_font)
# Set Category Title
chart.getAxes().getHorizontalAxis().setTitle(True)
chart.getAxes().getHorizontalAxis().getTitle().addTextFrameForOverriding("")
category_axis_title = chart.getAxes().getHorizontalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0)
category_axis_title.setText("Sample Category")
category_axis_title.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
category_axis_title.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
category_axis_title.getPortionFormat().setFontHeight(20)
category_axis_title.getPortionFormat().setFontBold(nullable_true)
category_axis_title.getPortionFormat().setFontItalic(nullable_true)
# Set category axis label position
chart.getAxes().getHorizontalAxis().setTickLabelPosition(TickLabelPositionType.Low)
# Set category axis label rotation angle
chart.getAxes().getHorizontalAxis().setTickLabelRotationAngle(45)
# Set Legends Text Properties
legend_text = chart.getLegend().getTextFormat().getPortionFormat()
legend_text.setFontBold(nullable_true)
legend_text.setFontHeight(16)
legend_text.setFontItalic(nullable_true)
legend_text.getFillFormat().setFillType(FillType.Solid)
legend_text.getFillFormat().getSolidFillColor().setPresetColor(PresetColor.DarkRed)
# Show the chart legend without overlapping the chart
chart.getLegend().setOverlay(False)
chart.getChartData().getSeries().get_Item(0).setPlotOnSecondAxis(True)
# Set secondary value axis
chart.getAxes().getSecondaryVerticalAxis().setVisible(True)
chart.getAxes().getSecondaryVerticalAxis().getFormat().getLine().setStyle(LineStyle.ThickBetweenThin)
chart.getAxes().getSecondaryVerticalAxis().getFormat().getLine().setWidth(20)
# Set secondary value axis Number format
chart.getAxes().getSecondaryVerticalAxis().setNumberFormatLinkedToSource(False)
chart.getAxes().getSecondaryVerticalAxis().setDisplayUnit(DisplayUnitType.Hundreds)
chart.getAxes().getSecondaryVerticalAxis().setNumberFormat("0.0%")
# Set chart maximum, minimum values
chart.getAxes().getSecondaryVerticalAxis().setAutomaticMajorUnit(False)
chart.getAxes().getSecondaryVerticalAxis().setAutomaticMaxValue(False)
chart.getAxes().getSecondaryVerticalAxis().setAutomaticMinorUnit(False)
chart.getAxes().getSecondaryVerticalAxis().setAutomaticMinValue(False)
chart.getAxes().getSecondaryVerticalAxis().setMaxValue(20)
chart.getAxes().getSecondaryVerticalAxis().setMinValue(-5)
chart.getAxes().getSecondaryVerticalAxis().setMinorUnit(0.5)
chart.getAxes().getSecondaryVerticalAxis().setMajorUnit(2.0)
# Set chart back wall color
chart.getBackWall().setThickness(1)
chart.getBackWall().getFormat().getFill().setFillType(FillType.Solid)
chart.getBackWall().getFormat().getFill().getSolidFillColor().setColor(Color.ORANGE)
chart.getFloor().getFormat().getFill().setFillType(FillType.Solid)
chart.getFloor().getFormat().getFill().getSolidFillColor().setColor(Color.RED)
# Set Plot area color
chart.getPlotArea().getFormat().getFill().setFillType(FillType.Solid)
chart.getPlotArea().getFormat().getFill().getSolidFillColor().setPresetColor(PresetColor.LightCyan)
# Save the presentation
presentation.save("FormattedChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Set Font Properties for a Chart
Aspose.Slides for Python via Java supports setting font properties for charts. Follow these steps to set the font properties:
- Create an instance of the Presentation class.
- Add a chart to the slide.
- Set font height.
- Save the modified presentation.
The following example demonstrates these steps.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
# Create an instance of the Presentation class
presentation = Presentation()
try:
chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 500, 400)
chart.getTextFormat().getPortionFormat().setFontHeight(20)
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(True)
presentation.save("FontPropertiesForChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Set the Numeric Format
Aspose.Slides for Python via Java provides a simple API for managing chart data formats:
- Create an instance of the Presentation class.
- Access a slide by its index.
- Add a chart of the desired type with default data (this example uses ChartType.ClusteredColumn).
- Set the preset number format from the possible preset values.
- Iterate through the data cells in every chart series and set their number format.
- Save the presentation.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
# Create an instance of the Presentation class
presentation = Presentation()
try:
# Access the first presentation slide
slide = presentation.getSlides().get_Item(0)
# Add a default clustered column chart
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 500, 400)
# Access the chart series collection
chart_series_collection = chart.getChartData().getSeries()
# Iterate through every chart series
for chart_series in chart_series_collection:
# Iterate through every data point in the series
for data_point in chart_series.getDataPoints():
# Set the number format
data_point.getValue().getAsCell().setPresetNumberFormat(jpype.JByte(10)) # 0.00%
# Save the presentation
presentation.save("PresetNumberFormat.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
The available preset number formats and their indices are listed below:
| 0 | General |
|---|---|
| 1 | 0 |
| 2 | 0.00 |
| 3 | #,##0 |
| 4 | #,##0.00 |
| 5 | $#,##0;$-#,##0 |
| 6 | $#,##0;Red$-#,##0 |
| 7 | $#,##0.00;$-#,##0.00 |
| 8 | $#,##0.00;Red$-#,##0.00 |
| 9 | 0% |
| 10 | 0.00% |
| 11 | 0.00E+00 |
| 12 | # ?/? |
| 13 | # / |
| 14 | m/d/yy |
| 15 | d-mmm-yy |
| 16 | d-mmm |
| 17 | mmm-yy |
| 18 | h:mm AM/PM |
| 19 | h:mm:ss AM/PM |
| 20 | h:mm |
| 21 | h:mm:ss |
| 22 | m/d/yy h:mm |
| 37 | #,##0;-#,##0 |
| 38 | #,##0;Red-#,##0 |
| 39 | #,##0.00;-#,##0.00 |
| 40 | #,##0.00;Red-#,##0.00 |
| 41 | _ * #,##0_ ;_ * “_ ;_ @_ |
| 42 | _ $* #,##0_ ;_ $* “_ ;_ @_ |
| 43 | _ * #,##0.00_ ;_ * “??_ ;_ @_ |
| 44 | _ $* #,##0.00_ ;_ $* “??_ ;_ @_ |
| 45 | mm:ss |
| 46 | h:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+00 |
| 49 | @ |
Set Chart Area Rounded Borders
Aspose.Slides for Python via Java supports rounded corners for the chart area through the hasRoundedCorners and setRoundedCorners methods of the Chart class.
- Create an instance of the Presentation class.
- Add a chart to the slide.
- Set the fill type and style of the chart border line.
- Enable rounded corners.
- Save the modified presentation.
The following example demonstrates these steps.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, LineStyle, Presentation, SaveFormat
# Create an instance of the Presentation class
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 100, 600, 400)
chart.getLineFormat().getFillFormat().setFillType(FillType.Solid)
chart.getLineFormat().setStyle(LineStyle.Single)
chart.setRoundedCorners(True)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
FAQ
Can I set semi-transparent fills for columns/areas while keeping the border opaque?
Yes. Fill transparency and the outline are configured separately. This is useful for improving the readability of the grid and data in dense visualizations.
How can I deal with data labels when they overlap?
Reduce the font size, disable nonessential label components (for example, categories), set the label offset/position, show labels only for selected points if necessary, or switch the format to “value + legend”.
Can I apply gradient or pattern fills to series?
Yes. Both solid and gradient/pattern fills are typically available. In practice, use gradients sparingly and avoid combinations that reduce contrast with the grid and text.