Add Error Bars and Trend Lines to Charts — Presentation API for Java
- 7 minutes to read
The Presentation API allows you to add error bars and trend lines to series in standard charts. Error bars display the margin of error or the variability of series values. A trend line displays the general direction of series data and can extend beyond the data to forecast values.
Series Types That Support Error Bars and Trend Lines
You can add error bars and a trend line to the following series types in a standard chart (Chart):
| Series Type | Series Class | Error Bar Method | Trend Line Method |
|---|---|---|---|
| Area | AreaSeries | getErrorBars() | setTrendLine(TrendLine value) |
| Bar | BarSeries | getErrorBars() | setTrendLine(TrendLine value) |
| Bubble | BubbleSeries | getErrorBars() | setTrendLine(TrendLine value) |
| Line | LineSeries | getErrorBars() | setTrendLine(TrendLine value) |
| Scatter | ScatterSeries | getErrorBars() | setTrendLine(TrendLine value) |
| Stock | StockSeries | getErrorBars() | setTrendLine(TrendLine value) |
Note
Other series types (for example, pie, radar, and 3D series) and chart types introduced in Microsoft Excel 2016 (ChartEx) do not support error bars and trend lines.
Add Error Bars to a Series
Call the getErrorBars() method of a series to access the error bar collection (IErrorBarCollection). Create an ErrorBar object, configure the error bar, and add the object to the collection.
Use the following methods to configure error bars:
| Method | Description |
|---|---|
| ErrorBar.setAxis(value) | Sets the axis to which error bars apply. |
| ErrorBar.setDirection(value) | Sets the error bar direction. |
| ErrorBar.setType(value) | Sets the calculation mode for error amounts. |
| ErrorBar.setValue(value) | Sets the error amount. |
| ErrorBar.setPlus(value) | Sets custom positive error amounts. |
| ErrorBar.setMinus(value) | Sets custom negative error amounts. |
| ErrorBar.setShowCap(value) | Sets whether to display end caps for error bars. |
| ErrorBar.setOutlineStyle(value) | Sets line settings for error bars. |
Call the setType(value) method to specify one of the following calculation modes for error amounts:
| Calculation Mode | Description |
|---|---|
| FIXED_VALUE | Uses a fixed error amount for all data points. |
| PERCENTAGE | Uses a percentage of each data point value. |
| STANDARD_DEVIATION | Uses a multiple of the standard deviation. |
| STANDARD_ERROR | Uses the standard error. |
| CUSTOM | Uses custom error amounts specified by the ErrorBar.setPlus(value) and ErrorBar.setMinus(value) methods. |
For fixed value, percentage, and standard deviation modes, call the setValue(value) method to specify the error amount (for example, 10 for 10% in percentage mode).
For custom error amounts, call the setType(ErrorValueType.CUSTOM) method and pass the amounts to setPlus(value) and setMinus(value) methods. You can pass a ChartNumericData object with error amounts, or a ChartDataReference or ChartDataExpressionReference object that refers to chart data cells.
The following code snippet creates a column chart with two series, adds percentage error bars with end caps to the first series, and adds custom positive error bars to the second series:
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 two column series.
String[] names = { "2024", "2025" };
double[][] values = { { 42, 55, 48, 61 }, { 47, 52, 58, 66 } };
for (int i = 0; i < names.length; i++) {
BarSeries series = new BarSeries();
series.setText(names[i]);
series.setArguments(new ChartStringData(List.of("Q1", "Q2", "Q3", "Q4")));
series.setValues(new ChartNumericData(List.of(values[i][0], values[i][1], values[i][2], values[i][3])));
chart.getSeries().add(series);
}
// Display 10% error bars with end caps for the first series.
ErrorBar percentageErrorBar = new ErrorBar();
percentageErrorBar.setType(ErrorValueType.PERCENTAGE);
percentageErrorBar.setValue(10);
percentageErrorBar.setShowCap(true);
OutlineStyle errorBarLine = new OutlineStyle();
errorBarLine.setFill(new SolidFill(Color.getDarkRed()));
percentageErrorBar.setOutlineStyle(errorBarLine);
((BarSeries) chart.getSeries().get(0)).getErrorBars().add(percentageErrorBar);
// Display custom positive error values for the second series.
ErrorBar customErrorBar = new ErrorBar();
customErrorBar.setType(ErrorValueType.CUSTOM);
customErrorBar.setDirection(ErrorBarDirection.PLUS);
customErrorBar.setPlus(new ChartNumericData(List.of(3.0, 5.0, 4.0, 6.0)));
customErrorBar.setShowCap(true);
((BarSeries) chart.getSeries().get(1)).getErrorBars().add(customErrorBar);
// 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:

