在 Android 上管理簡報中的圖表資料系列

概觀

圖表可以將其繪製的值儲存在工作簿儲存格中,或直接作為數值常數。 IChartSeries 代表一組相關的值,每個 IChartDataPoint 代表該系列中的一個點。 IChartCategory 物件提供系列共用的標籤或分組值。在使用預設資料建立的圖表中,系列名稱、類別和點值使用來自圖表工作簿的 IChartDataCell 物件。

對於一般的類別圖表,預設工作簿使用第 0 列儲存系列名稱,第 0 欄儲存類別名稱,其餘儲存格則存放系列值。傳遞給 IChartDataWorkbook.getCell 的工作表、列與欄索引皆為零基礎。此佈局在您建立具有預設資料的圖表時相當有用,但請勿假設所有現有圖表皆採用此方式。對於已載入的簡報,請在變更工作簿值之前,先檢查系列、類別與資料點所參照的儲存格。

圖表設定有三種不同的範圍:

當未設定明確的點或系列填充時,圖表樣式與佈景主題會決定自動外觀。當同時存在系列與點的格式設定時,點的格式設定會在該點上優先。

chart-series-powerpoint

使用數值常數設定系列值

當系列值來自應用程式資料或計算且不需要工作表儲存格參照時,請使用數值常數。於指派每個點的 getValue 結果之前,先以 setDataSourceTypeForValues 並傳入 DataSourceType.DoubleLiterals 設定類型,然後使用 setAsLiteralDouble 指派數值。

此集合設定會控制該系列中所有點值的來源類型。每個點值的 getDataSourceType 方法會回報所選類型;可透過集合設定變更。若使用 Worksheet,請以 getAsCell 取得參照的儲存格。若使用 DoubleLiterals,則以 getAsLiteralDouble 或 setAsLiteralDouble 直接讀寫數字。

