在 .NET 中建立或更新 PowerPoint 簡報圖表
概觀
本文提供了使用 Aspose.Slides for .NET 建立與自訂圖表的完整指南。您將學會如何以程式碼方式將圖表加入投影片、填入資料,並套用各種格式設定以符合特定的設計需求。整篇文章皆附有詳細的程式碼範例,說明從初始化簡報與圖表物件、設定系列、座標軸與圖例的每個步驟。透過本指南,您將能深入了解如何在 .NET 應用程式中整合動態圖表產生,簡化製作資料驅動簡報的流程。
建立圖表
圖表可協助使用者快速視覺化資料,並發掘在表格或試算表中不易察覺的洞見。
為什麼要建立圖表?
使用圖表,您可以:
- 在單一投影片上彙總、濃縮或概括大量資料;
- 揭示資料中的模式與趨勢;
- 推斷資料隨時間或相對於特定測量單位的方向與動能;
- 偵測異常值、偏差、錯誤與不合理的資料;
- 傳遞或展示複雜資料。
在 PowerPoint 中,您可以透過 Insert 功能建立圖表,該功能提供多種圖表樣板。使用 Aspose.Slides,您可以建立一般圖表(基於常見圖表類型)以及自訂圖表。
建立群組直條圖
此範例使用 AddChart 建立一個群組直條圖,比較三個類別中的兩個系列。系列使用紅色與綠色填色,且個別標籤顯示類別名稱、系列名稱與數值。工作表索引 0 代表嵌入式圖表工作簿的第一張工作表。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 1, "Series 1");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
seriesNameCell = workbook.GetCell(worksheetIndex, 0, 2, "Series 2");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var categoryCell = workbook.GetCell(worksheetIndex, 1, 0, "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 2, 0, "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 3, 0, "Category 3");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series[0];
var valueCell = workbook.GetCell(worksheetIndex, 1, 1, 20);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 1, 50);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 1, 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red;
series = chart.ChartData.Series[1];
valueCell = workbook.GetCell(worksheetIndex, 1, 2, 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 2, 10);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 2, 60);
series.DataPoints.AddDataPointForBarSeries(valueCell);
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Green;
var label = series.DataPoints[0].Label;
label.DataLabelFormat.ShowCategoryName = true;
label = series.DataPoints[1].Label;
label.DataLabelFormat.ShowSeriesName = true;
label = series.DataPoints[2].Label;
label.DataLabelFormat.ShowValue = true;
label.DataLabelFormat.ShowSeriesName = true;
label.DataLabelFormat.Separator = "/";
presentation.Save("AsposeChart.pptx", SaveFormat.Pptx);
結果:

建立散佈圖
散佈圖(亦稱為散點圖或 x‑y 圖)常用於檢查模式或展示兩個變數之間的相關性。
當符合以下條件時使用散佈圖:
- 您有成對的數值資料。
- 您有兩個相互配合的變數。
- 您想判斷這兩個變數是否相關。
- 您有一個自變數對多個因變數具有多個值。
此範例繪製兩組 x‑y 值。第一個系列使用直線與星形標記,第二個系列使用平滑線與圓形標記;兩者的 Size 都為 10。 Symbol 屬性控制標記形狀。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 20, 20, 500, 300);
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
chart.ChartData.Series.Clear();
var seriesNameCell = workbook.GetCell(worksheetIndex, 1, 1, "Series 1");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
seriesNameCell = workbook.GetCell(worksheetIndex, 1, 3, "Series 2");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var series = chart.ChartData.Series[0];
var xValueCell = workbook.GetCell(worksheetIndex, 2, 1, 1);
var yValueCell = workbook.GetCell(worksheetIndex, 2, 2, 3);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 3, 1, 2);
yValueCell = workbook.GetCell(worksheetIndex, 3, 2, 10);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
series.Type = ChartType.ScatterWithStraightLinesAndMarkers;
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Star;
series = chart.ChartData.Series[1];
xValueCell = workbook.GetCell(worksheetIndex, 2, 3, 5);
yValueCell = workbook.GetCell(worksheetIndex, 2, 4, 2);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 3, 3, 3);
yValueCell = workbook.GetCell(worksheetIndex, 3, 4, 1);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 4, 3, 2);
yValueCell = workbook.GetCell(worksheetIndex, 4, 4, 2);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 5, 3, 5);
yValueCell = workbook.GetCell(worksheetIndex, 5, 4, 1);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Circle;
presentation.Save("AsposeChart.pptx", SaveFormat.Pptx);
結果:

建立圓餅圖
圓餅圖最適合用於顯示資料的部分與整體關係,特別是資料包含分類標籤與數值時。然而,若資料包含太多部分或標籤,建議改用直條圖。
此範例建立一個包含三個季節值的圓餅圖。每個切片皆有自訂的填色與邊框,標籤顯示不同組合的數值、百分比與系列資訊。 FirstSliceAngle 屬性將第一切片的起始角度設定為 180 度。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Pie, 20, 20, 500, 300);
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var categoryCell = workbook.GetCell(0, 1, 0, "1st Qtr");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 2, 0, "2nd Qtr");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 3, 0, "3rd Qtr");
chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(0, 0, 1, "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var valueCell = workbook.GetCell(worksheetIndex, 1, 1, 20);
series.DataPoints.AddDataPointForPieSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 1, 50);
series.DataPoints.AddDataPointForPieSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 1, 30);
series.DataPoints.AddDataPointForPieSeries(valueCell);
chart.ChartData.SeriesGroups[0].IsColorVaried = true;
var firstSlice = series.DataPoints[0];
firstSlice.Format.Fill.FillType = FillType.Solid;
firstSlice.Format.Fill.SolidFillColor.Color = Color.Cyan;
firstSlice.Format.Line.FillFormat.FillType = FillType.Solid;
firstSlice.Format.Line.FillFormat.SolidFillColor.Color = Color.Gray;
firstSlice.Format.Line.Width = 3.0;
firstSlice.Format.Line.Style = LineStyle.ThinThick;
firstSlice.Format.Line.DashStyle = LineDashStyle.LargeDash;
var secondSlice = series.DataPoints[1];
secondSlice.Format.Fill.FillType = FillType.Solid;
secondSlice.Format.Fill.SolidFillColor.Color = Color.Brown;
secondSlice.Format.Line.FillFormat.FillType = FillType.Solid;
secondSlice.Format.Line.FillFormat.SolidFillColor.Color = Color.Blue;
secondSlice.Format.Line.Width = 3.0;
secondSlice.Format.Line.Style = LineStyle.Single;
secondSlice.Format.Line.DashStyle = LineDashStyle.LargeDashDot;
var thirdSlice = series.DataPoints[2];
thirdSlice.Format.Fill.FillType = FillType.Solid;
thirdSlice.Format.Fill.SolidFillColor.Color = Color.Coral;
thirdSlice.Format.Line.FillFormat.FillType = FillType.Solid;
thirdSlice.Format.Line.FillFormat.SolidFillColor.Color = Color.Red;
thirdSlice.Format.Line.Width = 2.0;
thirdSlice.Format.Line.Style = LineStyle.ThinThin;
thirdSlice.Format.Line.DashStyle = LineDashStyle.LargeDashDotDot;
var firstLabel = series.DataPoints[0].Label;
firstLabel.DataLabelFormat.ShowValue = true;
var secondLabel = series.DataPoints[1].Label;
secondLabel.DataLabelFormat.ShowValue = true;
secondLabel.DataLabelFormat.ShowLegendKey = true;
secondLabel.DataLabelFormat.ShowPercentage = true;
var thirdLabel = series.DataPoints[2].Label;
thirdLabel.DataLabelFormat.ShowSeriesName = true;
thirdLabel.DataLabelFormat.ShowPercentage = true;
series.Labels.DefaultDataLabelFormat.ShowLeaderLines = true;
chart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;
presentation.Save("PieChart.pptx", SaveFormat.Pptx);
結果:

若需改善圓餅圖外側標籤的可讀性,請參考 Format Leader Lines 中示範如何定位標籤並自訂線條的顏色、寬度、透明度與虛線樣式。
建立折線圖
折線圖(亦稱為折線圖)最適合用於展示隨時間變化的數值。使用折線圖,您可以一次比較大量資料、追蹤趨勢、突顯資料系列中的異常等。
此範例使用 AddChart 建立一個折線圖,使用預設系列與類別,然後將簡報另存為 PPTX。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var lineChart = slide.Shapes.AddChart(ChartType.Line, 20, 20, 500, 300);
presentation.Save("lineChart.pptx", SaveFormat.Pptx);
預設情況下,折線圖的資料點以直線相連。若希望以虛線連接,請將 DashStyle 設為 LineDashStyle 中的任一值:
using Aspose.Slides;
using Aspose.Slides.Charts;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var lineChart = slide.Shapes.AddChart(ChartType.Line, 20, 20, 500, 300);
foreach (var series in lineChart.ChartData.Series)
{
series.Format.Line.DashStyle = LineDashStyle.Dash;
}
結果:

