在 Android 上格式化簡報圖表
概述
本文說明如何透過 Aspose.Slides 在 PowerPoint 簡報中設定圖表格式。它展示了如何自訂圖表的關鍵元素,如座標軸、格線、標題、圖例、繪圖區域以及牆面填色,以提升圖表資料的外觀與可讀性。它同時示範了如何設定圖表文字的字型屬性、套用預設數字格式至圖表資料,以及為圖表區域啟用圓角。結合這些範例,可說明如何同時控制簡報中圖表的視覺樣式與資料呈現方式。
格式化圖表實體
使用IChart 來格式化圖表的標題、座標軸、格線、圖例與繪圖區域。此範例建立一個帶有標記與預設資料的折線圖,然後對其值軸與類別軸套用不同的文字與線條樣式。字型大小與線寬以點為單位指定;類別標籤旋轉 45 度。
值軸使用setNumberFormat,獨立於來源儲存格。其刻度範圍為 -2 到 15,主要間隔為 2,次要間隔為 0.5。此範例同時結合百分比格式與將setDisplayUnit 設為千位,以符合資料的意義與量級。圖例因呼叫setOverlay 並傳入 false 而保持在繪圖區域外部。背景牆與地板的填色透過getBackWall 與getFloor 設定,但這些表面僅在 3D 圖表中可見。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 50, 50, 500, 400);
chart.setTitle(true);
chart.getChartTitle().addTextFrameForOverriding("");
IPortion chartTitle = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0);
chartTitle.setText("Sample Chart");
chartTitle.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
chartTitle.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
chartTitle.getPortionFormat().setFontHeight(20);
chartTitle.getPortionFormat().setFontBold(NullableBool.True);
chartTitle.getPortionFormat().setFontItalic(NullableBool.True);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().setWidth(5);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().setDashStyle(LineDashStyle.DashDot);
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED);
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().setWidth(3);
chart.getAxes().getVerticalAxis().setNumberFormatLinkedToSource(false);
chart.getAxes().getVerticalAxis().setDisplayUnit(DisplayUnitType.Thousands);
chart.getAxes().getVerticalAxis().setNumberFormat("0.0%");
chart.getAxes().getVerticalAxis().setAutomaticMajorUnit(false);
chart.getAxes().getVerticalAxis().setAutomaticMaxValue(false);
chart.getAxes().getVerticalAxis().setAutomaticMinorUnit(false);
chart.getAxes().getVerticalAxis().setAutomaticMinValue(false);
chart.getAxes().getVerticalAxis().setMaxValue(15f);
chart.getAxes().getVerticalAxis().setMinValue(-2f);
chart.getAxes().getVerticalAxis().setMinorUnit(0.5f);
chart.getAxes().getVerticalAxis().setMajorUnit(2.0f);
IChartPortionFormat valueAxisText = chart.getAxes().getVerticalAxis().getTextFormat().getPortionFormat();
valueAxisText.setFontBold(NullableBool.True);
valueAxisText.setFontHeight(16);
valueAxisText.setFontItalic(NullableBool.True);
valueAxisText.getFillFormat().setFillType(FillType.Solid);
valueAxisText.getFillFormat().getSolidFillColor().setPresetColor(PresetColor.DarkGreen);
valueAxisText.setLatinFont(new FontData("Times New Roman"));
chart.getAxes().getVerticalAxis().setTitle(true);
chart.getAxes().getVerticalAxis().getTitle().addTextFrameForOverriding("");
IPortion valueAxisTitle = chart.getAxes().getVerticalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0);
valueAxisTitle.setText("Primary Axis");
valueAxisTitle.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
valueAxisTitle.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
valueAxisTitle.getPortionFormat().setFontHeight(20);
valueAxisTitle.getPortionFormat().setFontBold(NullableBool.True);
valueAxisTitle.getPortionFormat().setFontItalic(NullableBool.True);
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 128, 0));
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().setWidth(5);
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.YELLOW);
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().setWidth(3);
IChartPortionFormat categoryAxisText = chart.getAxes().getHorizontalAxis().getTextFormat().getPortionFormat();
categoryAxisText.setFontBold(NullableBool.True);
categoryAxisText.setFontHeight(16);
categoryAxisText.setFontItalic(NullableBool.True);
categoryAxisText.getFillFormat().setFillType(FillType.Solid);
categoryAxisText.getFillFormat().getSolidFillColor().setColor(Color.BLUE);
categoryAxisText.setLatinFont(new FontData("Arial"));
chart.getAxes().getHorizontalAxis().setTitle(true);
chart.getAxes().getHorizontalAxis().getTitle().addTextFrameForOverriding("");
IPortion categoryAxisTitle = chart.getAxes().getHorizontalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0);
categoryAxisTitle.setText("Sample Category");
categoryAxisTitle.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
categoryAxisTitle.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
categoryAxisTitle.getPortionFormat().setFontHeight(20);
categoryAxisTitle.getPortionFormat().setFontBold(NullableBool.True);
categoryAxisTitle.getPortionFormat().setFontItalic(NullableBool.True);
chart.getAxes().getHorizontalAxis().setTickLabelPosition(TickLabelPositionType.Low);
chart.getAxes().getHorizontalAxis().setTickLabelRotationAngle(45);
IChartPortionFormat legendText = chart.getLegend().getTextFormat().getPortionFormat();
legendText.setFontBold(NullableBool.True);
legendText.setFontHeight(16);
legendText.setFontItalic(NullableBool.True);
legendText.getFillFormat().setFillType(FillType.Solid);
legendText.getFillFormat().getSolidFillColor().setPresetColor(PresetColor.DarkRed);
chart.getLegend().setOverlay(false);
chart.getBackWall().setThickness(1);
chart.getBackWall().getFormat().getFill().setFillType(FillType.Solid);
chart.getBackWall().getFormat().getFill().getSolidFillColor().setColor(Color.rgb(255, 165, 0));
chart.getFloor().getFormat().getFill().setFillType(FillType.Solid);
chart.getFloor().getFormat().getFill().getSolidFillColor().setColor(Color.RED);
chart.getPlotArea().getFormat().getFill().setFillType(FillType.Solid);
chart.getPlotArea().getFormat().getFill().getSolidFillColor().setPresetColor(PresetColor.LightCyan);
presentation.save("FormattedChart_out.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定圖表的字型屬性
透過Chart.getTextFormat 設定圖表文字的預設字型大小。此範例建立一個叢集直條圖,字型大小為 20 點,並使用setShowValue 為第一個系列啟用值標籤。具有自行明確字型設定的文字元素可以覆寫圖表的預設設定。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 500, 400);
chart.getTextFormat().getPortionFormat().setFontHeight(20);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
presentation.save("FontPropertiesForChart.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定數字格式
使用IChartDataCell.setPresetNumberFormat 來將內建數字格式套用至圖表工作簿儲存格。此範例建立一個具有預設儲存格資料的叢集直條圖,並將格式索引 10(0.00%)套用至每個系列的值儲存格。這會改變工作簿中值的顯示方式,而不改變其實際數字;例如,0.25 會顯示為 25.00%。座標軸與資料標籤的格式可分別設定。
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, 500, 400);
IChartSeriesCollection series = chart.getChartData().getSeries();
for (IChartSeries chartSeries : series)
{
for (IChartDataPoint dataPoint : chartSeries.getDataPoints())
{
dataPoint.getValue().getAsCell().setPresetNumberFormat((byte) 10); // 0.00%
}
}
presentation.save("PresetNumberFormat_out.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
下表列出內建的數字格式索引,以及於英文(美國)語系的代表性 Excel 格式。實際的格式字串與顯示方式可能因應用程式與語系而異,尤其在貨幣與日期方面。某些 Excel 格式亦與 OOXML 標準的預設值不同,詳情請參閱Microsoft 的數字格式實作說明。
| 索引 | 格式 |
|---|---|
| 0 | General |
| 1 | 0 |
| 2 | 0.00 |
| 3 | #,##0 |
| 4 | #,##0.00 |
| 5 | "$"#,##0_);("$"#,##0) |
| 6 | "$"#,##0_);[Red]("$"#,##0) |
| 7 | "$"#,##0.00_);("$"#,##0.00) |
| 8 | "$"#,##0.00_);[Red]("$"#,##0.00) |
| 9 | 0% |
| 10 | 0.00% |
| 11 | 0.00E+00 |
| 12 | # ?/? |
| 13 | # ??/?? |
| 14 | m/d/yyyy |
| 15 | d-mmm-yy |
| 16 | d-mmm |
| 17 | mmm-yy |
| 18 | h:mm AM/PM |
| 19 | h:mm:ss AM/PM |
| 20 | h:mm |
| 21 | h:mm:ss |
| 22 | m/d/yyyy h:mm |
| 37 | #,##0_);(#,##0) |
| 38 | #,##0_);[Red](#,##0) |
| 39 | #,##0.00_);(#,##0.00) |
| 40 | #,##0.00_);[Red](#,##0.00) |
| 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
| 42 | _("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_) |
| 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
| 44 | _("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_) |
| 45 | mm:ss |
| 46 | [h]:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+0 |
| 49 | @ |
設定圖表區域圓角邊框
呼叫IChart.setRoundedCorners 並傳入 true,即可為圖表區域的角落設定圓角。此範例建立一個叢集直條圖,並使用getLineFormat 為其輪廓設定實心灰色及單線樣式,然後儲存簡報。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 100, 600, 400);
chart.getLineFormat().getFillFormat().setFillType(FillType.Solid);
chart.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
chart.getLineFormat().setStyle(LineStyle.Single);
chart.setRoundedCorners(true);
presentation.save("out.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
編輯現有圖表註解
圖表可以包含自訂形狀,例如說明報告狀態的文字註解。可透過IChart.getUserShapes 取得這些形狀,該方法也在 Chart API 參考文件的Chart.getUserShapes 中列出。getShapes 回傳的集合包含圖表擁有的形狀。
放置於圖表之上的普通文字方塊屬於投影片,即使看起來像是圖表的一部分。遍歷Slide.getShapes 可找到圖表以及普通投影片形狀,但不會列舉圖表內部的個別形狀。圖表標題與資料標籤是獨立的圖表元素,並非此集合中的自訂註解。
下載chart-annotations.pptx 並置於應用程式的工作目錄中。其第一張投影片包含一個圖表,圖表上有未群組的註解「Preliminary results」、一條圖表擁有的線條,以及一個普通投影片文字方塊「Slide overlay」。範例會在該投影片上找到第一個圖表,並在報告準備發佈時將註解改為「Final results」。程式會檢查集合是否為空,僅編輯由getTextFrame 取得且非 null 的文字框的IAutoShape,且文字符合預期。
import com.aspose.slides.*;
Presentation presentation = new Presentation("chart-annotations.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = null;
for (IShape shape : slide.getShapes()) {
if (shape instanceof IChart) {
chart = (IChart) shape;
break;
}
}
if (chart == null || chart.getUserShapes().getShapes().size() == 0) {
System.out.println("No chart or chart-owned shapes found.");
return;
}
boolean updated = false;
for (IShape shape : chart.getUserShapes().getShapes()) {
if (shape instanceof IAutoShape) {
IAutoShape annotation = (IAutoShape) shape;
if (annotation.getTextFrame() != null && "Preliminary results".equals(annotation.getTextFrame().getText())) {
annotation.getTextFrame().setText("Final results");
updated = true;
break;
}
}
}
if (updated) {
presentation.save("chart-annotations-updated.pptx", SaveFormat.Pptx);
} else {
System.out.println("No matching text annotation found.");
}
} finally {
presentation.dispose();
}
線條與投影片文字方塊保持不變。以下影像顯示輸入前後重新開啟後的結果:
| 編輯前 | 編輯後 |
|---|---|
![]() |
![]() |
Note
此範例僅檢查頂層的圖表擁有形狀。若註解位於IGroupShape 之中,亦需檢查該群組的getShapes 集合。對於巢狀群組,遞迴執行相同的文字檢查於其子形狀。群組本身僅是容器,請勿將其當作文字方塊處理。常見問題
我可以為柱狀/區域設定半透明的填色,同時保持邊框不透明嗎?
可以。填色的透明度與輪廓分別設定。這在密集視覺化中提升格線與資料的可讀性時非常有用。
當資料標籤重疊時,我該如何處理?
可縮小字型、關閉非必要的標籤元件(例如類別)、調整標籤位置、必要時僅顯示選取點的標籤,或改用「值 + 圖例」的格式。
我可以對系列套用漸層或圖案填色嗎?
可以。系列支援實心、漸層與圖案填色。實務上,應節制使用漸層,且避免與格線或文字的對比度降低的組合。

