Create Excel Charts With Trendline, Error Bars And Data Labels Using C#

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:

  • Trendline types, features and usage.
  • Error bar types and settings in Excel.
  • Create charts with trendline, error bars, data table, and data labels in C# using Spire.XLS.

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.

  • A linear trendline is usually used to show something is increasing or decreasing at a steady rate. When the data is simple, try it to show the trend. 

  • A logarithmic trendline is appropriate to show the rate of change in the data increases or decreases quickly and then levels out for both positive values and negative values. 

  • A polynomial trendline is a curved line to show the fluctuations of data. We should choose different order of polynomial trendline based on how many bends (hills and valleys) the curve has. 

  • A power trendline is the best-fit curved line to show whether data increase at a specific rate. It can only be used for positive data (not zero or negative).

  • An exponential trendline is a curved line used mostly to show data rise or fall at increasingly higher rates. It also can’t be used for zero or negative data. 

  • A moving average trendline consists of average data points with lines. Its data points are decided by the period option we set. The moving average trendline works fine to smooth the fluctuation and illustrate the trend more clearly. 
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.   
  6.  //Fill Excel cells with sample data  
  7.  sheet.Name = "sample 1";  
  8.  sheet.Range["A1"].Value = "Month";  
  9.  sheet.Range["A2"].Value = "Jan.";  
  10.  sheet.Range["A3"].Value = "Feb.";  
  11.  sheet.Range["A4"].Value = "Mar.";  
  12.  sheet.Range["A5"].Value = "Apr.";  
  13.  sheet.Range["A6"].Value = "May.";  
  14.  sheet.Range["A7"].Value = "Jun.";  
  15.  sheet.Range["B1"].Value = "Porter";  
  16.  sheet.Range["B2"].NumberValue = 15;  
  17.  sheet.Range["B3"].NumberValue = 18;  
  18.  sheet.Range["B4"].NumberValue = 24;  
  19.  sheet.Range["B5"].NumberValue = 32;  
  20.  sheet.Range["B6"].NumberValue = 37;  
  21.  sheet.Range["B7"].NumberValue = 40;  
  22.  sheet.Range["C1"].Value = "Harry";  
  23.  sheet.Range["C2"].NumberValue = 38;  
  24.  sheet.Range["C3"].NumberValue = 32;  
  25.  sheet.Range["C4"].NumberValue = 17;  
  26.  sheet.Range["C5"].NumberValue = 35;  
  27.  sheet.Range["C6"].NumberValue = 45;  
  28.  sheet.Range["C7"].NumberValue = 43;  
  29.  sheet.Range["D1"].Value = "Chocolate";  
  30.  sheet.Range["D2"].NumberValue = 30;  
  31.  sheet.Range["D3"].NumberValue = 28;  
  32.  sheet.Range["D4"].NumberValue = 35;  
  33.  sheet.Range["D5"].NumberValue = 23;  
  34.  sheet.Range["D6"].NumberValue = 33;  
  35.  sheet.Range["D7"].NumberValue = 38;  
  36.    
  37.  //Create a columnclustered chart  
  38.  Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);  
  39.  chart.DataRange = sheet.Range["B1:D7"];  
  40.  chart.SeriesDataFromRange = false;  
  41.  chart.TopRow = 4;  
  42.  chart.BottomRow = 27;  
  43.  chart.LeftColumn = 2;  
  44.  chart.RightColumn =11;  
  45.  chart.ChartTitle = "Chart with trendline and datatable";  
  46.  chart.ChartTitleArea.IsBold = true;  
  47.  chart.ChartTitleArea.Size = 12;  
  48.  Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];  
  49.  cs1.CategoryLabels = sheet.Range["A2:A7"];  
  50.   
  51.  //Add trendline and datatable to the chart  
  52.  chart.Series[0].TrendLines.Add(TrendLineType.Exponential);  
  53.  chart.HasDataTable = true;  
  54.   
  55.  //Save the document as .xlsx file  
  56.  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.   
  32. // Set the displayed elements for data labels.   
  33. cs1.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;  
  34. cs1.DataPoints.DefaultDataPoint.DataLabels.HasLegendKey = false;  
  35. cs1.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = false;  
  36. cs1.DataPoints.DefaultDataPoint.DataLabels.HasSeriesName = true;  
  37. cs1.DataPoints.DefaultDataPoint.DataLabels.HasCategoryName = true;  
  38. cs1.DataPoints.DefaultDataPoint.DataLabels.Delimiter = ". ";  
  39.   
  40. //Set the font and fill effect for data labels.  
  41. cs1.DataPoints.DefaultDataPoint.DataLabels.Size = 9;  
  42. cs1.DataPoints.DefaultDataPoint.DataLabels.Color = Color.Red;  
  43. cs1.DataPoints.DefaultDataPoint.DataLabels.FontName = "Calibri";  
  44. cs1.DataPoints.DefaultDataPoint.DataLabels.Position = DataLabelPositionType.Center;  
  45. cs1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Fill.Texture = GradientTextureType.BlueTissuePaper;  
  46.   
  47. 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. 

Up Next
    Ebook Download
    View all
    Learn
    View all