[]
        
(Showing Draft Content)

Error Bars

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:

  • Area Charts

  • Bar Charts

  • Column charts

  • Line Charts

  • xyScatter Charts

Error Bar Types

Type

Snapshot

Description

Plus

Plus chart

Error bar depicts only the positive values.

series.YErrorBar.Type = ErrorBarInclude.Plus;

Minus

Minus chart

Error bar depicts only the negative values.

series.YErrorBar.Type = ErrorBarInclude.Minus;

Both

Both chart

Error bar depicts positive and negative values at the same time.

series.YErrorBar.Type = ErrorBarInclude.Both;


Error Bar End Styles

Type

Snapshot

Description

Cap

Cap end style

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

series.YErrorBar.EndStyle = EndStyleCap.Cap;

No Cap

No cap end style

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

series.YErrorBar.EndStyle = EndStyleCap.NoCap;


Error Bar Value Types

Type

Snapshot

Description

Fixed Value


Fixed value type


Error bar represents the error as an absolute value.

series1.YErrorBar.ValueType = ErrorBarType.FixedValue;

Percentage


Percentage type


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

series1.YErrorBar.ValueType = 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.

series1.YErrorBar.ValueType = ErrorBarType.StDev;

Standard Error


Standard error type


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

series1.YErrorBar.ValueType = 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.

series1.YErrorBar.ValueType = 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 the formula string (for example, "=Sheet1!$B$2:$D$2" or "={1,2,3}"). The error bar values are displayed based on the total count of values:If count = 1: all error bars have the same value.If count < number of data points: the value of rest of the error bars is zero.If count > number of data points: the remaining values will do nothing.

Using Code

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

    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.Worksheets[0];
    // Prepare data for chart
    worksheet.Range["A1:D4"].Value = new object[,]
    {
{null, "Q1", "Q2", "Q3"},
{"Mobile Phones", 1330, 2345, 3493},
{"Laptops", 2032, 3632, 2197},
{"Tablets", 6233, 3270, 2030}
    };
    worksheet.Range["A:D"].Columns.AutoFit();
    // Add Column Chart
    IShape columnChartshape = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 250, 20, 360, 230);

    // Adding series to SeriesCollection
    columnChartshape.Chart.SeriesCollection.Add(worksheet.Range["A1:D4"], RowCol.Columns, true, true);

    // Get first series
    ISeries series1 = columnChartshape.Chart.SeriesCollection[0];

    //Config first series' properties
    series1.HasErrorBars = true;
    series1.YErrorBar.Type = ErrorBarInclude.Both;
    series1.YErrorBar.ValueType = ErrorBarType.Custom;
    series1.YErrorBar.EndStyle = EndStyleCap.Cap;
    series1.YErrorBar.Plus = "={200,400,600}";
    series1.YErrorBar.Minus = "={600,400,200}";

    // Get second series
    ISeries series2 = columnChartshape.Chart.SeriesCollection[1];

    //Config second series' properties
    series2.HasErrorBars = true;
    series2.YErrorBar.Type = ErrorBarInclude.Plus;
    series2.YErrorBar.ValueType = ErrorBarType.FixedValue;
    series2.YErrorBar.EndStyle = EndStyleCap.Cap;
    series2.YErrorBar.Amount = 1000;
    series2.YErrorBar.Format.Line.Color.RGB = Color.Red;
    series2.YErrorBar.Format.Line.Weight = 2;

    // Get last series
    ISeries series3 = columnChartshape.Chart.SeriesCollection[2];

    //Config last series' properties
    series3.HasErrorBars = true;
    series3.YErrorBar.Type = ErrorBarInclude.Both;
    series3.YErrorBar.ValueType = ErrorBarType.StError;
    series3.YErrorBar.EndStyle = EndStyleCap.NoCap;

    //save to an excel file
    workbook.Save("ErrorBar.xlsx");

Important Points

  • Only series in scatter chart groups can have x and y error bars. Otherwise, an exception would be thrown.

  • ISeries.HasErrorBars must be set as "true" to display error bar.

  • IErrorBar.Amount only takes effect when IErrorBar.ValueType is FixedValue or Percentage or Standard Deviation.

  • IErrorBar.Plus or IErrorBar.Minus only takes effect when IErrorBar.ValueType is Custom.

  • IErrorBar.Plus or IErrorBar.Minus accepts a formula string like "=Sheet1!$B$2:$D$2" or "={1,2,3}".

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.