Skip to main content

TrendLine Class

A trend line of a chart series.

Declaration

public class TrendLine extends ChartElement

Remarks

To display a trend line for a series, pass a TrendLine object to the series setTrendLine(value) method. The following methods specify trend lines:

A series can contain a single trend line. To remove the trend line, call the setTrendLine(null) method.

The setType(TrendLineType value) method specifies the mathematical model of the trend line. setPolynomialOrder(int value) and setMovingAveragePeriod(int value) methods specify settings of polynomial and moving average trend lines.

setForecastForward(double value) and setForecastBackward(double value) methods extend the trend line beyond series data.

setShowEquationLabel(boolean value) and setShowRSquaredValue(boolean value) methods specify whether a label displays the trend line equation and R-squared value. The setEquationLabel(TrendLineLabel value) method specifies the label settings.

The TrendLine.setOutlineStyle(value) method specifies the trend line color, width, and dash style.

Refer to the following help topic for additional information: Add Error Bars and Trend Lines to Charts — Presentation API for Java.

Example

Add a Linear Trend Line with an Equation Label to a Line Chart

The following code snippet adds a linear trend line to a line series, extends the trend line two periods forward, and displays the trend line equation and R-squared value in bold:

import java.io.*;
import java.nio.file.*;
import java.util.List;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;
import com.devexpress.system.drawing.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (Presentation presentation = new Presentation()) {
            // Add a blank slide.
            presentation.getSlides().clear();
            Slide slide = new Slide(SlideLayoutType.BLANK);
            presentation.getSlides().add(slide);

            // Insert a chart that fills the slide.
            Chart chart = new Chart();
            chart.setWidth(presentation.getSlideSize().getWidth());
            chart.setHeight(presentation.getSlideSize().getHeight());
            slide.getShapes().add(chart);

            // Add a line series.
            LineSeries series = new LineSeries();
            series.setText("Sales");
            series.setArguments(new ChartStringData(List.of("Jan", "Feb", "Mar", "Apr", "May", "Jun")));
            series.setValues(new ChartNumericData(List.of(15.0, 18.0, 24.0, 21.0, 28.0, 31.0)));
            chart.getSeries().add(series);

            // Add a linear trend line that extends two periods forward.
            TrendLine trendLine = new TrendLine();
            trendLine.setName("Sales Trend");
            trendLine.setType(TrendLineType.LINEAR);
            trendLine.setForecastForward(2);

            // Display the trend line equation and R-squared value in bold.
            trendLine.setShowEquationLabel(true);
            trendLine.setShowRSquaredValue(true);
            TextProperties labelText = new TextProperties();
            labelText.setFontSize(16f);
            labelText.setBold(true);
            TrendLineLabel label = new TrendLineLabel();
            label.setTextProperties(labelText);
            trendLine.setEquationLabel(label);

            // Display the trend line as a dashed orange line.
            OutlineStyle trendLineStyle = new OutlineStyle();
            trendLineStyle.setFill(new SolidFill(Color.getOrange()));
            trendLineStyle.setWidth(10);
            trendLineStyle.setDashType(LineDashType.DASH);
            trendLine.setOutlineStyle(trendLineStyle);
            series.setTrendLine(trendLine);

            // Save the presentation.
            Files.createDirectories(Path.of("output"));
            try (OutputStream output = Files.newOutputStream(Path.of("output", "Chart.pptx"))) {
                presentation.save(output);
            }
        }
    }
}

The following screenshot illustrates the result:

 Line chart with a dashed linear trend line that extends two periods forward and displays the equation and R-squared value created with the DevExpress Presentation API for Java

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

See Also

TrendLine()

Initializes a new instance of the TrendLine class.

Declaration

public TrendLine()

Methods

deepClone() Method

Returns a deep copy of this TrendLine instance.

Declaration

public TrendLine deepClone()

Returns

Type Description
TrendLine

A deep copy of this TrendLine.

getEquationLabel() Method

Returns settings of the label that displays the trend line equation and R-squared value.

Declaration

public TrendLineLabel getEquationLabel()

Returns

Type Description
TrendLineLabel

Trend line label settings or null if the label settings are not specified.

getForecastBackward() Method

Returns the distance by which the trend line extends backward beyond series data.

Declaration

public double getForecastBackward()

Returns

Type Description
double

The backward forecast distance in argument axis units.

getForecastForward() Method

Returns the distance by which the trend line extends forward beyond series data.

Declaration

public double getForecastForward()

Returns

Type Description
double

The forward forecast distance in argument axis units.

getIntercept() Method

Returns the value at which the trend line crosses the value axis.

Declaration

public double getIntercept()

Returns

Type Description
double

The intercept value.

getMovingAveragePeriod() Method

Returns the number of data points that the moving average trend line averages.

Declaration

public int getMovingAveragePeriod()

Returns

Type Description
int

The moving average period, from 2 to 255.

getName() Method

Returns the trend line name.

Declaration

public String getName()

Returns

Type Description
String

The trend line name.

getPolynomialOrder() Method

Returns the order of the polynomial trend line.

Declaration

public int getPolynomialOrder()

Returns

Type Description
int

The polynomial order, from 2 to 6.

getShowEquationLabel() Method

Returns whether the trend line label displays the trend line equation.

Declaration

public boolean getShowEquationLabel()

Returns

Type Description
boolean