建立樹狀圖
樹狀圖最適合用於銷售資料,能顯示資料類別的相對大小,並快速突顯各類別中佔較大貢獻的項目。
此範例建立一個包含八個葉節點、分為四個主幹與兩條分支的樹狀圖。 SetGroupingItem 方法在分組層級 1 設定主幹、在層級 2 設定分支。值決定矩形的相對大小,類別標籤與重疊的父層標籤說明階層結構。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Treemap, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
// 分支 1
var categoryCell = workbook.GetCell(0, "C1", "Leaf1");
var leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem1");
leaf.GroupingLevels.SetGroupingItem(2, "Branch1");
categoryCell = workbook.GetCell(0, "C2", "Leaf2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C3", "Leaf3");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem2");
categoryCell = workbook.GetCell(0, "C4", "Leaf4");
chart.ChartData.Categories.Add(categoryCell);
// 分支 2
categoryCell = workbook.GetCell(0, "C5", "Leaf5");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem3");
leaf.GroupingLevels.SetGroupingItem(2, "Branch2");
categoryCell = workbook.GetCell(0, "C6", "Leaf6");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C7", "Leaf7");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem4");
categoryCell = workbook.GetCell(0, "C8", "Leaf8");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.Treemap);
series.Labels.DefaultDataLabelFormat.ShowCategoryName = true;
var valueCell = workbook.GetCell(0, "D1", 4);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D2", 5);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D3", 3);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D4", 6);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D5", 9);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D6", 9);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D7", 4);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D8", 3);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
series.ParentLabelLayout = ParentLabelLayoutType.Overlapping;
presentation.Save("Treemap.pptx", SaveFormat.Pptx);
結果:

建立瀑布圖
瀑布圖顯示正向與負向變化如何共同構成最終值。例如,利潤分析可從營收開始,減去成本,再加上其他收入。變化欄位在前後累計值之間浮動,而總計欄位則從 0 延伸至累積值。
以下範例使用千美元作為單位:
| 類別 | 工作簿值 | 欄位類型 |
|---|---|---|
| 營收 | 1000 | 增加 |
| 銷貨成本 | -400 | 減少 |
| 毛利 | 600 | 中間合計 |
| 營運費用 | -200 | 減少 |
| 其他收入 | 50 | 增加 |
| 淨利 | 450 | 最終合計 |
若要將資料點標記為中間或最終合計,請將其 SetAsTotal 屬性設為 true,並在該點的工作表儲存格中提供實際累積金額:毛利為 600(1000 - 400),淨利為 450(600 - 200 + 50)。此屬性僅標記欄位為合計,並不會自行計算或填入儲存格。請勿使用 0 作為佔位符,亦不要為合計點提供其他變化值。合計欄位會直接顯示累積值,而不會再次加到目前的累計值上。
以下完整的 C# 範例使用 ChartType 的 Waterfall 成員建立圖表,清除預設資料,並填入嵌入式工作簿。工作表索引 0 代表第一個工作表;A 欄存放類別名稱,B 欄存放數值。每個數值儲存格皆透過 AddDataPointForWaterfallSeries 加入。零基索引 2 與 5 分別代表中間合計與最終合計。啟用 ShowValue 後會在欄位上顯示金額。此範例不需要輸入簡報或工作簿。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Waterfall, 20, 20, 680, 200);
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("Profit breakdown (USD thousands)");
chart.ChartTitle.Overlay = false;
chart.HasLegend = false;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
workbook.Clear(worksheetIndex);
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 1, "Profit");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.Waterfall);
var categories = new[] { "Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit" };
var values = new double[] { 1000, -400, 600, -200, 50, 450 };
for (var i = 0; i < categories.Length; i++)
{
var categoryCell = workbook.GetCell(worksheetIndex, i + 1, 0, categories[i]);
chart.ChartData.Categories.Add(categoryCell);
var valueCell = workbook.GetCell(worksheetIndex, i + 1, 1, values[i]);
series.DataPoints.AddDataPointForWaterfallSeries(valueCell);
}
// 第三和第六個點代表總計,而不是額外的變化。
series.DataPoints[2].SetAsTotal = true;
series.DataPoints[5].SetAsTotal = true;
series.ShowConnectorLines = true;
series.Labels.DefaultDataLabelFormat.ShowValue = true;
presentation.Save("WaterfallChart.pptx", SaveFormat.Pptx);
將系列層級的 ShowConnectorLines 屬性設為 true,即可顯示連接相鄰欄位的線條,讓累計值更易追蹤。若要隱藏這些線條,只需在儲存前將 series.ShowConnectorLines = false; 取代原先的指定。此變更僅影響線條可見性,並不會改變資料或合計。
儲存後的簡報顯示毛利 600 與淨利 450,兩個合計皆從 0 開始:

建立股票圖表
股票圖表用於顯示開盤價、最高價、最低價與收盤價等金融資料,協助分析市場走勢與波動性。它提供對股票表現的關鍵洞見,協助投資人與分析師作出明智決策。
此範例建立一個依序包含開盤、最高、最低與收盤四個系列的 OHLC 圖表,並包含三個類別。 HasUpDownBars 屬性啟用上下棒,HiLowLinesFormat 控制顯示價位範圍的高低線,個別系列線條與次要水平格線則被隱藏。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.OpenHighLowClose, 20, 20, 500, 300, false);
var workbook = chart.ChartData.ChartDataWorkbook;
var categoryCell = workbook.GetCell(0, 1, 0, "A");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 2, 0, "B");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 3, 0, "C");
chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(0, 0, 1, "Open");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.GetCell(0, 0, 2, "High");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.GetCell(0, 0, 3, "Low");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.GetCell(0, 0, 4, "Close");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
var series = chart.ChartData.Series[0];
var valueCell = workbook.GetCell(0, 1, 1, 72);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 1, 25);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 1, 38);
series.DataPoints.AddDataPointForStockSeries(valueCell);
series = chart.ChartData.Series[1];
valueCell = workbook.GetCell(0, 1, 2, 172);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 2, 57);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 2, 57);
series.DataPoints.AddDataPointForStockSeries(valueCell);
series = chart.ChartData.Series[2];
valueCell = workbook.GetCell(0, 1, 3, 12);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 3, 12);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 3, 13);
series.DataPoints.AddDataPointForStockSeries(valueCell);
series = chart.ChartData.Series[3];
valueCell = workbook.GetCell(0, 1, 4, 25);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 4, 38);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 4, 50);
series.DataPoints.AddDataPointForStockSeries(valueCell);
chart.ChartData.SeriesGroups[0].UpDownBars.HasUpDownBars = true;
chart.ChartData.SeriesGroups[0].HiLowLinesFormat.Line.FillFormat.FillType = FillType.Solid;
foreach (var stockSeries in chart.ChartData.Series)
{
stockSeries.Format.Line.FillFormat.FillType = FillType.NoFill;
}
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
presentation.Save("Stock-chart.pptx", SaveFormat.Pptx);
結果:

建立箱形圖與鬍鬚圖
箱形圖與鬍鬚圖用於透過中位數、四分位數與可能的異常值等統計指標,展示資料的分佈情形。它們在探索性資料分析與統計研究中相當有用,能快速了解資料變異性並辨識異常。
此範例以六個數值建立箱形圖與鬍鬚圖。 QuartileMethod 屬性選用排除式四分位計算。範例同時啟用平均線、平均標記、內部點與異常點。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.BoxAndWhisker, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var categoryCell = workbook.GetCell(0, "A1", "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A2", "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A3", "Category 3");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A4", "Category 4");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A5", "Category 5");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A6", "Category 6");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.BoxAndWhisker);
series.QuartileMethod = QuartileMethodType.Exclusive;
series.ShowMeanLine = true;
series.ShowMeanMarkers = true;
series.ShowInnerPoints = true;
series.ShowOutlierPoints = true;
var valueCell = workbook.GetCell(0, "B1", 15);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B2", 41);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B3", 16);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B4", 10);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B5", 23);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B6", 16);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
presentation.Save("BoxAndWhisker.pptx", SaveFormat.Pptx);
建立漏斗圖
漏斗圖用於視覺化包含連續階段的流程,資料量會隨著每一步的進行而遞減。它們特別適合分析轉換率、找出瓶頸,並追蹤銷售或行銷流程的效率。
此範例使用 AddChart 在既有簡報的第一張投影片加入漏斗圖,將預設資料替換為六個類別,值介於 50 至 500,然後儲存更新後的簡報。輸入簡報必須至少包含一張投影片。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation("test.pptx");
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Funnel, 50, 50, 500, 400);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var categoryCell = workbook.GetCell(0, "A1", "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A2", "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A3", "Category 3");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A4", "Category 4");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A5", "Category 5");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.getCell(0, "A6", "Category 6");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.Funnel);
var valueCell = workbook.GetCell(0, "B1", 50);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B2", 100);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B3", 200);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B4", 300);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B5", 400);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B6", 500);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
presentation.Save("Funnel.pptx", SaveFormat.Pptx);
結果:

建立旭日圖
旭日圖用於視覺化階層資料,以同心圓環的方式顯示不同層級。它們有助於說明部分與整體的關係,且非常適合以緊湊且清晰的方式呈現巢狀類別與子類別。
此範例建立一個包含八個葉節點、分為四個主幹與兩條分支的旭日圖。 SetGroupingItem 方法在分組層級 1 設定主幹、在層級 2 設定分支。類別標籤說明各區塊,值則決定相對大小。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Sunburst, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
// 分支 1
var categoryCell = workbook.GetCell(0, "C1", "Leaf1");
var leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem1");
leaf.GroupingLevels.SetGroupingItem(2, "Branch1");
categoryCell = workbook.GetCell(0, "C2", "Leaf2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C3", "Leaf3");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem2");
categoryCell = workbook.GetCell(0, "C4", "Leaf4");
chart.ChartData.Categories.Add(categoryCell);
// 分支 2
categoryCell = workbook.GetCell(0, "C5", "Leaf5");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem3");
leaf.GroupingLevels.SetGroupingItem(2, "Branch2");
categoryCell = workbook.GetCell(0, "C6", "Leaf6");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C7", "Leaf7");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem4");
categoryCell = workbook.GetCell(0, "C8", "Leaf8");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.Sunburst);
series.Labels.DefaultDataLabelFormat.ShowCategoryName = true;
var valueCell = workbook.GetCell(0, "D1", 4);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D2", 5);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D3", 3);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D4", 6);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D5", 9);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D6", 9);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D7", 4);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D8", 3);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
presentation.Save("Sunburst.pptx", SaveFormat.Pptx);
結果:

建立直方圖
直方圖顯示數值在各區間(箱)中的分布情形。若要在每月訂單處理時間報表中保持區間的一致性,應使用相同的箱寬與閾值,這樣才能讓不同月份的分布更易比較。
將水平軸的 AggregationType 設為 AxisAggregationType 中的任一模式。 BinWidth 只適用於 ByBinWidth;NumberOfBins 只適用於 ByNumberOfBins。使用 Automatic 可讓圖表自行決定箱子。
使用固定箱寬
此範例將處理時間分為 10 分鐘的區間,另外為 ≤10 分鐘與 >40 分鐘各設一個箱。可透過 IsUnderflowBin 與 IsOverflowBin 啟用這兩個箱。若要自行指定 UnderflowBin 與 OverflowBin 閾值,需將 IsAutomaticUnderflowBin 與 IsAutomaticOverflowBin 設為 false。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Histogram, 20, 20, 680, 200);
chart.HasLegend = false;
chart.HasTitle = false;
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var series = chart.ChartData.Series.Add(ChartType.Histogram);
double[] values = { 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 };
for (var i = 0; i < values.Length; i++)
{
var valueCell = workbook.GetCell(0, i, 0, values[i]);
series.DataPoints.AddDataPointForHistogramSeries(valueCell);
}
var axis = chart.Axes.HorizontalAxis;
axis.AggregationType = AxisAggregationType.ByBinWidth;
axis.BinWidth = 10;
axis.IsUnderflowBin = true;
axis.IsAutomaticUnderflowBin = false;
axis.UnderflowBin = 10;
axis.IsOverflowBin = true;
axis.IsAutomaticOverflowBin = false;
axis.OverflowBin = 40;
presentation.Save("Histogram-bin-width.pptx", SaveFormat.Pptx);
在 PowerPoint 中顯示的簡報會呈現 3、4、4、4、2 的箱計數。低於箱包含 5、10、10,因此其門檻為「含等於」;超過箱僅包含 41、50,其門檻為「不含等於」;40 仍屬於前一箱。內部箱的條件為「大於下界且小於或等於上界」。

指定箱數
若需固定細節層級而非固定間距,可要求將相同資料分為五個箱。此範例將低於與超過箱關閉。圖表會根據資料範圍自動計算箱寬,因而在不同報表間可能出現不同的邊界。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Histogram, 20, 20, 680, 200);
chart.HasLegend = false;
chart.HasTitle = false;
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var series = chart.ChartData.Series.Add(ChartType.Histogram);
double[] values = { 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 };
for (var i = 0; i < values.Length; i++)
{
var valueCell = workbook.GetCell(0, i, 0, values[i]);
series.DataPoints.AddDataPointForHistogramSeries(valueCell);
}
var axis = chart.Axes.HorizontalAxis;
axis.AggregationType = AxisAggregationType.ByNumberOfBins;
axis.NumberOfBins = 5;
presentation.Save("Histogram-bin-count.pptx", SaveFormat.Pptx);
五個箱的計數分別為 4、4、4、4、1。將此分布與上面的固定寬度結果比較:輸入值相同,但邊界不同。

建立雷達圖
雷達圖用於在二維平面上展示多變量資料,讓多個變數能同時進行直觀比較。它們特別適合辨識多項績效指標或屬性之間的模式、優勢與弱點。
此範例使用 AddChart 建立雷達圖,使用預設系列與類別,然後將簡報另存為 PPTX。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
slide.Shapes.AddChart(ChartType.Radar, 20, 20, 500, 300);
presentation.Save("Radar-chart.pptx", SaveFormat.Pptx);
結果:

建立多類別圖表
多類別圖表用於呈現涉及多個類別分組的資料,讓您能同時在多個維度上比較數值。當需要分析複雜、多層次資料集中的趨勢與關係時,此類圖表特別有用。
此範例建立一個群組直條圖,包含八個類別,排列為四組配對。 SetGroupingItem 方法在分組層級 1 為每對類別設定共同標籤,產生同時具備單一與群組標籤的類別軸。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var worksheetIndex = 0;
var categoryCell = workbook.GetCell(0, "c2", "A");
var category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group1");
categoryCell = workbook.GetCell(0, "c3", "B");
category = chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "c4", "C");
category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group2");
categoryCell = workbook.GetCell(0, "c5", "D");
category = chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "c6", "E");
category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group3");
categoryCell = workbook.GetCell(0, "c7", "F");
category = chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "c8", "G");
category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group4");
categoryCell = workbook.GetCell(0, "c9", "H");
category = chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(0, "D1", "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.ClusteredColumn);
var valueCell = workbook.GetCell(worksheetIndex, "D2", 10);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D3", 20);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D4", 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D5", 40);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D6", 50);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D7", 60);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D8", 70);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D9", 80);
series.DataPoints.AddDataPointForBarSeries(valueCell);
presentation.Save("AsposeChart.pptx", SaveFormat.Pptx);
結果:

建立地圖圖表
地圖圖表用於將資訊映射至特定地理位置(如國家、州或城市),以視覺化的方式呈現地理資料。它們在分析區域趨勢、人口統計資料與空間分布時特別有幫助,且能以清晰且具有吸引力的方式呈現。
此範例使用 AddChart 建立地圖圖表,使用預設的地理資料,並儲存以供 PowerPoint 檢視。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Map, 20, 20, 500, 300);
presentation.Save("mapChart.pptx", SaveFormat.Pptx);
結果:

Note
上圖顯示於 PowerPoint 中開啟的已儲存簡報。Aspose.Slides 正確寫入了地圖圖表與其資料,但本身不會繪製地圖圖表:當投影片被渲染為圖像或轉換為 PDF、SVG 時,圖表區域會呈現空白。其他同投影片上的圖形不受影響。建立組合圖表
組合圖表(或稱 Combo 圖表)在同一圖表中結合兩種或以上的圖表類型。此圖表可讓您突顯、比較或檢視多個資料集之間的差異,協助辨識它們之間的關係。

