C # add trend lines to the charts in the ppt

Time:2022-5-7

This article shares the method of adding data trend lines to the charts in ppt documents through c# program code.

Chart types supporting trend lines include two-dimensional area chart, bar chart, column chart, column chart, stock price chart, XY (scatter chart) and bubble chart; You cannot add trend lines to data series of 3D, stacked, radar, pie, surface, or doughnut charts. There are 6 types of trend lines that can be added, namely polynomial trend line, exponential trend line, linear trend line, logarithmic trend line, power trend line and moving average trend line. Next, take the column chart as an example to add a trend line. The methods and steps are as follows.

[program environment]

   1. Implementation method

By calling spire Presentation. Methods provided by itrendline interface in DLLAddTrendLine(TrendlinesType type)To add a trend line. Before editing the code, please add a reference to spire in the program according to the method in point 2 below Presentation. dll。

    2.  About pptClass library installation: directly through nugetSearch for installed programs. The specific methods are as follows:

Right click “reference”, “manage nuget package”, and then follow the steps below;

 

 

Complete the installation:

 

【C#】

using Spire.Presentation;
using Spire.Presentation.Charts;

namespace AddTrendline
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of the presentation class
            Presentation ppt = new Presentation();
            //Load PowerPoint document
            ppt.LoadFromFile("test.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Get the first chart on the slide
            IChart chart = (IChart)slide.Shapes[0];

            //Add a linear trend line to the first data series of the chart
            ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Polynomial);// Polynomial trend line
            //ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Exponential);// Exponential trend line
            //ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Linear);// Linear trend line
            //ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Logarithmic);// Logarithmic trend line
            //ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Power);// Power trend line
            //ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.MovingAverage);// Moving average trend line        

            //Display formula
            trendLine.displayEquation = true;
            //Display r squared value
            trendLine.displayRSquaredValue = true;

            //Save result document
            ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("AddTrendline.pptx");
        }
    }
}

Trend line adding effect:

 

【vb.net】

Imports Spire.Presentation
Imports Spire.Presentation.Charts

Namespace AddTrendline
    Class Program
        Private Shared Sub Main(args As String())
            'create an instance of the presentation class
            Dim ppt As New Presentation()
            'load PowerPoint document
            ppt.LoadFromFile("test.pptx")

            'get first slide
            Dim slide As ISlide = ppt.Slides(0)

            'get the first chart on the slide
            Dim chart As IChart = DirectCast(slide.Shapes(0), IChart)

            'add a linear trend line to the first data series of the chart
            Dim trendLine As ITrendlines = chart.Series(0).AddTrendLine(TrendlinesType.Polynomial)
            'polynomial trend line
            'ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Exponential); ' Exponential trend line
            'ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Linear); ' Linear trend line
            'ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Logarithmic); ' Logarithmic trend line
            'ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.Power); ' Power trend line
            'ITrendlines trendLine = chart. Series[0]. AddTrendLine(TrendlinesType.MovingAverage); ' Moving average trend line        
            'show formula
            trendLine.displayEquation = True
            'show r squared value
            trendLine.displayRSquaredValue = True

            'save result document
            ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013)
            System.Diagnostics.Process.Start("AddTrendline.pptx")
        End Sub
    End Class
End Namespace

 

—End—