Add Trend Lines to Presentation Charts in Python

Overview

This article explains how to add trend lines to presentation charts by using Aspose.Slides. It shows how to create a chart, add trend lines to chart series, and work with several trend line types, including exponential, linear, logarithmic, moving average, polynomial, and power.

It also describes how to add a custom line to a chart by inserting a line shape, and includes a short FAQ about forward and backward trend line projection values and whether trend lines are preserved during export to PDF or SVG and when rendering charts as images.

Add a Trend Line

Aspose.Slides for Python via Java provides a simple API for managing different chart trend lines:

  1. Create an instance of the Presentation class.
  2. Obtain a reference to a slide by its index.
  3. Add a chart with default data and the desired type (this example uses ChartType.ClusteredColumn).
  4. Add an exponential trend line to chart series 1.
  5. Add a linear trend line to chart series 1.
  6. Add a logarithmic trend line to chart series 2.
  7. Add a moving average trend line to chart series 2.
  8. Add a polynomial trend line to chart series 3.
  9. Add a power trend line to chart series 3.
  10. Write the modified presentation to a PPTX file.

The following code creates a chart with trend lines.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import ChartType, FillType, Presentation, SaveFormat, TrendlineType
from java.awt import Color

# Create an instance of the Presentation class.
presentation = Presentation()
try:
    # Create a clustered column chart.
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 400)

    # Add an exponential trend line to chart series 1.
    exponential_trend_line = chart.getChartData().getSeries().get_Item(0).getTrendLines().add(TrendlineType.Exponential)
    exponential_trend_line.setDisplayEquation(False)
    exponential_trend_line.setDisplayRSquaredValue(False)

    # Add a linear trend line to chart series 1.
    linear_trend_line = chart.getChartData().getSeries().get_Item(0).getTrendLines().add(TrendlineType.Linear)
    linear_trend_line.setTrendlineType(TrendlineType.Linear)
    linear_trend_line.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
    linear_trend_line.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED)

    # Add a logarithmic trend line to chart series 2.
    logarithmic_trend_line = chart.getChartData().getSeries().get_Item(1).getTrendLines().add(TrendlineType.Logarithmic)
    logarithmic_trend_line.setTrendlineType(TrendlineType.Logarithmic)
    logarithmic_trend_line.addTextFrameForOverriding("New log trend line")

    # Add a moving average trend line to chart series 2.
    moving_average_trend_line = chart.getChartData().getSeries().get_Item(1).getTrendLines().add(TrendlineType.MovingAverage)
    moving_average_trend_line.setTrendlineType(TrendlineType.MovingAverage)
    moving_average_trend_line.setPeriod(jpype.JByte(3))
    moving_average_trend_line.setTrendlineName("New TrendLine Name")

    # Add a polynomial trend line to chart series 3.
    polynomial_trend_line = chart.getChartData().getSeries().get_Item(2).getTrendLines().add(TrendlineType.Polynomial)
    polynomial_trend_line.setTrendlineType(TrendlineType.Polynomial)
    polynomial_trend_line.setForward(1)
    polynomial_trend_line.setOrder(jpype.JByte(3))

    # Add a power trend line to chart series 3.
    power_trend_line = chart.getChartData().getSeries().get_Item(2).getTrendLines().add(TrendlineType.Power)
    power_trend_line.setTrendlineType(TrendlineType.Power)
    power_trend_line.setBackward(1)

    # Save the presentation.
    presentation.save("ChartTrendLines_out.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Add a Custom Line

Aspose.Slides for Python via Java provides a simple API to add custom lines to a chart. To add a plain line to a chart on a selected slide, follow these steps:

  • Create an instance of the Presentation class.
  • Obtain a reference to a slide by its index.
  • Create a new chart using the addChart method of the ShapeCollection class.
  • Add a line shape using the addAutoShape method with ShapeType.Line.
  • Set the color of the shape’s line.
  • Write the modified presentation to a PPTX file.

The following code creates a chart with a custom line.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import ChartType, FillType, Presentation, SaveFormat, ShapeType
from java.awt import Color

# Create an instance of the Presentation class.
presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 500, 400)
    shape = chart.getUserShapes().getShapes().addAutoShape(ShapeType.Line, 0, chart.getHeight() / 2, chart.getWidth(), 0)

    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.RED)

    presentation.save("Presentation.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

FAQ

What do ‘forward’ and ‘backward’ mean for a trend line?

They are the lengths of the trend line projected forward or backward: for scatter (XY) charts, they are measured in axis units; for non-scatter charts, they are measured in the number of categories. Only non-negative values are allowed.

Will the trend line be preserved when exporting the presentation to PDF or SVG, or when rendering a slide to an image?

Yes. Aspose.Slides converts presentations to PDF/SVG and renders charts to images; trend lines, as part of the chart, are preserved during these operations. A method is also available to export an image of the chart itself.