此範例將兩個群組直條系列與一條折線系列結合於四個類別之中。直條使用主垂直軸,而 PlotOnSecondAxis 則將折線放置於右側的次要垂直軸。輔助函式負責設定標題、圖例、軸標籤與格線。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
CreateComboChart();
static void CreateComboChart()
{
using var presentation = new Presentation();
var chart = CreateChartWithFirstSeries(presentation.Slides[0]);
AddSecondSeriesToChart(chart);
AddThirdSeriesToChart(chart);
SetPrimaryAxesFormat(chart);
SetSecondaryAxesFormat(chart);
presentation.Save("combo-chart.pptx", SaveFormat.Pptx);
}
static IChart CreateChartWithFirstSeries(ISlide slide)
{
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("Chart Title");
chart.ChartTitle.Overlay = false;
var portionFormat = chart.ChartTitle.TextFrameForOverriding.Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
portionFormat.FontBold = NullableBool.False;
portionFormat.FontHeight = 18f;
chart.Legend.Position = LegendPositionType.Bottom;
chart.Legend.TextFormat.PortionFormat.FontHeight = 12f;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
var categoryCell = workbook.GetCell(worksheetIndex, 1, 0, "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 2, 0, "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 3, 0, "Category 3");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 4, 0, "Category 4");
chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 1, "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
series.ParentSeriesGroup.Overlap = -25;
series.ParentSeriesGroup.GapWidth = 220;
var valueCell = workbook.GetCell(worksheetIndex, 1, 1, 4.3);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 1, 2.5);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 1, 3.5);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 4, 1, 4.5);
series.DataPoints.AddDataPointForBarSeries(valueCell);
return chart;
}
static void AddSecondSeriesToChart(IChart chart)
{
var workbook = chart.ChartData.ChartDataWorkbook;
const int worksheetIndex = 0;
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 2, "Series 2");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.ClusteredColumn);
series.ParentSeriesGroup.Overlap = -25;
series.ParentSeriesGroup.GapWidth = 220;
var valueCell = workbook.GetCell(worksheetIndex, 1, 2, 2.4);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 2, 4.4);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 2, 1.8);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 4, 2, 2.8);
series.DataPoints.AddDataPointForBarSeries(valueCell);
}
static void AddThirdSeriesToChart(IChart chart)
{
var workbook = chart.ChartData.ChartDataWorkbook;
const int worksheetIndex = 0;
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 3, "Series 3");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.Line);
var valueCell = workbook.GetCell(worksheetIndex, 1, 3, 2.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 3, 2.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 3, 3.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 4, 3, 5.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
series.PlotOnSecondAxis = true;
}
static void SetPrimaryAxesFormat(IChart chart)
{
var horizontalAxis = chart.Axes.HorizontalAxis;
horizontalAxis.TextFormat.PortionFormat.FontHeight = 12f;
horizontalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(horizontalAxis, "X Axis");
var verticalAxis = chart.Axes.VerticalAxis;
verticalAxis.TextFormat.PortionFormat.FontHeight = 12f;
verticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(verticalAxis, "Y Axis 1");
var majorGridLinesFormat = verticalAxis.MajorGridLinesFormat.Line.FillFormat;
majorGridLinesFormat.FillType = FillType.Solid;
majorGridLinesFormat.SolidFillColor.Color = Color.FromArgb(217, 217, 217);
}
static void SetSecondaryAxesFormat(IChart chart)
{
var secondaryHorizontalAxis = chart.Axes.SecondaryHorizontalAxis;
secondaryHorizontalAxis.Position = AxisPositionType.Bottom;
secondaryHorizontalAxis.CrossType = CrossesType.Maximum;
secondaryHorizontalAxis.IsVisible = false;
secondaryHorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
secondaryHorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
var secondaryVerticalAxis = chart.Axes.SecondaryVerticalAxis;
secondaryVerticalAxis.Position = AxisPositionType.Right;
secondaryVerticalAxis.TextFormat.PortionFormat.FontHeight = 12f;
secondaryVerticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
secondaryVerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
secondaryVerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(secondaryVerticalAxis, "Y Axis 2");
}
static void SetAxisTitle(IAxis axis, string axisTitle)
{
axis.HasTitle = true;
axis.Title.Overlay = false;
var titlePortionFormat = axis.Title.AddTextFrameForOverriding(axisTitle).Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
titlePortionFormat.FontBold = NullableBool.False;
titlePortionFormat.FontHeight = 12f;
}
更新圖表
Aspose.Slides for .NET 讓您可以透過修改圖表資料、格式與樣式來更新 PowerPoint 圖表。此功能簡化了讓簡報隨動態內容保持最新的流程,確保圖表正確反映最新資料與視覺標準。
此範例更新現有簡報第一張投影片上名為「My chart」的圖表。它需要兩個欄位系列,各有三個資料點,類別標籤放在 A 欄,系列名稱放在第一張工作表的第一列。範例使用 GetCell 變更工作簿儲存格,更新系列值,加入第三個系列,並透過 Type 將圖表改為群組圓柱體。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
const string chartName = "My chart";
using var presentation = new Presentation("ExistingChart.pptx");
var slide = presentation.Slides[0];
foreach (IShape shape in slide.Shapes)
{
if (shape is IChart chart && chart.Name == chartName)
{
var worksheetIndex = 0;
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.GetCell(worksheetIndex, 1, 0, "Modified Category 1");
workbook.GetCell(worksheetIndex, 2, 0, "Modified Category 2");
var series = chart.ChartData.Series[0];
workbook.GetCell(worksheetIndex, 0, 1, "New_Series 1");
series.DataPoints[0].Value.Data = 90;
series.DataPoints[1].Value.Data = 123;
series.DataPoints[2].Value.Data = 44;
series = chart.ChartData.Series[1];
workbook.GetCell(worksheetIndex, 0, 2, "New_Series 2");
series.DataPoints[0].Value.Data = 23;
series.DataPoints[1].Value.Data = 67;
series.DataPoints[2].Value.Data = 99;
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 3, "Series 3");
series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var valueCell = workbook.GetCell(worksheetIndex, 1, 3, 20);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 3, 50);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 3, 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
chart.Type = ChartType.ClusteredCylinder;
}
}
presentation.Save("AsposeChartModified.pptx", SaveFormat.Pptx);
設定圖表的資料範圍
若要檢視現有圖表已使用的範圍,請參考 Retrieve a Chart’s Data Range。
Aspose.Slides for .NET 提供彈性,可將工作表中的特定資料範圍直接指定為圖表資料來源。這意味著您可以直接對應工作表的某個區段至圖表,控制哪些儲存格會影響圖表的系列與類別。如此一來,您即可輕鬆更新與同步圖表,使簡報即時反映工作表中的最新資料變更,確保資訊的正確性與時效性。
此範例使用 SetRange,將現有簡報第一張投影片上名為「My chart」的圖表資料來源設定為 Sheet1!A1:B4。圖表工作簿必須包含該工作表,且該範圍需有第一列的系列標題、A 欄的類別標籤與 B 欄的三個數值。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
const string chartName = "My chart";
using var presentation = new Presentation("ExistingChart.pptx");
var slide = presentation.Slides[0];
foreach (IShape shape in slide.Shapes)
{
if (shape is IChart chart && chart.Name == chartName)
{
chart.ChartData.SetRange("Sheet1!A1:B4");
}
}
presentation.Save("SetDataRange.pptx", SaveFormat.Pptx);
在圖表中使用預設標記
使用預設標記時,每個圖表系列會自動獲得不同的預設標記符號。
此範例使用 AddChart 建立一個包含兩條折線系列與圖例的圖表。 Marker 設定保留了自動指派的標記符號。第一系列包含負值與缺失值(以 null 表示),第二系列則在每個類別都有值。
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.LineWithMarkers, 10, 10, 400, 400);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var seriesNameCell = workbook.GetCell(0, 0, 1, "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var categoryCell = workbook.GetCell(0, 1, 0, "C1");
chart.ChartData.Categories.Add(categoryCell);
var valueCell = workbook.GetCell(0, 1, 1, 24);
series.DataPoints.AddDataPointForLineSeries(valueCell);
categoryCell = workbook.GetCell(0, 2, 0, "C2");
chart.ChartData.Categories.Add(categoryCell);
valueCell = workbook.GetCell(0, 2, 1, 23);
series.DataPoints.AddDataPointForLineSeries(valueCell);
categoryCell = workbook.GetCell(0, 3, 0, "C3");
chart.ChartData.Categories.Add(categoryCell);
valueCell = workbook.GetCell(0, 3, 1, -10);
series.DataPoints.AddDataPointForLineSeries(valueCell);
categoryCell = workbook.GetCell(0, 4, 0, "C4");
chart.ChartData.Categories.Add(categoryCell);
valueCell = workbook.GetCell(0, 4, 1, null);
series.DataPoints.AddDataPointForLineSeries(valueCell);
seriesNameCell = workbook.GetCell(0, 0, 2, "Series 2");
var secondSeries = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
valueCell = workbook.GetCell(0, 1, 2, 30);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 2, 10);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 2, 60);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(0, 4, 2, 40);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
chart.HasLegend = true;
chart.Legend.Overlay = false;
presentation.Save("DefaultMarkersInChart.pptx", SaveFormat.Pptx);
常見問題集
Aspose.Slides for .NET 支援哪些圖表類型?
Aspose.Slides for .NET 支援廣泛的圖表類型,包括長條圖、折線圖、圓餅圖、區域圖、散佈圖、直方圖、雷達圖等,讓您可依資料視覺化需求選擇最合適的圖表。
如何在投影片中加入新圖表?
加入圖表的步驟為:先建立 Presentation 物件,依索引取得目標投影片,然後呼叫加入圖表的方法,同時指定圖表類型與初始資料,即可將圖表直接嵌入簡報。
如何更新圖表中顯示的資料?
您可以透過存取圖表的資料工作簿(IChartDataWorkbook),先清除預設的系列與類別,接著加入自訂資料,藉此程式化地刷新圖表以呈現最新資料。
是否可以自訂圖表的外觀?
可以,Aspose.Slides for .NET 提供豐富的客製化選項,您可以修改顏色、字型、標籤、圖例及其他格式元素,以符合特定的設計需求。