這篇文章主要介紹了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
通過調用Spire.Presentation.dll
中Itrendline
接口提供的方法 AddTrendLine(TrendlinesType type)
來添加趨勢線,編輯代碼前,請先按照如下第2點中的方法在程序中添加引用Spire.Presentation.dll。
關于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"); } } }
趨勢線添加效果:
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
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#中如何給PPT的圖表添加趨勢線”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。