Display Horizontal and Vertical Error Bars
A series can contain up to two error bars for different axes. For example, a scatter series can display horizontal error bars for X values and vertical error bars for Y values.
A new ErrorBar object uses the ErrorBarAxis.AUTO axis. Before you add a second error bar to a series, call the setAxis(value) method of each error bar to specify different axes.
Note
If the collection already contains an error bar for the same axis, the add method does not add the error bar and throws an InvalidOperationException. The API throws the same exception if the collection contains two error bars.
The following code snippet creates a scatter chart with horizontal error bars with a fixed error amount and vertical error bars with custom positive and negative error amounts for each data point:
import java.io.*;
import java.nio.file.*;
import java.util.List;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;
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);
// Create a scatter series: temperature (X) and reaction time (Y).
ScatterSeries series = new ScatterSeries();
series.setText("Reaction Time");
series.setArguments(new ChartNumericData(List.of(20.0, 30.0, 40.0, 50.0, 60.0)));
series.setValues(new ChartNumericData(List.of(48.0, 39.0, 33.0, 25.0, 21.0)));
chart.getSeries().add(series);
// Display circle markers without lines.
Marker marker = new Marker();
marker.setSymbol(MarkerStyle.CIRCLE);
marker.setSize(9);
series.setMarker(marker);
OutlineStyle noLine = new OutlineStyle();
noLine.setFill(new NoFill());
ChartElement seriesProperties = new ChartElement();
seriesProperties.setOutlineStyle(noLine);
series.setProperties(seriesProperties);
// Display horizontal error bars with a fixed error amount.
ErrorBar horizontalErrorBar = new ErrorBar();
horizontalErrorBar.setAxis(ErrorBarAxis.HORIZONTAL);
horizontalErrorBar.setType(ErrorValueType.FIXED_VALUE);
horizontalErrorBar.setValue(2);
horizontalErrorBar.setShowCap(true);
series.getErrorBars().add(horizontalErrorBar);
// Display vertical error bars with custom error amounts for each data point.
ErrorBar verticalErrorBar = new ErrorBar();
verticalErrorBar.setAxis(ErrorBarAxis.VERTICAL);
verticalErrorBar.setType(ErrorValueType.CUSTOM);
verticalErrorBar.setPlus(new ChartNumericData(List.of(6.0, 5.0, 4.0, 4.0, 3.0)));
verticalErrorBar.setMinus(new ChartNumericData(List.of(3.0, 3.0, 2.0, 2.0, 2.0)));
verticalErrorBar.setShowCap(true);
series.getErrorBars().add(verticalErrorBar);
// 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:

Add a Trend Line to a Series
Create a TrendLine object and pass it to the setTrendLine(value) method of a series (for example, LineSeries.setTrendLine(value)). A series can contain a single trend line. To remove the trend line, call the setTrendLine(null) method.
The API supports linear, logarithmic, exponential, moving average, polynomial, and power trend lines (TrendLineType). Use the following methods to configure a trend line:
| Method | Description |
|---|---|
| TrendLine.setType(value) | Sets the trend line type. |
| TrendLine.setPolynomialOrder(value) | Sets the order of the polynomial trend line. |
| TrendLine.setMovingAveragePeriod(value) | Sets the number of data points that the moving average trend line averages. |
| TrendLine.setForecastForward(value) | Sets the distance by which the trend line extends forward beyond series data. |
| TrendLine.setForecastBackward(value) | Sets the distance by which the trend line extends backward beyond series data. |
| TrendLine.setIntercept(value) | Sets the value at which the trend line crosses the value axis. |
| TrendLine.setName(value) | Sets the trend line name. |
| TrendLine.setOutlineStyle(value) | Sets outline settings of the chart element. |
The chart legend displays the trend line name. If you do not specify a name, the chart generates the name from the trend line type and series name (for example, Linear (Sales)).
Display the Trend Line Equation and R-Squared Value
To display the trend line equation in a label next to the trend line, call the TrendLine.setShowEquationLabel(true) method. To display the R-squared value in the label, call the TrendLine.setShowRSquaredValue(true) method.
To customize the label, pass a TrendLineLabel object to the TrendLine.setEquationLabel(value) method. The TrendLineLabel class contains custom text, text formatting, number format, and layout settings for the label.
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:
