使用 JavaScript 在簡報中管理圖表資料標籤
簡介
資料標籤顯示圖表系列與單一資料點的資訊,協助讀者辨識數值並了解圖表。本篇文章說明如何格式化數值、顯示百分比、讀取標籤文字、控制超出軸上限的標籤、調整類別軸標籤間距,以及設定圓餅圖標籤的位置。
在圖表資料標籤中設定資料精度
使用 setNumberFormatOfValues 來格式化系列數值。此範例建立一個使用預設資料的折線圖,顯示其資料表格,並為第一個系列啟用數值標籤。格式 #,##0.00 會顯示千位分隔符與兩位小數,且不會變更底層的數值。
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.Line, 50, 50, 450, 300);
chart.setDataTable(true);
const series = chart.getChartData().getSeries().get_Item(0);
series.setNumberFormatOfValues("#,##0.00");
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
presentation.save("PrecisionOfDatalabels_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
以百分比作為標籤顯示
對於堆疊直條圖,將每個數值計算為其類別總和的百分比,並將文字指派給由 getTextFrameForOverriding 回傳的文字框。本範例使用預設圖表資料,並以 8 點字型顯示保留兩位小數的百分比。若類別總和為零,則會略過以避免除以零。若圖表資料變更,請重新計算自訂標籤文字。
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.StackedColumn, 20, 20, 400, 400);
const categoryTotals = new Array(chart.getChartData().getCategories().size()).fill(0);
for (let k = 0; k < chart.getChartData().getCategories().size(); k++) {
for (let i = 0; i < chart.getChartData().getSeries().size(); i++) {
const series = chart.getChartData().getSeries().get_Item(i);
const pointValue = series.getDataPoints().get_Item(k).getValue().getData();
categoryTotals[k] += Number(pointValue);
}
}
for (let x = 0; x < chart.getChartData().getSeries().size(); x++) {
const series = chart.getChartData().getSeries().get_Item(x);
series.getLabels().getDefaultDataLabelFormat().setShowLegendKey(false);
for (let j = 0; j < series.getDataPoints().size(); j++) {
const label = series.getDataPoints().get_Item(j).getLabel();
if (categoryTotals[j] == 0) {
continue;
}
const pointValue = series.getDataPoints().get_Item(j).getValue().getData();
const dataPointPercent = (Number(pointValue) / categoryTotals[j]) * 100;
const portion = new aspose.slides.Portion();
portion.setText(dataPointPercent.toFixed(2) + " %");
portion.getPortionFormat().setFontHeight(8);
label.getTextFrameForOverriding().setText("");
const paragraph = label.getTextFrameForOverriding().getParagraphs().get_Item(0);
paragraph.getPortions().add(portion);
label.getDataLabelFormat().setShowValue(true);
label.getDataLabelFormat().setShowSeriesName(false);
label.getDataLabelFormat().setShowPercentage(false);
label.getDataLabelFormat().setShowLegendKey(false);
label.getDataLabelFormat().setShowCategoryName(false);
label.getDataLabelFormat().setShowBubbleSize(false);
}
}
presentation.save("DisplayPercentageAsLabels_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
在圖表資料標籤中設定百分比符號
當數值以分數形式存儲時,使用 setNumberFormat 以顯示百分比。將 false 傳遞給 setNumberFormatLinkedToSource 以使標籤格式獨立於來源儲存格。
此範例建立一個 100% 堆疊直條圖,包含紅色與藍色系列,跨四個類別。每對數值相加為 1。標籤格式 0.0% 會將 0.30 顯示為 30.0%,而垂直軸使用兩位小數。兩個系列的標籤文字皆為白色、10 點大小。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.PercentsStackedColumn, 20, 20, 500, 400);
chart.getAxes().getVerticalAxis().setNumberFormatLinkedToSource(false);
chart.getAxes().getVerticalAxis().setNumberFormat("0.00%");
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
const workbook = chart.getChartData().getChartDataWorkbook();
const worksheetIndex = 0;
for (let i = 0; i < 4; i++) {
const categoryCell = workbook.getCell(worksheetIndex, i + 1, 0, "Category " + (i + 1));
chart.getChartData().getCategories().add(categoryCell);
}
const seriesNames = ["Reds", "Blues"];
const white = java.getStaticFieldValue("java.awt.Color", "WHITE");
const seriesColors = [java.getStaticFieldValue("java.awt.Color", "RED"), java.getStaticFieldValue("java.awt.Color", "BLUE")];
const values = [[0.30, 0.50, 0.80, 0.65], [0.70, 0.50, 0.20, 0.35]];
for (let i = 0; i < seriesNames.length; i++) {
const seriesCell = workbook.getCell(worksheetIndex, 0, i + 1, seriesNames[i]);
const series = chart.getChartData().getSeries().add(seriesCell, chart.getType());
for (let j = 0; j < 4; j++) {
const valueCell = workbook.getCell(worksheetIndex, j + 1, i + 1, values[i][j]);
series.getDataPoints().addDataPointForBarSeries(valueCell);
}
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(seriesColors[i]);
const labelFormat = series.getLabels().getDefaultDataLabelFormat();
labelFormat.setShowValue(true);
labelFormat.setNumberFormatLinkedToSource(false);
labelFormat.setNumberFormat("0.0%");
labelFormat.getTextFormat().getPortionFormat().setFontHeight(10);
labelFormat.getTextFormat().getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
labelFormat.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(white);
}
presentation.save("SetDataLabelsPercentageSign_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
讀取資料標籤的實際文字
使用 getActualLabelText 來取得資料標籤設定所產生的文字。當需要為報告擷取標籤、搜尋簡報內容或驗證產生的圖表時,此功能相當有用。在下方範例中,預設的 data label format 會結合每個類別名稱、系列名稱與數值。一個資料點將其數值格式化為百分比,另一個則使用來自 getTextFrameForOverriding 的自訂文字。
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
const workbook = chart.getChartData().getChartDataWorkbook();
const firstCategoryCell = workbook.getCell(0, 1, 0, "Q1");
chart.getChartData().getCategories().add(firstCategoryCell);
const secondCategoryCell = workbook.getCell(0, 2, 0, "Q2");
chart.getChartData().getCategories().add(secondCategoryCell);
const northSeriesCell = workbook.getCell(0, 0, 1, "North");
const north = chart.getChartData().getSeries().add(northSeriesCell, chart.getType());
const northFirstValueCell = workbook.getCell(0, 1, 1, 0.25);
north.getDataPoints().addDataPointForBarSeries(northFirstValueCell);
const northSecondValueCell = workbook.getCell(0, 2, 1, 0.75);
north.getDataPoints().addDataPointForBarSeries(northSecondValueCell);
const southSeriesCell = workbook.getCell(0, 0, 2, "South");
const south = chart.getChartData().getSeries().add(southSeriesCell, chart.getType());
const southFirstValueCell = workbook.getCell(0, 1, 2, 0.40);
south.getDataPoints().addDataPointForBarSeries(southFirstValueCell);
const southSecondValueCell = workbook.getCell(0, 2, 2, 0.60);
south.getDataPoints().addDataPointForBarSeries(southSecondValueCell);
for (let i = 0; i < chart.getChartData().getSeries().size(); i++) {
const series = chart.getChartData().getSeries().get_Item(i);
const format = series.getLabels().getDefaultDataLabelFormat();
format.setShowCategoryName(true);
format.setShowSeriesName(true);
format.setShowValue(true);
}
north.getLabels().get_Item(1).getDataLabelFormat().setNumberFormatLinkedToSource(false);
north.getLabels().get_Item(1).getDataLabelFormat().setNumberFormat("0%");
south.getLabels().get_Item(0).getTextFrameForOverriding().setText("Reviewed");
for (let i = 0; i < chart.getChartData().getSeries().size(); i++) {
const series = chart.getChartData().getSeries().get_Item(i);
for (let j = 0; j < series.getDataPoints().size(); j++) {
const point = series.getDataPoints().get_Item(j);
const label = point.getLabel();
if (!label.isVisible()) {
continue;
}
console.log("Value: " + point.getValue().getData() + "; label: " + label.getActualLabelText());
}
}
} finally {
presentation.dispose();
}
資料點中儲存的數值仍為 0.75,即使其標籤顯示 75% 並附帶類別與系列名稱。自訂文字會取代產生的標籤文字。無論哪種情況,getActualLabelText 都會回傳最終的標籤字串。若只想擷取可見的標籤,請如前所示分別檢查 isVisible。
控制超出軸上限的資料標籤
當手動限制軸範圍時,某些資料點可能會超過其上限。使用 setShowDataLabelsOverMaximum 來控制是否顯示其資料標籤。此設定僅變更標籤的可見性,並不會改變軸範圍或底層資料值。
下列範例建立一個 2D 群組直條圖,數值為 60 與 120。它對垂直軸呼叫 setAutomaticMaxValue 並傳入 false,再以 setMaxValue 設定上限為 100。第一張投影片允許顯示超過上限的標籤;其副本則停用此功能。兩張投影片皆儲存為 DataLabelsOverMaximum.pptx。
使用 setShowValue 以啟用數值標籤。圖表層級的設定不會自行啟用數值顯示,也不會覆寫個別標籤已停用的數值顯示。此範例為整個系列啟用數值,並使用 setPosition 將標籤放置在每個柱狀的外側端點。
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.setLegend(false);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
const workbook = chart.getChartData().getChartDataWorkbook();
const firstCategory = workbook.getCell(0, 1, 0, "Within range");
const secondCategory = workbook.getCell(0, 2, 0, "Above maximum");
chart.getChartData().getCategories().add(firstCategory);
chart.getChartData().getCategories().add(secondCategory);
const seriesName = workbook.getCell(0, 0, 1, "Values");
const series = chart.getChartData().getSeries().add(seriesName, chart.getType());
const firstValue = workbook.getCell(0, 1, 1, 60);
const secondValue = workbook.getCell(0, 2, 1, 120);
series.getDataPoints().addDataPointForBarSeries(firstValue);
series.getDataPoints().addDataPointForBarSeries(secondValue);
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
series.getLabels().getDefaultDataLabelFormat().setPosition(aspose.slides.LegendDataLabelPosition.OutsideEnd);
chart.getAxes().getVerticalAxis().setAutomaticMaxValue(false);
chart.getAxes().getVerticalAxis().setMaxValue(100);
chart.setShowDataLabelsOverMaximum(true);
const secondSlide = presentation.getSlides().addClone(slide);
const secondChart = secondSlide.getShapes().get_Item(0);
secondChart.setShowDataLabelsOverMaximum(false);
presentation.save("DataLabelsOverMaximum.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
以下圖片顯示由 Microsoft PowerPoint 轉譯的儲存投影片。設定為 true 時,標籤 120 會在上限處可見;設定為 false 時,則會隱藏。標籤 60 仍保持可見,軸上限維持在 100,且第二個資料點在兩種情況下皆為 120。
| setShowDataLabelsOverMaximum(true) | setShowDataLabelsOverMaximum(false) |
|---|---|
![]() |
![]() |
Chart Type
此範例使用具有數值軸的 2D 直條圖。沒有數值軸的圖表,例如圓餅圖和環形圖,無法以此方式限制軸上限。設定標籤與軸的距離
使用 setLabelOffset 來控制類別軸標籤與軸之間的距離。該數值為軸標籤最大字型大小的百分比。此範例建立一個群組直條圖,並將水平軸標籤偏移設定為 500。此設定影響類別軸標籤,而非附加於單一資料點的標籤。
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.getAxes().getHorizontalAxis().setLabelOffset(500);
presentation.save("SetCategoryAxisLabelDistance_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
調整標籤位置
在圓餅圖上,調整資料標籤位置以改善間距並為指示線騰出空間。
此範例顯示第一個資料點的數值,將其標籤放置於切片外側,並使用 setX 與 setY 調整水平與垂直偏移。這些偏移分別相對於圖表的寬度與高度。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const chart = slide.getShapes().addChart(aspose.slides.ChartType.Pie, 50, 50, 200, 200);
const series = chart.getChartData().getSeries();
const label = series.get_Item(0).getLabels().get_Item(0);
label.getDataLabelFormat().setShowValue(true);
label.getDataLabelFormat().setPosition(aspose.slides.LegendDataLabelPosition.OutsideEnd);
label.setX(java.newFloat(0.71));
label.setY(java.newFloat(0.04));
presentation.save("presentation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}

常見問題
如何防止在密集圖表上資料標籤重疊?
結合自動標籤放置、指示線與縮小字型大小;如有需要,可隱藏某些欄位(例如類別),或僅為極端值或關鍵點顯示標籤。
如何僅對零、負值或空值停用標籤?
在啟用標籤之前先過濾資料點,並根據定義的規則對值為 0、負值或缺失值的項目關閉顯示。
如何在匯出為 PDF/圖片時確保標籤樣式一致?
明確設定字型族與大小,並確認該字型在渲染環境中可用,以避免備用字型。

