Documents for Excel, Java Edition Documentation
Features / Chart / Customize Chart Objects / Series / Error Bars
In This Topic
    Error Bars
    In This Topic

    Error bars are used in charts to indicate the error or uncertainty of data. They act as an extremely useful tool for scientists, statisticians, and research analysts to showcase data variability and measurement accuracy.

    GcExcel allows you to configure error bars in charts using IErrorBar interface. The interface represents error bars in a chart series and provides properties to configure various types, end styles and value types of error bars. The error bars can also be exported or imported to JSON or a PDF document.

    Supported Chart Types

    The following chart types are supported while adding error bars in charts:

    Error Bar Types

    Type Snapshot Description
    Plus Plus error bar

    Error bar depicts only the positive values.

    Java
    Copy Code
    series.getYErrorBar.setType(ErrorBarInclude.Plus);
    Minus Minus error bar

    Error bar depicts only the negative values.

    Java
    Copy Code
    series.getYErrorBar.setType(ErrorBarInclude.Minus);
    Both Both error bar

    Error bar depicts positive and negative values at the same time

    Java
    Copy Code
    series.getYErrorBar.setType(ErrorBarInclude.Both);

    Error Bar End Styles

    Type Snapshot Description
    Cap Cap end style

    Error bar displays caps at the end of error bar lines.

    Java
    Copy Code
    series.getYErrorBar.setEndStyle(EndStyleCap.Cap);
    No Cap No cap end style

    Error bar does not display caps at the end of error bar lines.

    Java
    Copy Code
    series.getYErrorBar.setEndStyle(EndStyleCap.NoCap);

    Error Bar Value Types

    Type Snapshot Description
    Fixed Value Fixed value type

    Error bar represents the error as an absolute value.

    Java
    Copy Code
    series.getYErrorBar.setValueType(ErrorBarType.FixedValue);
    Percentage Percentage type

    Error bar represents the error as a percentage of data value in the same direction axis.

    Java
    Copy Code
    series.getYErrorBar.setValueType(ErrorBarType.Percentage);
    Standard Deviation Standard deviation type

    Error bar represents the error as a calculating value which depends on the set deviation and chart data values.

    Java
    Copy Code
    series.getYErrorBar.setValueType(ErrorBarType.StDev);
    Standard Error Standard error type

    Error bar represents the error as a calculating value which only depends on the chart data values.

    Java
    Copy Code
    series.getYErrorBar.setValueType(ErrorBarType.StError);
    Custom Custom type

    Error bar represents the error values that are set with positive and negative values respectively by formulas or fixed values.

    Java
    Copy Code
    series.getYErrorBar.setValueType(ErrorBarType.Custom);
    Note: In Custom value type, the array and reference formula string for plus or minus is supported. The final count of error bar values is evaluated by custom formula which has different behavior.
    If count = 1: all error bars are the same as the only one value.
    If count < number of data points: the rest error bar value will be zero.
    If count > number of data points: the beyond values will do nothing.

     

    Using Code


    Refer to the following example code to add error bars using various properties in the chart.

    Java
    Copy Code
    // create a new workbook
     Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Prepare data for chart
    worksheet.getRange("A1:D4")
    .setValue(new Object[][] { { null, "Q1", "Q2", "Q3" }, { "Mobile Phones", 1330, 2345, 3493 },
    { "Laptops", 2032, 3632, 2197 }, { "Tablets", 6233, 3270, 2030 } });
    worksheet.getRange("A:D").getColumns().autoFit();
    // Add Column Chart
    IShape columnChartshape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
    
    // Adding series to SeriesCollection
    columnChartshape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D4"), RowCol.Columns, true, true);
    
    // Get first series
    ISeries series1 = columnChartshape.getChart().getSeriesCollection().get(0);
    
    // Config first series' properties
    series1.setHasErrorBars(true);
    series1.getYErrorBar().setType(ErrorBarInclude.Both);
    series1.getYErrorBar().setValueType(ErrorBarType.Custom);
    series1.getYErrorBar().setEndStyle(EndStyleCap.Cap);
    series1.getYErrorBar().setPlus("={200,400,600}");
    series1.getYErrorBar().setMinus("={600,400,200}");
    
    // Get second series
    ISeries series2 = columnChartshape.getChart().getSeriesCollection().get(1);
    
    // Config second series' properties
    series2.setHasErrorBars(true);
    series2.getYErrorBar().setType(ErrorBarInclude.Plus);
    series2.getYErrorBar().setValueType(ErrorBarType.FixedValue);
    series2.getYErrorBar().setEndStyle(EndStyleCap.Cap);
    series2.getYErrorBar().setAmount(1000);
    series2.getYErrorBar().getFormat().getLine().getColor().setRGB(Color.GetRed());
    series2.getYErrorBar().getFormat().getLine().setWeight(2);
    
    // Get last series
    ISeries series3 = columnChartshape.getChart().getSeriesCollection().get(2);
    
    // Config last series' properties
    series3.setHasErrorBars(true);
    series3.getYErrorBar().setType(ErrorBarInclude.Both);
    series3.getYErrorBar().setValueType(ErrorBarType.StError);
    series3.getYErrorBar().setEndStyle(EndStyleCap.NoCap);
    
    // save to an excel file
    workbook.save("ErrorBar.xlsx");

    Important Points

    Limitation

    There can be some difference between the exported PDF and Excel containing error bars. It is caused due to different ways of calculating error bar value between GcExcel and Excel.