true if the label displays the equation; otherwise, false.

getShowRSquaredValue() Method

Returns whether the trend line label displays the R-squared value.

Declaration

public boolean getShowRSquaredValue()

Returns

Type Description
boolean

true if the label displays the R-squared value; otherwise, false.

getType() Method

Returns the trend line type.

Declaration

public TrendLineType getType()

Returns

Type Description
TrendLineType

The trend line type.

setEquationLabel(TrendLineLabel value) Method

Sets settings of the label that displays the trend line equation and R-squared value.

Declaration

public void setEquationLabel(TrendLineLabel value)

Parameters

Name Type Description
value TrendLineLabel

Trend line label settings.

Remarks

setShowEquationLabel(boolean value) and setShowRSquaredValue(boolean value) methods specify the label content.

If getShowEquationLabel() and getShowRSquaredValue() methods return false, the chart does not display the label.

For a new TrendLine object, the getEquationLabel() method returns null.

setForecastBackward(double value) Method

Sets the distance by which the trend line extends backward beyond series data.

Declaration

public void setForecastBackward(double value)

Parameters

Name Type Description
value double

The backward forecast distance in argument axis units.

Remarks

If you do not call the setForecastBackward(value) method, the API uses 0 (the trend line does not extend backward) for a new trend line and the value from the document for a loaded trend line.

The setForecastForward(double value) method extends the trend line forward.

setForecastForward(double value) Method

Sets the distance by which the trend line extends forward beyond series data.

Declaration

public void setForecastForward(double value)

Parameters

Name Type Description
value double

The forward forecast distance in argument axis units.

Remarks

If you do not call the setForecastForward(value) method, the API uses 0 (the trend line does not extend forward) for a new trend line and the value from the document for a loaded trend line.

The setForecastBackward(double value) method extends the trend line backward.

setIntercept(double value) Method

Sets the value at which the trend line crosses the value axis.

Declaration

public void setIntercept(double value)

Parameters

Name Type Description
value double

The intercept value.

Remarks

If you call the setIntercept(0) method, the trend line does not use a fixed intercept.

If you do not call the setIntercept(value) method, the API uses 0 for a new trend line and the value from the document for a loaded trend line.

setMovingAveragePeriod(int value) Method

Sets the number of data points that the moving average trend line averages.

Declaration

public void setMovingAveragePeriod(int value)

Parameters

Name Type Description
value int

The moving average period, from 2 to 255.

Exceptions

Type Description
ArgumentOutOfRangeException

Thrown if the value parameter is less than 2 or greater than 255.

Remarks

If you do not call the setMovingAveragePeriod(value) method, the API uses 2 for a new trend line and the value from the document for a loaded trend line.

Note

The moving average period takes effect if the getType() method returns TrendLineType.MOVING_AVERAGE.

setName(String value) Method

Sets the trend line name.

Declaration

public void setName(String value)

Parameters

Name Type Description
value String

The trend line name.

Remarks

If you pass null or an empty string, the trend line has no custom name. In this instance, the chart generates the trend line name from the trend line type and series name (for example, Linear (Series1)).

For a new TrendLine object, the getName() method returns null.

setPolynomialOrder(int value) Method

Sets the order of the polynomial trend line.

Declaration

public void setPolynomialOrder(int value)

Parameters

Name Type Description
value int

The polynomial order, from 2 to 6.

Exceptions

Type Description
ArgumentOutOfRangeException

Thrown if the value parameter is less than 2 or greater than 6.

Remarks

If you do not call the setPolynomialOrder(value) method, the API uses 2 for a new trend line and the value from the document for a loaded trend line.

Note

The polynomial order takes effect if the getType() method returns TrendLineType.POLYNOMIAL.

setShowEquationLabel(boolean value) Method

Sets whether the trend line label displays the trend line equation.

Declaration

public void setShowEquationLabel(boolean value)

Parameters

Name Type Description
value boolean

true to display the equation; otherwise, false.

Remarks

If you do not call the setShowEquationLabel(value) method, the API uses false for a new trend line and the value from the document for a loaded trend line.

The setEquationLabel(TrendLineLabel value) method specifies label settings.

setShowRSquaredValue(boolean value) Method

Sets whether the trend line label displays the R-squared value.

Declaration

public void setShowRSquaredValue(boolean value)

Parameters

Name Type Description
value boolean

true to display the R-squared value; otherwise, false.

Remarks

If you do not call the setShowRSquaredValue(value) method, the API uses false for a new trend line and the value from the document for a loaded trend line.

The setEquationLabel(TrendLineLabel value) method specifies label settings.

setType(TrendLineType value) Method

Sets the trend line type.

Declaration

public void setType(TrendLineType value)

Parameters

Name Type Description
value TrendLineType

The trend line type.

Remarks

The setType(value) method accepts the following values:

Value Description
LINEAR Indicates a linear trend line.
LOGARITHMIC Indicates a logarithmic trend line.
EXPONENTIAL Indicates an exponential trend line.
MOVING_AVERAGE Indicates a moving average trend line.
POLYNOMIAL Indicates a polynomial trend line.
POWER Indicates a power trend line.

If you do not call the setType(value) method, the API uses TrendLineType.LINEAR for a new trend line and the value from the document for a loaded trend line.

setPolynomialOrder(int value) and setMovingAveragePeriod(int value) methods specify settings of polynomial and moving average trend lines.