在 Java 中為簡報優化圖表計算
概述
Aspose.Slides 提供用於在簡報中處理圖表計算和版面配置資料的 API。本文章說明如何取得圖表元件的實際值,包括實作 IActualLayout 的元件的真實位置與尺寸,以及圖表座標軸的實際值。它還解釋這些值會在圖表版面配置驗證之後填入。
此外,本文示範如何取得父圖表元件的實際位置,以及如何隱藏圖表元件,例如標題、座標軸、圖例和格線。結合這些範例,可協助您以程式方式檢查圖表版面資訊,並控制 PowerPoint 簡報中圖表元件的可見性。
計算圖表元件的實際值
Aspose.Slides for Java 提供簡單的 API 以取得這些屬性。IAxis 介面的屬性提供有關座標軸圖表元件實際位置的資訊(IAxis.getActualMaxValue、IAxis.getActualMinValue、IAxis.getActualMajorUnit、IAxis.getActualMinorUnit、IAxis.getActualMajorUnitScale、IAxis.getActualMinorUnitScale)。必須先呼叫方法 IChart.validateChartLayout() 以在屬性中填入實際值。
Presentation pres = new Presentation();
try {
Chart chart = (Chart)pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 100, 100, 500, 350);
chart.validateChartLayout();
double maxValue = chart.getAxes().getVerticalAxis().getActualMaxValue();
double minValue = chart.getAxes().getVerticalAxis().getActualMinValue();
double majorUnit = chart.getAxes().getHorizontalAxis().getActualMajorUnit();
double minorUnit = chart.getAxes().getHorizontalAxis().getActualMinorUnit();
} finally {
if (pres != null) pres.dispose();
}
計算父圖表元件的實際位置
Aspose.Slides for Java 提供簡單的 API 以取得這些屬性。IActualLayout 介面的屬性提供有關父圖表元件實際位置的資訊(IActualLayout.getActualX、IActualLayout.getActualY、IActualLayout.getActualWidth、IActualLayout.getActualHeight)。必須先呼叫方法 IChart.validateChartLayout() 以在屬性中填入實際值。
Presentation pres = new Presentation();
try {
Chart chart = (Chart) pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 500, 350);
chart.validateChartLayout();
double x = chart.getPlotArea().getActualX();
double y = chart.getPlotArea().getActualY();
double w = chart.getPlotArea().getActualWidth();
double h = chart.getPlotArea().getActualHeight();
} finally {
if (pres != null) pres.dispose();
}
隱藏圖表元件
本主題說明如何隱藏圖表中的資訊。使用 Aspose.Slides for Java,您可以隱藏圖表的標題、垂直座標軸、水平座標軸與格線。以下程式碼範例示範如何使用這些屬性。
Presentation pres = new Presentation();
try {
ISlide slide = pres.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 140, 118, 320, 370);
//隱藏圖表標題
chart.setTitle(false);
///隱藏數值軸
chart.getAxes().getVerticalAxis().setVisible(false);
//類別軸可見性
chart.getAxes().getHorizontalAxis().setVisible(false);
//隱藏圖例
chart.setLegend(false);
//隱藏主要格線
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
for (int i = 0; i < chart.getChartData().getSeries().size(); i++)
{
chart.getChartData().getSeries().removeAt(i);
}
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
series.getMarker().setSymbol(MarkerStyleType.Circle);
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
series.getLabels().getDefaultDataLabelFormat().setPosition(LegendDataLabelPosition.Top);
series.getMarker().setSize(15);
//設定系列線條顏色
series.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
series.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.MAGENTA);
series.getFormat().getLine().setDashStyle(LineDashStyle.Solid);
pres.save("HideInformationFromChart.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
FAQ
外部 Excel 活頁簿可以作為資料來源嗎?其對重新計算有何影響?
是。圖表可以參照外部活頁簿:當您連接或重新整理外部來源時,公式和數值會從該活頁簿取得,圖表會在開啟/編輯操作期間反映更新。API 允許您指定外部活頁簿 路徑並管理連結的資料。
我可以在不自行實作迴歸的情況下計算並顯示趨勢線嗎?
是。趨勢線(線性、指數等)由 Aspose.Slides 加入並自動更新;其參數會根據系列資料重新計算,您無需自行實作計算。
如果簡報中有多個帶有外部連結的圖表,我可以控制每個圖表使用哪個活頁簿來計算值嗎?
是。每個圖表都可以指向自己的外部活頁簿,或您可為每個圖表獨立建立/替換外部活頁簿,而不受其他圖表影響。