5

C# 给PPT中的图表添加趋势线

 3 years ago
source link: https://www.cnblogs.com/Yesi/p/15251090.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# 给PPT中的图表添加趋势线

本文内容分享通过C#程序代码给PPT文档中的图表添加数据趋势线的方法。

支持趋势线的图表类型包括二维面积图、条形图、柱形图、柱形图、股价图、xy (散点图) 和气泡图中;不能向三维、堆积、雷达图、饼图、曲面图或圆环图的数据系列添加趋势线。可添加的趋势线类型包括6种,即多项式(Polynomial)趋势线、指数(Exponential)趋势线、线性(Linear)趋势线、对数(Logarithmic)趋势线、幂(Power)趋势线、移动平均(移动平均)趋势线。下面以柱形图表为例,添加趋势线。方法及步骤参考如下。

【程序环境】

  • Visual Studio 2017
  • .net framework 4.6.1
  • Power Point 2013 (.pptx)
  • PPT类库:Spire.Presentation for .NET

   1. 实现方法

    通过调用Spire.Presentation.dll中Itrendline接口提供的方法AddTrendLine(TrendlinesType type)来添加趋势线,编辑代码前,请先按照如下第2点中的方法在程序中添加引用Spire.Presentation.dll。

    2.  关于PPT类库安装:可直接通过Nuget搜索安装到程序。具体方法如下:

     鼠标右键点击“引用”,“管理Nuget包”,然后按照下图步骤操作;

完成安装:

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

namespace AddTrendline
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Presentation类的实例
            Presentation ppt = new Presentation();
            //加载PowerPoint文档
            ppt.LoadFromFile("test.pptx");

            //获取第一张幻灯片
            ISlide slide = ppt.Slides[0];

            //获取幻灯片上的第一个图表
            IChart chart = (IChart)slide.Shapes[0];

            //给图表的第一个数据系列添加线性趋势线
            ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Polynomial);//多项式趋势线
            //ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Exponential);//指数趋势线
            //ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Linear);//线性趋势线
            //ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Logarithmic);//对数趋势线
            //ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Power);//幂趋势线
            //ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.MovingAverage);//移动平均趋势线        

            //显示公式
            trendLine.displayEquation = true;
            //显示R平方值
            trendLine.displayRSquaredValue = true;

            //保存结果文档
            ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("AddTrendline.pptx");
        }
    }
}

趋势线添加效果:

【vb.net】

Imports Spire.Presentation
Imports Spire.Presentation.Charts

Namespace AddTrendline
    Class Program
        Private Shared Sub Main(args As String())
            '创建Presentation类的实例
            Dim ppt As New Presentation()
            '加载PowerPoint文档
            ppt.LoadFromFile("test.pptx")

            '获取第一张幻灯片
            Dim slide As ISlide = ppt.Slides(0)

            '获取幻灯片上的第一个图表
            Dim chart As IChart = DirectCast(slide.Shapes(0), IChart)

            '给图表的第一个数据系列添加线性趋势线
            Dim trendLine As ITrendlines = chart.Series(0).AddTrendLine(TrendlinesType.Polynomial)
            '多项式趋势线
            'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Exponential); '指数趋势线
            'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Linear); '线性趋势线
            'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Logarithmic); '对数趋势线
            'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Power); '幂趋势线
            'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.MovingAverage); '移动平均趋势线        
            '显示公式
            trendLine.displayEquation = True
            '显示R平方值
            trendLine.displayRSquaredValue = True

            '保存结果文档
            ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013)
            System.Diagnostics.Process.Start("AddTrendline.pptx")
        End Sub
    End Class
End Namespace

—End—


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK