How To Create A Presentation Chart With Trendlines And Data Labels In C#

We always use presentation charts to analyze the huge data and show their relationships to our readers for reporting. Usually when we create a chart, the default chart on the presentation slides may not contain some excellent tools such as trendline and data tables etc. In this article, we will show clearly how to create a presentation chart with trendline and data labels in C#.

This article contains the two parts, which are given below.

  • Trendline types and its usage.
  • Create a presentation chart with trendline and data labels in C#, using Spire.presentation.

There are six kinds of different types of trendlines and each of them has its own usage.

  • Exponential 
    An Exponential trendline is a curved line, which is used mostly to show the data rise or fall at increasingly higher rates.

  • Linear
    A Linear trendline is usually used to show something, which is increasing or decreasing at a steady rate. When the data is simple, we always use Linear.

  • Logarithmic
    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. 

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

  • Power
    A power trendline is the best-fit curved line to show whether the data increases at a specific rate. It can only be used for positive data.

  • Moving Average
    A moving average trendline consists of average data points with the lines. Its data points are decided by the period option we set.

Trendline options on Microsoft PowerPoint files are given below.

First, get the Spire.Presentation.dll from NuGet and add it as a reference for our projects.

Tools, which we require are given below.

  • Spire.Presentation.dll
  • Visual Studio

Namespaces to be used are given below.

  1. using Spire.Presentation;  
  2. using Spire.Presentation.Charts;  
  3. using Spire.Presentation.Drawing;  
  4. using Spire.Presentation.Collections;  
  5.   
  6. using System.Drawing;   

Code snippets of how to create a presentation chart. Add trendlines and data labels to the chart in C#. 

  1. Presentation ppt = new Presentation();  
  2.   
  3. //Insert a chart and set the chart type.  
  4. RectangleF rect1 = new RectangleF(40, 100, 550, 320);  
  5. IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.ColumnStacked, rect1, false);  
  6.   
  7. //Add a title for the chart and format it.  
  8. chart.ChartTitle.TextProperties.Text = "Sales Report";  
  9. chart.ChartTitle.TextProperties.IsCentered = true;  
  10. chart.ChartTitle.Height = 30;  
  11. chart.HasTitle = true;  
  12.   
  13. // Define some data.  
  14. string[] quarters = new string[] { "1st Qtr""2nd Qtr""3rd Qtr""4th Qtr" };  
  15. int[] sales = new int[] { 210, 320, 180, 500 };  
  16.   
  17. //Append data to ChartData which represents a data table where the chart data is stored.  
  18.   
  19. chart.ChartData[0, 0].Text = "Quarters";  
  20. chart.ChartData[0, 1].Text = "Sales";  
  21. for (int i = 0; i < quarters.Length; ++i)  
  22. {  
  23.     chart.ChartData[i + 1, 0].Value = quarters[i];  
  24.     chart.ChartData[i + 1, 1].Value = sales[i];  
  25. }  
  26.   
  27. // Set series label, category labels and series data.  
  28. chart.Series.SeriesLabel = chart.ChartData["B1""B1"];  
  29. chart.Categories.CategoryLabels = chart.ChartData["A2""A5"];  
  30. chart.Series[0].Values = chart.ChartData["B2""B5"];  
  31.   
  32.   
  33. //Add data points to series and fill each data point with different color.  
  34. for (int i = 0; i < chart.Series[0].Values.Count; i++)  
  35. {  
  36.     ChartDataPoint cdp = new ChartDataPoint(chart.Series[0]);  
  37.     cdp.Index = i;  
  38.     chart.Series[0].DataPoints.Add(cdp);  
  39.   
  40. }  
  41. chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid;  
  42. chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.DarkSeaGreen;  
  43. chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;  
  44. chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.LightBlue;  
  45. chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid;  
  46. chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.LightSeaGreen;  
  47. chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid;  
  48. chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.LightSkyBlue;  
  49.   
  50.   
  51. //Add the trendline to the chart and set TrendlinesType as Linear.  
  52. ITrendlines it = chart.Series[0].AddTrendLine(TrendlinesType.Linear);  
  53.   
  54. //Set the trend line properties to determine what should be displayed.  
  55. it.displayEquation = false;  
  56. it.displayRSquaredValue = false;  
  57.   
  58. // Get the chart series  
  59. ChartSeriesFormatCollection sers = chart.Series;  
  60.   
  61. //Format data labels  
  62. ChartDataLabel cd1 = sers[0].DataLabels.Add();  
  63. cd1.LabelValueVisible = true;  
  64. cd1.Position = ChartDataLabelPosition.InsideBase;  
  65.   
  66. //Save the document to file.  
  67. ppt.SaveToFile("Chart.pptx", FileFormat.Pptx2010);   

Effective screenshot of the presentation chart with trendlines and data labels.


Conclusion

Thanks to Spire.Presentation. I am able to show you how to add trendlines, data label and format the data points for presentation charts programmatically in C#. I hope you have a deeper understanding of chart tools on the presentation slides after reading my article. Thank you for reading. 


Similar Articles