C#如何給PPT中圖表添加趨勢(shì)線詳解
前言
本文內(nèi)容分享通過C#程序代碼給PPT文檔中的圖表添加數(shù)據(jù)趨勢(shì)線的方法。
支持趨勢(shì)線的圖表類型包括二維面積圖、條形圖、柱形圖、柱形圖、股價(jià)圖、xy (散點(diǎn)圖) 和氣泡圖中;不能向三維、堆積、雷達(dá)圖、餅圖、曲面圖或圓環(huán)圖的數(shù)據(jù)系列添加趨勢(shì)線??商砑拥内厔?shì)線類型包括6種,即多項(xiàng)式(Polynomial)趨勢(shì)線、指數(shù)(Exponential)趨勢(shì)線、線性(Linear)趨勢(shì)線、對(duì)數(shù)(Logarithmic)趨勢(shì)線、冪(Power)趨勢(shì)線、移動(dòng)平均(移動(dòng)平均)趨勢(shì)線。下面以柱形圖表為例,添加趨勢(shì)線。方法及步驟參考如下。
【程序環(huán)境】
- Visual Studio 2017
- .net framework 4.6.1
- Power Point 2013 (.pptx)
- PPT類庫:Spire.Presentation for .NET
1.實(shí)現(xiàn)方法
通過調(diào)用Spire.Presentation.dll中Itrendline接口提供的方法AddTrendLine(TrendlinesType type)來添加趨勢(shì)線,編輯代碼前,請(qǐng)先按照如下第2點(diǎn)中的方法在程序中添加引用Spire.Presentation.dll。
2.關(guān)于PPT類庫安裝:可直接通過Nuget搜索安裝到程序。
具體方法如下:
鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理Nuget包”,然后按照下圖步驟操作;




完成安裝:

【C#】
using Spire.Presentation;
using Spire.Presentation.Charts;
namespace AddTrendline
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建Presentation類的實(shí)例
Presentation ppt = new Presentation();
//加載PowerPoint文檔
ppt.LoadFromFile("test.pptx");
//獲取第一張幻燈片
ISlide slide = ppt.Slides[0];
//獲取幻燈片上的第一個(gè)圖表
IChart chart = (IChart)slide.Shapes[0];
//給圖表的第一個(gè)數(shù)據(jù)系列添加線性趨勢(shì)線
ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Polynomial);//多項(xiàng)式趨勢(shì)線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Exponential);//指數(shù)趨勢(shì)線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Linear);//線性趨勢(shì)線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Logarithmic);//對(duì)數(shù)趨勢(shì)線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Power);//冪趨勢(shì)線
//ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.MovingAverage);//移動(dòng)平均趨勢(shì)線
//顯示公式
trendLine.displayEquation = true;
//顯示R平方值
trendLine.displayRSquaredValue = true;
//保存結(jié)果文檔
ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013);
System.Diagnostics.Process.Start("AddTrendline.pptx");
}
}
}
趨勢(shì)線添加效果:

【vb.net】
Imports Spire.Presentation
Imports Spire.Presentation.Charts
Namespace AddTrendline
Class Program
Private Shared Sub Main(args As String())
'創(chuàng)建Presentation類的實(shí)例
Dim ppt As New Presentation()
'加載PowerPoint文檔
ppt.LoadFromFile("test.pptx")
'獲取第一張幻燈片
Dim slide As ISlide = ppt.Slides(0)
'獲取幻燈片上的第一個(gè)圖表
Dim chart As IChart = DirectCast(slide.Shapes(0), IChart)
'給圖表的第一個(gè)數(shù)據(jù)系列添加線性趨勢(shì)線
Dim trendLine As ITrendlines = chart.Series(0).AddTrendLine(TrendlinesType.Polynomial)
'多項(xiàng)式趨勢(shì)線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Exponential); '指數(shù)趨勢(shì)線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Linear); '線性趨勢(shì)線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Logarithmic); '對(duì)數(shù)趨勢(shì)線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.Power); '冪趨勢(shì)線
'ITrendlines trendLine = chart.Series[0].AddTrendLine(TrendlinesType.MovingAverage); '移動(dòng)平均趨勢(shì)線
'顯示公式
trendLine.displayEquation = True
'顯示R平方值
trendLine.displayRSquaredValue = True
'保存結(jié)果文檔
ppt.SaveToFile("AddTrendline.pptx", FileFormat.Pptx2013)
System.Diagnostics.Process.Start("AddTrendline.pptx")
End Sub
End Class
End Namespace
總結(jié)
到此這篇關(guān)于C#如何給PPT中圖表添加趨勢(shì)線的文章就介紹到這了,更多相關(guān)C#給PPT圖表添加趨勢(shì)線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# Newtonsoft 六個(gè)值得使用的特性(下)
這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
C#?wpf使用DockPanel實(shí)現(xiàn)制作截屏框
做桌面客戶端的時(shí)候有時(shí)需要實(shí)現(xiàn)截屏功能,能夠在界面上框選截屏,本文就來為大家介紹一下wpf如何使用DockPanel制作截屏框吧,感興趣的可以了解下2023-09-09
C#無邊框窗體實(shí)現(xiàn)以及拖動(dòng)代碼
我們給大家分享了關(guān)于C#無邊框窗體實(shí)現(xiàn)以及拖動(dòng)代碼,大家在程序設(shè)計(jì)的時(shí)候如果用的到一起跟著小編學(xué)習(xí)下吧。2018-03-03
C#?使用SpecFlow創(chuàng)建BDD測(cè)試用例的示例代碼
這篇文章主要介紹了C#?使用SpecFlow創(chuàng)建BDD測(cè)試用例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#實(shí)現(xiàn)圖形區(qū)域組合操作的方法
這篇文章主要介紹了C#實(shí)現(xiàn)圖形區(qū)域組合操作的方法,涉及C#操作圖片實(shí)現(xiàn)組合操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Unity Shader實(shí)現(xiàn)素描風(fēng)格的渲染
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)素描風(fēng)格的渲染,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

