Using charts, we are able to understand huge data and their relationships easily for charts can visualize and present raw data via line, pie, bar, area, scatter. Excel provides numerous charts solution. However, the default charts created by Excel might not contain those excellent tools such as trendline, error bars, and data tables in chart, which are often ignored by most of users though those analysis tools are boosts to display data. And those chart elements are also widely ignored by programmers when they create charts. So in this article, I’m going to give you a detailed introduction about trendline, error bars and then I’ll show you how to use free Spire.XLS to create excel charts with trendline, error bars, data table and data labels in C#.

The following three parts are included in this article:

Part 1

Detailed Introduction Of Trendline


Trendline in Excel has the following six types: Linear, Logarithmic, Polynomial, Power, Exponential, and Moving average. We should choose different type of trendline based on the type of the data and we could check the reliability of trendline by the R-squared value (near or at 1 is excellent). Please note that trendline can be only added to 2-D, area, bar, bubble, column, line, stock, scatter and unstacked chart.

Trendline Options In Excel


Figure 1: Trending Options

Part 2

Detail Of Error Bars In Excel Charts


Error bars are a graphical representation of the variability of data and are used on graphs to indicate the error, or uncertainty in a reported measurement (quoted from Wikipedia). Using error bars in Excel chart, we could easily see margins of error and standard deviation. There are 5 options in Excel to set error type: fixed value, percentage, standard deviation, standard error and custom specified value. The difference between standard deviation and standard error in Excel charts lies in different equations, please refer to the following picture from Microsoft Office official website.



Figure 2: Standard Error


Figure 3: Standard Deviation

Besides the type, we could also set the direction of error bars: both, minus and plus. Please note that only 2-D area, bar, column, line, scatter, and bubble charts support error bars.

Since it’s not difficult to understand the meaning of data tables and data labels in Charts, here I’m not going to give more introduction about them in detail. Once you see the charts with data tables and data labels in the part 3, you’ll know them clearly.

Part 3

Create Excel charts with trendline, error bars, data tables, data labels in C# using Spire.XLS


Tools free us from extra work. Here I use .dll from free Spire.XLS to create Excel charts and add all the chart tools I mentioned above if you would like to try, you could get it from E-iceblue official website or Nuget.

Step 1: I use the following code to create a column clustered chart (see the screenshot) with trendline and data table using the .dll in Visual Studio.

Screenshot: (R2=0.9688, which means the trendline is very fit to the data.)


Figure 4: Chart

Code
  1. //Create an Excel and add a sheet
  2. Workbook workbook = new Workbook();
  3. workbook.CreateEmptySheets(1);
  4. Worksheet sheet = workbook.Worksheets[0];
  5. //Fill Excel cells with sample data
  6. sheet.Name = "sample 1";
  7. sheet.Range["A1"].Value = "Month";
  8. sheet.Range["A2"].Value = "Jan.";
  9. sheet.Range["A3"].Value = "Feb.";
  10. sheet.Range["A4"].Value = "Mar.";
  11. sheet.Range["A5"].Value = "Apr.";
  12. sheet.Range["A6"].Value = "May.";
  13. sheet.Range["A7"].Value = "Jun.";
  14. sheet.Range["B1"].Value = "Porter";
  15. sheet.Range["B2"].NumberValue = 15;
  16. sheet.Range["B3"].NumberValue = 18;
  17. sheet.Range["B4"].NumberValue = 24;
  18. sheet.Range["B5"].NumberValue = 32;
  19. sheet.Range["B6"].NumberValue = 37;
  20. sheet.Range["B7"].NumberValue = 40;
  21. sheet.Range["C1"].Value = "Harry";
  22. sheet.Range["C2"].NumberValue = 38;
  23. sheet.Range["C3"].NumberValue = 32;
  24. sheet.Range["C4"].NumberValue = 17;
  25. sheet.Range["C5"].NumberValue = 35;
  26. sheet.Range["C6"].NumberValue = 45;
  27. sheet.Range["C7"].NumberValue = 43;
  28. sheet.Range["D1"].Value = "Chocolate";
  29. sheet.Range["D2"].NumberValue = 30;
  30. sheet.Range["D3"].NumberValue = 28;
  31. sheet.Range["D4"].NumberValue = 35;
  32. sheet.Range["D5"].NumberValue = 23;
  33. sheet.Range["D6"].NumberValue = 33;
  34. sheet.Range["D7"].NumberValue = 38;
  35. //Create a columnclustered chart
  36. Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
  37. chart.DataRange = sheet.Range["B1:D7"];
  38. chart.SeriesDataFromRange = false;
  39. chart.TopRow = 4;
  40. chart.BottomRow = 27;
  41. chart.LeftColumn = 2;
  42. chart.RightColumn =11;
  43. chart.ChartTitle = "Chart with trendline and datatable";
  44. chart.ChartTitleArea.IsBold = true;
  45. chart.ChartTitleArea.Size = 12;
  46. Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];
  47. cs1.CategoryLabels = sheet.Range["A2:A7"];
  48. //Add trendline and datatable to the chart
  49. chart.Series[0].TrendLines.Add(TrendLineType.Exponential);
  50. chart.HasDataTable = true;
  51. //Save the document as .xlsx file
  52. workbook.SaveToFile("Sample1.xlsx", ExcelVersion.Version2010);
Step 2: Using the same sample data, I create another chart to show the error bars in Excel using C#. Only the different codes are listed.
  1. Chart chart = sheet.Charts.Add(ExcelChartType.Line);
  2. chart.DataRange = sheet.Range["C1:D7"];
  3. chart.SeriesDataFromRange = false;
  4. chart.TopRow = 4;
  5. chart.BottomRow = 27;
  6. chart.LeftColumn = 2;
  7. chart.RightColumn =11;
  8. chart.ChartTitle = "Chart with error bars";
  9. chart.ChartTitleArea.IsBold = true;
  10. chart.ChartTitleArea.Size = 12;
  11. chart.Series[0].CategoryLabels = sheet.Range["A2:A7"];
  12. chart.Series[0].ErrorBar(true, ErrorBarIncludeType.Plus, ErrorBarType.Fixed, 2);
  13. chart.Series[1].ErrorBar(true, ErrorBarIncludeType.Both, ErrorBarType.Percentage,5);
Please see the effect


Figure 5: Chart with Error

Step 3: In the final part, I’m going to create a chart with data labels, by which the value, legend key, percentage (pie chart), series name, and category name can be added to the plot area of the chart.

Effect screenshot


Figure 6: Effect Screenshot

Namespace in this part
  1. using Spire.Xls;
  2. using System.Drawing;
Codes
  1. Workbook workbook = new Workbook();
  2. workbook.CreateEmptySheets(1);
  3. Worksheet sheet = workbook.Worksheets[0];
  4. sheet.Name = "sample 1";
  5. sheet.Range["A1"].Value = "Month";
  6. sheet.Range["A2"].Value = "Jan.";
  7. sheet.Range["A3"].Value = "Feb.";
  8. sheet.Range["A4"].Value = "Mar.";
  9. sheet.Range["A5"].Value = "Apr.";
  10. sheet.Range["A6"].Value = "May.";
  11. sheet.Range["A7"].Value = "Jun.";
  12. sheet.Range["B1"].Value = "Porter";
  13. sheet.Range["B2"].NumberValue = 8;
  14. sheet.Range["B3"].NumberValue = 15;
  15. sheet.Range["B4"].NumberValue = 3;
  16. sheet.Range["B5"].NumberValue = 9;
  17. sheet.Range["B6"].NumberValue = 13;
  18. sheet.Range["B7"].NumberValue = 17;
  19. Chart chart = sheet.Charts.Add(ExcelChartType.LineMarkers);
  20. chart.DataRange = sheet.Range["B1:B7"];
  21. chart.SeriesDataFromRange = false;
  22. chart.TopRow = 4;
  23. chart.BottomRow = 27;
  24. chart.LeftColumn = 2;
  25. chart.RightColumn =11;
  26. chart.ChartTitle = "Chart with data labels";
  27. chart.ChartTitleArea.IsBold = true;
  28. chart.ChartTitleArea.Size = 12;
  29. Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];
  30. cs1.CategoryLabels = sheet.Range["A2:A7"];
  31. // Set the displayed elements for data labels.
  32. cs1.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
  33. cs1.DataPoints.DefaultDataPoint.DataLabels.HasLegendKey = false;
  34. cs1.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = false;
  35. cs1.DataPoints.DefaultDataPoint.DataLabels.HasSeriesName = true;
  36. cs1.DataPoints.DefaultDataPoint.DataLabels.HasCategoryName = true;
  37. cs1.DataPoints.DefaultDataPoint.DataLabels.Delimiter = ". ";
  38. //Set the font and fill effect for data labels.
  39. cs1.DataPoints.DefaultDataPoint.DataLabels.Size = 9;
  40. cs1.DataPoints.DefaultDataPoint.DataLabels.Color = Color.Red;
  41. cs1.DataPoints.DefaultDataPoint.DataLabels.FontName = "Calibri";
  42. cs1.DataPoints.DefaultDataPoint.DataLabels.Position = DataLabelPositionType.Center;
  43. cs1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Fill.Texture = GradientTextureType.BlueTissuePaper;
  44. workbook.SaveToFile("Sample1.xlsx", ExcelVersion.Version2010);
Conclusion

Thanks to Spire.XLS, I’m able to show you how to add trendline, error bars, data labels and data tables for Excel charts programmatically in C#. I hope you could have a deeper understanding of chart tools in Excel after reading my article. Thank you for reading.