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.

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

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