使用 JavaScript 在簡報中自訂圓餅圖
Contents
[
Hide
]
概述
本文說明如何在 Aspose.Slides 中使用圓餅圖。它展示了如何為「Pie of Pie」和「Bar of Pie」圖表設定次要圖表選項,以及如何為標準圓餅圖啟用自動切片著色。
範例聚焦於實務的圖表自訂步驟,例如在投影片中加入圖表、調整系列與標籤設定、以自訂的類別與數值取代預設圖表資料,並儲存更新後的簡報。
Pie of Pie 與 Bar of Pie 圖表的次要圖表選項
Aspose.Slides for Node.js via Java 現已支援 Pie of Pie 或 Bar of Pie 圖表的次要圖表選項。在本主題中,我們將示範如何使用 Aspose.Slides 來指定這些選項。設定屬性請按以下步驟操作:
- 實例化 Presentation 類別物件。
- 在投影片上加入圖表。
- 指定圖表的次要圖表選項。
- 將簡報寫入磁碟。
以下範例中,我們已設定 Pie of Pie 圖表的不同屬性。
// 建立 Presentation 類別的實例
var pres = new aspose.slides.Presentation();
try {
// 在投影片上新增圖表
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.PieOfPie, 50, 50, 500, 400);
// 設定不同的屬性
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
chart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setSecondPieSize(149);
chart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setPieSplitBy(aspose.slides.PieSplitType.ByPercentage);
chart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setPieSplitPosition(53);
// 將簡報寫入磁碟
pres.save("SecondPlotOptionsforCharts_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
設定自動圓餅圖切片顏色
Aspose.Slides for Node.js via Java 提供簡易的 API 以設定自動圓餅圖切片顏色。範例程式碼套用了上述屬性設定。
- 建立 Presentation 類別的實例。
- 存取第一張投影片。
- 使用預設資料加入圖表。
- 設定圖表標題。
- 將第一個系列設定為顯示數值。
- 設定圖表資料工作表的索引。
- 取得圖表資料工作表。
- 刪除預設產生的系列與類別。
- 新增類別。
- 新增系列。
將修改後的簡報寫入 PPTX 檔案。
// 建立 Presentation 類別的實例
var pres = new aspose.slides.Presentation();
try {
// 新增帶有預設資料的圖表
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Pie, 100, 100, 400, 400);
// 設定圖表標題
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(aspose.slides.NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
// 設定第一個系列以顯示數值
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
// 設定圖表資料工作表的索引
var defaultWorksheetIndex = 0;
// 取得圖表資料工作表
var fact = chart.getChartData().getChartDataWorkbook();
// 刪除預設產生的系列與類別
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
// 新增類別
chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "First Qtr"));
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "2nd Qtr"));
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "3rd Qtr"));
// 新增系列
var series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType());
// 立即填入系列資料
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
series.getParentSeriesGroup().setColorVaried(true);
pres.save("Pie.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
常見問題
是否支援「Pie of Pie」和「Bar of Pie」變體?
是的,該函式庫支援 圓餅圖的次要圖表,包括「Pie of Pie」與「Bar of Pie」類型。
我可以只將圖表匯出為影像(例如 PNG)嗎?
是的,您可以將圖表本身匯出為影像(例如 PNG),而無需匯出整個簡報。