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.
DsExcel 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.
The following chart types are supported while adding error bars in charts:
Type | Snapshot | Description | ||||
---|---|---|---|---|---|---|
Plus |
Error bar depicts only the positive values.
| |||||
Minus |
Error bar depicts only the negative values.
| |||||
Both |
Error bar depicts positive and negative values at the same time
|
Type | Snapshot | Description | ||||
---|---|---|---|---|---|---|
Cap |
Error bar displays caps at the end of error bar lines.
| |||||
No Cap |
Error bar does not display caps at the end of error bar lines.
|
Type | Snapshot | Description | ||||
---|---|---|---|---|---|---|
Fixed Value |
Error bar represents the error as an absolute value.
| |||||
Percentage |
Error bar represents the error as a percentage of data value in the same direction axis.
| |||||
Standard Deviation |
Error bar represents the error as a calculating value which depends on the set deviation and chart data values.
| |||||
Standard Error |
Error bar represents the error as a calculating value which only depends on the chart data values.
| |||||
Custom |
Error bar represents the error values that are set with positive and negative values respectively by formulas or fixed values.
|
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"); |
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 DsExcel and Excel.