以下範例建立一個預設系列與類別的柱狀圖,然後將計算得到的 10、20 等值指派給其第一系列的現有點。範例會儲存簡報、重新開啟,並列印儲存的值。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
    IChartSeries series = chart.getChartData().getSeries().get_Item(0);

    series.getDataPoints().setDataSourceTypeForValues(DataSourceType.DoubleLiterals);
    for (int i = 0; i < series.getDataPoints().size(); i++) {
        series.getDataPoints().get_Item(i).getValue().setAsLiteralDouble((i + 1) * 10);
    }

    presentation.save("numeric_literals.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Presentation reopenedPresentation = new Presentation("numeric_literals.pptx");
try {
    ISlide reopenedSlide = reopenedPresentation.getSlides().get_Item(0);
    IChart reopenedChart = (IChart) reopenedSlide.getShapes().get_Item(0);
    IChartSeries reopenedSeries = reopenedChart.getChartData().getSeries().get_Item(0);
    for (IChartDataPoint point : reopenedSeries.getDataPoints()) {
        System.out.println(point.getValue().getAsLiteralDouble());
    }
} finally {
    reopenedPresentation.dispose();
}

點值來源類型與 IChartData.getDataSourceType 是分開的,後者識別圖表的內部或外部工作簿配置。使用數值常數本身不會移除嵌入式工作簿。在此範例中,工作簿仍然保留供類別標籤、系列名稱以及其他系列使用。請參閱 Chart Workbook 瞭解工作簿管理。

對於散佈圖與氣泡圖,請使用 setDataSourceTypeForXValues 與 setDataSourceTypeForYValues 分別選擇 getXValue 與 getYValue 的來源。氣泡圖亦使用 setDataSourceTypeForBubbleSizes 取得 getBubbleSize 的來源。於指派數字前,先將每個相關來源設定為 DoubleLiterals,再透過該值的 setAsLiteralDouble 方法指派。

設定圖表系列的重疊度

IChartSeries.getOverlap 會回報 2D 圖表中長條或柱狀的重疊程度,範圍為 -100 至 100 百分比。它是父系列群組設定的唯讀投影。請使用 IChartSeriesGroup.setOverlap 來更新該群組中所有相容的系列。此選項適用於顯示群組長條或柱狀的圖表類型;對組合圖中不相關的系列群組不會產生影響。

以下範例將第一系列所在群組的重疊度設為指定值:

import com.aspose.slides.*;

final int firstSlideIndex = 0;
final int firstSeriesIndex = 0;
final byte overlapPercent = 30;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    // 新的圖表包含範例系列、類別和數值。
    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    IChartSeries series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    series.getParentSeriesGroup().setOverlap(overlapPercent);

    presentation.save("series_overlap.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

The series overlap

變更系列填充顏色

使用 IChartSeries.getFormat 可為整個系列設定預設填充。如果某個點已明確設定填充,則其 IChartDataPoint.getFormat 會覆蓋該系列的填充。

以下範例為第一系列套用實心藍色填充:

import com.aspose.slides.*;
import android.graphics.Color;

final int firstSlideIndex = 0;
final int firstSeriesIndex = 0;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    IChartSeries series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    series.getFormat().getFill().setFillType(FillType.Solid);
    series.getFormat().getFill().getSolidFillColor().setColor(Color.BLUE);

    presentation.save("series_color.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

The color of the series

變更系列名稱

系列名稱儲存在圖表資料工作簿中,通常會顯示於圖例。對於預設建立的群組柱狀圖,儲存格 B1(第 0 列第 1 欄)即是第一系列的名稱。以下範例使用具名常數明確說明此結構:

import com.aspose.slides.*;

final int firstSlideIndex = 0;
final int worksheetIndex = 0;
final int seriesNameRowIndex = 0;
final int firstSeriesColumnIndex = 1;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
    IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, seriesNameRowIndex, firstSeriesColumnIndex);
    seriesNameCell.setValue("Revenue");

    presentation.save("series_name.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

您也可以直接更新由 IChartSeries.getName 參照的儲存格。此做法避免對現有圖表假設特定的列與欄:

import com.aspose.slides.*;

final int firstSlideIndex = 0;
final int firstSeriesIndex = 0;
final int firstNameCellIndex = 0;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    IChartSeries series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    IChartDataCell seriesNameCell = series.getName().getAsCells().get_Item(firstNameCellIndex);
    seriesNameCell.setValue("Revenue");

    presentation.save("series_name.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

The series name

從多個儲存格建立具名稱的系列

當產品名稱與報告期間分別存放於不同工作簿儲存格時,合成的系列名稱相當實用。例如,您可以將 B1 的 Product A 與 C1 的 2026 合併為單一系列名稱,同時保留兩個部分與來源儲存格的連結。

使用 IChartDataWorkbook.getCellCollection 取得名稱範圍,然後將該集合傳遞給 IChartSeriesCollection.add。skipHiddenCells 參數決定是否排除隱藏儲存格:true 為排除,false 為包含。此範例使用 false 以包含名稱範圍內的每個儲存格。

以下範例建立一個包含一個系列與兩個資料點的簡報。儲存格 B1:C1 只提供系列名稱;A2:A3 提供類別標籤;B2:B3 提供數值。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 620, 180);

    chart.getChartData().getSeries().clear();
    chart.getChartData().getCategories().clear();
    chart.setLegend(true);

    IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
    workbook.clear(0);

    // 這兩個儲存格提供系列名稱。
    workbook.getCell(0, 0, 1, "Product A");
    workbook.getCell(0, 0, 2, "2026");
    IChartCellCollection nameCells = workbook.getCellCollection("Sheet1!$B$1:$C$1", false);
    IChartSeries series = chart.getChartData().getSeries().add(nameCells, ChartType.ClusteredColumn);

    // 分別的儲存格提供類別和數值資料點。
    IChartDataCell northCategory = workbook.getCell(0, 1, 0, "North");
    IChartDataCell southCategory = workbook.getCell(0, 2, 0, "South");
    chart.getChartData().getCategories().add(northCategory);
    chart.getChartData().getCategories().add(southCategory);
    IChartDataCell northValue = workbook.getCell(0, 1, 1, 120);
    IChartDataCell southValue = workbook.getCell(0, 2, 1, 150);
    series.getDataPoints().addDataPointForBarSeries(northValue);
    series.getDataPoints().addDataPointForBarSeries(southValue);

    presentation.save("composite_series_name.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

最終的系列名稱為 Product A 2026(兩個儲存格值之間有空格)。圖例會將其視為單一條目。下圖示範結果:

Column chart with North and South values and the composite series name Product A 2026 in the legend

取得自動系列填充顏色

IChartSeries.getAutomaticSeriesColor 會回傳以 Android ARGB 整數表示,根據系列索引與圖表樣式計算出的顏色。這是系列填充未明確定義時所使用的顏色。呼叫此方法僅會讀取計算出的顏色,並不會指派新的填充。

以下範例列印每個預設系列的自動顏色整數:

import com.aspose.slides.*;

final int firstSlideIndex = 0;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    int seriesCount = chart.getChartData().getSeries().size();
    for (int seriesIndex = 0; seriesIndex < seriesCount; seriesIndex++) {
        IChartSeries series = chart.getChartData().getSeries().get_Item(seriesIndex);
        int automaticColor = series.getAutomaticSeriesColor();
        System.out.println("Series " + seriesIndex + ": " + automaticColor);
    }
} finally {
    presentation.dispose();
}

具體的整數值會依圖表樣式與佈景主題而異。

為圖表系列設定反轉填充顏色

對於長條、柱狀與氣泡系列,IChartSeries.setInvertIfNegative 可以在負值時以不同的填充顯示。請先將常規系列填充設為實心,啟用反轉,並透過 IChartSeries.getInvertedSolidFillColor 取得負值顏色。負數在工作簿中仍保持不變,僅改變顯示顏色。

以下範例以一個系列取代預設圖表資料。工作表第 0 列儲存系列名稱,第 0 欄儲存類別名稱,第 1 欄儲存數值:

import com.aspose.slides.*;
import android.graphics.Color;

final int firstSlideIndex = 0;
final int worksheetIndex = 0;
final int headerRowIndex = 0;
final int categoryColumnIndex = 0;
final int firstSeriesColumnIndex = 1;
final int firstDataRowIndex = 1;

String[] categoryNames = { "Category 1", "Category 2", "Category 3" };
int[] seriesValues = { -20, 50, -30 };

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);
    IChartData chartData = chart.getChartData();
    IChartDataWorkbook workbook = chartData.getChartDataWorkbook();

    chartData.getSeries().clear();
    chartData.getCategories().clear();

    IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, headerRowIndex, firstSeriesColumnIndex, "Series 1");
    int chartType = chart.getType();
    IChartSeries series = chartData.getSeries().add(seriesNameCell, chartType);

    for (int categoryIndex = 0; categoryIndex < categoryNames.length; categoryIndex++) {
        int dataRowIndex = firstDataRowIndex + categoryIndex;
        String categoryName = categoryNames[categoryIndex];
        int seriesValue = seriesValues[categoryIndex];

        IChartDataCell categoryCell = workbook.getCell(worksheetIndex, dataRowIndex, categoryColumnIndex, categoryName);
        chartData.getCategories().add(categoryCell);

        IChartDataCell valueCell = workbook.getCell(worksheetIndex, dataRowIndex, firstSeriesColumnIndex, seriesValue);
        series.getDataPoints().addDataPointForBarSeries(valueCell);
    }

    int automaticSeriesColor = series.getAutomaticSeriesColor();
    series.getFormat().getFill().setFillType(FillType.Solid);
    series.getFormat().getFill().getSolidFillColor().setColor(automaticSeriesColor);
    series.setInvertIfNegative(true);
    series.getInvertedSolidFillColor().setColor(Color.RED);

    presentation.save("inverted_solid_fill_color.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

The inverted solid fill color

您也可以針對單一點啟用反轉,方式是呼叫 IChartDataPoint.setInvertIfNegative。以下範例在系列上停用反轉,僅為選取的點啟用,同時將該點指派為負值以便觀察效果:

import com.aspose.slides.*;
import android.graphics.Color;

final int firstSlideIndex = 0;
final int firstSeriesIndex = 0;
final int targetDataPointIndex = 2;
final int negativeValue = -30;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    IChartSeries series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    int automaticSeriesColor = series.getAutomaticSeriesColor();
    series.getFormat().getFill().setFillType(FillType.Solid);
    series.getFormat().getFill().getSolidFillColor().setColor(automaticSeriesColor);
    series.getInvertedSolidFillColor().setColor(Color.RED);
    series.setInvertIfNegative(false);

    IChartDataPoint dataPoint = series.getDataPoints().get_Item(targetDataPointIndex);
    dataPoint.getValue().getAsCell().setValue(negativeValue);
    dataPoint.setInvertIfNegative(true);

    presentation.save("data_point_invert_color_if_negative.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

清除特定資料點的值

若要讓單一點變為空白而不移除其他點,請將其對應的工作簿儲存格設為 null。對於柱狀圖,可透過 IChartDataPoint.getValue 取得繪製值。資料點仍保留在相同的類別位置,但圖表會依照空白值設定將其視為空白。

以下範例僅清除第一系列的第二個點:

import com.aspose.slides.*;

final int firstSlideIndex = 0;
final int firstSeriesIndex = 0;
final int targetDataPointIndex = 1;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 200);

    IChartSeries series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    IChartDataPoint dataPoint = series.getDataPoints().get_Item(targetDataPointIndex);
    dataPoint.getValue().getAsCell().setValue(null);

    presentation.save("clear_data_point_value.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

散佈圖使用分別的 X 與 Y 儲存格,氣泡圖亦使用大小儲存格。僅清除代表欲移除值的儲存格。若想保留其他點,請勿呼叫 IChartDataPointCollection.clear,因為該方法會移除集合中的所有資料點。

控制空白儲存格的顯示方式

隱藏且包含值的儲存格與空白儲存格是不同的情況。若要包含或排除隱藏工作表列與欄的資料,請參閱 Include Data from Hidden Rows and Columns。

空白工作簿儲存格代表缺失資料;儲存格內含 0 則代表已知的數值。使用 IChartDataCell.setValue 並傳入 null 即可將儲存格設為空白。數值零不會受到空白儲存格設定的影響。

使用 IChart.setDisplayBlanksAs 可選擇圖表如何顯示空白儲存格,該設定套用於整個圖表。它會改變空白的繪製方式,而不會將空白工作簿儲存格填入零或插值。

以下自包含範例建立一個含單一系列的折線圖,清除第 3 天的值,並以每種模式分別儲存相同圖表。無需輸入檔案。IChartDataWorkbook 使用工作表 0,第 0 欄作為類別標籤,第 1 欄作為數值;第 0 列為系列名稱。最終資料為 10, 20, empty, 30, 40。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IChart chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 40, 40, 640, 400);
    IChartData chartData = chart.getChartData();
    IChartDataWorkbook workbook = chartData.getChartDataWorkbook();

    chartData.getSeries().clear();
    chartData.getCategories().clear();

    IChartDataCell seriesNameCell = workbook.getCell(0, 0, 1, "Measurements");
    IChartSeries series = chartData.getSeries().add(seriesNameCell, chart.getType());
    int[] values = { 10, 20, 25, 30, 40 };

    for (int i = 0; i < values.length; i++) {
        IChartDataCell categoryCell = workbook.getCell(0, i + 1, 0, "Day " + (i + 1));
        chartData.getCategories().add(categoryCell);
        IChartDataCell valueCell = workbook.getCell(0, i + 1, 1, values[i]);
        series.getDataPoints().addDataPointForLineSeries(valueCell);
    }

    // 讓第 3 天真正保持空白,同時保留其類別和資料點。
    workbook.getCell(0, 3, 1).setValue(null);

    int[] modes = { DisplayBlanksAsType.Gap, DisplayBlanksAsType.Zero, DisplayBlanksAsType.Span };
    String[] modeNames = { "Gap", "Zero", "Span" };
    for (int i = 0; i < modes.length; i++) {
        chart.setDisplayBlanksAs(modes[i]);
        presentation.save("empty_cells_" + modeNames[i] + ".pptx", SaveFormat.Pptx);
    }
} finally {
    presentation.dispose();
}

每個輸出檔案會儲存於儲存前指定的模式:empty_cells_Gap.pptx、empty_cells_Zero.pptx 與 empty_cells_Span.pptx。若只想產生單一版本,可在儲存簡報前指定所需模式,然後只執行一次儲存。

以下比較圖示了三個檔案中的相同資料。第 3 天在所有工作簿中皆為空白:

Line charts with identical data: Gap breaks the line at Day 3, Zero drops the line to zero, and Span connects Day 2 to Day 4.

可見效果取決於圖表類型。折線圖於三種模式皆易於比較。長條與柱狀圖因缺少可跨過缺失類別的線條,Span 無法產生上述連接段落;缺失的柱與零高的柱看起來也相似。類似地,僅有標記的散佈圖亦無連接線。請勿期望每種圖表類型都會出現三種截然不同的結果;請自行檢查使用的圖表類型之輸出。

設定系列間隙寬度

間隙寬度是相鄰長條或柱狀叢集之間的空間,以長條或柱狀寬度的百分比表示。與重疊度相同,間隙寬度屬於父系列群組,而非單一系列。請對群組呼叫一次 IChartSeriesGroup.setGapWidth。較大的數值會在叢集間產生更多空間,較小的數值則使其更緊密。

以下範例變更間隙寬度,僅儲存最終的簡報:

import com.aspose.slides.*;

final int firstSlideIndex = 0;
final int firstSeriesIndex = 0;
final int gapWidthPercent = 30;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(firstSlideIndex);

    IChart chart = slide.getShapes().addChart(ChartType.StackedColumn, 20, 20, 500, 200);

    IChartSeries series = chart.getChartData().getSeries().get_Item(firstSeriesIndex);
    series.getParentSeriesGroup().setGapWidth(gapWidthPercent);

    presentation.save("gap_width_30.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

The gap width

常見問題集

哪些圖表類型支援資料系列?

所有由 ChartType 列舉表示的圖表類型皆使用圖表資料,但其系列並非皆擁有相同的值結構或設定。例如,類別圖使用類別與值,散佈圖使用 X 與 Y 值,氣泡圖則額外加入氣泡大小。請使用與系列類型相符的資料點建立方法。重疊與間隙寬度等選項僅適用於相容的長條或柱狀群組。

什麼是圖表系列群組?

IChartSeriesGroup 包含共享群組層級繪圖設定的相容系列。組合圖可能包含多個群組,因此透過單一系列取得的群組設定不一定會影響圖表中所有系列。

新建立的圖表是否包含預設資料?

是。預設情況下,IShapeCollection.addChart 會建立示範系列、類別與值。您可以編輯這些儲存格,或在加入完全自訂的資料集之前先清除系列與類別集合。也提供不含預設資料的重載方法。

圖表物件如何與工作簿儲存格連結?

系列名稱、類別標籤與資料點值皆參照 IChartDataWorkbook 中的儲存格。變更參照的儲存格會即時更新相應的圖表元素。建立自訂資料時,請確保類別列與系列值列保持對齊,使每個點皆在預期的類別下繪製。

要如何只清除單一點而不是整個系列?

將相關的值儲存格設為 null,即可保留點的類別位置作為空白點。僅在想移除該系列所有點時,才使用 IChartDataPointCollection.clear,因為它會移除該系列的全部資料點。若同時移除類別,請同步更新所有系列,使其值仍與類別集合保持對齊。

空白點會如何顯示?

顯示結果取決於圖表類型以及透過 IChart.setDisplayBlanksAs 設定的值。支援的圖表可以將空白顯示為間隙、零值,或連接相鄰點。請選擇符合簡報中遺失資料意義的設定。完整範例與視覺比較請參閱 Control the Display of Empty Cells。

負值的格式如何設定?

對於受支援的長條、柱狀與氣泡系列,請呼叫 IChartSeries.setInvertIfNegative,並設定 IChartSeries.getInvertedSolidFillColor 回傳的顏色。您亦可透過 IChartDataPoint.setInvertIfNegative 為個別點覆寫此行為。這些方法影響的是格式,而非儲存的數值。

當系列與點同時設定格式時,哪個會優先?

明確的資料點格式會在該點上優先。其他點則繼續使用明確的系列格式,或在系列格式未定義時使用自動的圖表樣式與佈景主題。群組設定(如重疊與間隙寬度)屬於版面配置,並非點層級的格式覆寫。

圖表的系列數量有上限嗎?

Aspose.Slides 本身未設置固定的系列數量上限。實務上,簡報檔案的限制、可用記憶體、渲染時間以及圖表可讀性會決定實際可使用的上限。

當欄位過於接近或相隔過遠時,應如何調整?

請對適當的父系列群組呼叫 IChartSeriesGroup.setGapWidth。增大數值可擴大叢集間的間距,減小數值則使叢集更緊密。