在 Java 中创建或更新 PowerPoint 演示文稿图表
概述
本文提供了使用 Aspose.Slides for Java 创建和自定义图表的完整指南。您将学习如何以编程方式向幻灯片添加图表、填充数据,以及应用各种格式设置以满足特定的设计要求。文章中通过详细的代码示例展示了每一步,从初始化演示文稿和图表对象到配置系列、坐标轴和图例。遵循本指南,您将深入了解如何在 Java 应用程序中集成动态图表生成,简化基于数据的演示文稿创建过程。
创建图表
图表帮助人们快速可视化数据,并获得在表格或电子表格中不易立即发现的洞察。
为什么要创建图表?
使用图表,您可以:
- 在单个幻灯片上汇总、压缩或概括大量数据;
- 揭示数据中的模式和趋势;
- 推断数据随时间或相对于特定计量单位的方向和势头;
- 发现离群值、异常、偏差、错误以及不合理的数据;
- 传达或展示复杂数据。
在 PowerPoint 中,您可以通过 Insert 功能创建图表,该功能提供了许多图表类型的模板。使用 Aspose.Slides,您既可以创建基于常见图表类型的普通图表,也可以创建自定义图表。
创建簇状柱形图
此示例使用 addChart 创建一个簇状柱形图,比较三个类别下的两组系列。系列使用红色和绿色填充,单独的标签展示类别名称、系列名称和数值。工作表索引 0 指代嵌入式图表工作簿中的第一张工作表。
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
int worksheetIndex = 0;
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Series 1");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
seriesNameCell = workbook.getCell(worksheetIndex, 0, 2, "Series 2");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
IChartDataCell categoryCell = workbook.getCell(worksheetIndex, 1, 0, "Category 1");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 2, 0, "Category 2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 3, 0, "Category 3");
chart.getChartData().getCategories().add(categoryCell);
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
IChartDataCell valueCell = workbook.getCell(worksheetIndex, 1, 1, 20);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 1, 50);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 1, 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(Color.RED);
series = chart.getChartData().getSeries().get_Item(1);
valueCell = workbook.getCell(worksheetIndex, 1, 2, 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 2, 10);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 2, 60);
series.getDataPoints().addDataPointForBarSeries(valueCell);
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(new Color(0, 128, 0));
IDataLabel label = series.getDataPoints().get_Item(0).getLabel();
label.getDataLabelFormat().setShowCategoryName(true);
label = series.getDataPoints().get_Item(1).getLabel();
label.getDataLabelFormat().setShowSeriesName(true);
label = series.getDataPoints().get_Item(2).getLabel();
label.getDataLabelFormat().setShowValue(true);
label.getDataLabelFormat().setShowSeriesName(true);
label.getDataLabelFormat().setSeparator("/");
presentation.save("AsposeChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建散点图
散点图(也称为散点图或 x‑y 图)常用于检查模式或展示两个变量之间的相关性。
在以下情况下使用散点图:
- 您拥有成对的数值数据。
- 您有两个配对良好的变量。
- 您想确定这两个变量是否相关。
- 您有一个独立变量对应多个因变量的取值。
此示例绘制了两组 x‑y 值。第一系列使用直线加星形标记,第二系列使用平滑线加圆形标记;两者的标记大小均为 10,使用 setSize 进行设置。setSymbol 方法控制标记形状。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 20, 20, 500, 300);
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
int worksheetIndex = 0;
chart.getChartData().getSeries().clear();
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 1, 1, "Series 1");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
seriesNameCell = workbook.getCell(worksheetIndex, 1, 3, "Series 2");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
IChartDataCell xValueCell = workbook.getCell(worksheetIndex, 2, 1, 1);
IChartDataCell yValueCell = workbook.getCell(worksheetIndex, 2, 2, 3);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 3, 1, 2);
yValueCell = workbook.getCell(worksheetIndex, 3, 2, 10);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
series.setType(ChartType.ScatterWithStraightLinesAndMarkers);
series.getMarker().setSize(10);
series.getMarker().setSymbol(MarkerStyleType.Star);
series = chart.getChartData().getSeries().get_Item(1);
xValueCell = workbook.getCell(worksheetIndex, 2, 3, 5);
yValueCell = workbook.getCell(worksheetIndex, 2, 4, 2);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 3, 3, 3);
yValueCell = workbook.getCell(worksheetIndex, 3, 4, 1);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 4, 3, 2);
yValueCell = workbook.getCell(worksheetIndex, 4, 4, 2);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 5, 3, 5);
yValueCell = workbook.getCell(worksheetIndex, 5, 4, 1);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
series.getMarker().setSize(10);
series.getMarker().setSymbol(MarkerStyleType.Circle);
presentation.save("AsposeChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建饼图
饼图最适合用于显示数据的部分与整体关系,尤其是当数据包含带数值的分类标签时。然而,如果数据包含的部分或标签过多,建议考虑使用条形图。
此示例创建了一个包含三个季度值的饼图。每个切片都有自定义填充和边框,标签显示了值、百分比和系列信息的不同组合。setFirstSliceAngle 方法将第一切片的起始角度设为 180 度。
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Pie, 20, 20, 500, 300);
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
int worksheetIndex = 0;
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataCell categoryCell = workbook.getCell(0, 1, 0, "1st Qtr");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 2, 0, "2nd Qtr");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 3, 0, "3rd Qtr");
chart.getChartData().getCategories().add(categoryCell);
IChartDataCell seriesNameCell = workbook.getCell(0, 0, 1, "Series 1");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
IChartDataCell valueCell = workbook.getCell(worksheetIndex, 1, 1, 20);
series.getDataPoints().addDataPointForPieSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 1, 50);
series.getDataPoints().addDataPointForPieSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 1, 30);
series.getDataPoints().addDataPointForPieSeries(valueCell);
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true);
IChartDataPoint firstSlice = series.getDataPoints().get_Item(0);
firstSlice.getFormat().getFill().setFillType(FillType.Solid);
firstSlice.getFormat().getFill().getSolidFillColor().setColor(Color.CYAN);
firstSlice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
firstSlice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
firstSlice.getFormat().getLine().setWidth(3.0);
firstSlice.getFormat().getLine().setStyle(LineStyle.ThinThick);
firstSlice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDash);
IChartDataPoint secondSlice = series.getDataPoints().get_Item(1);
secondSlice.getFormat().getFill().setFillType(FillType.Solid);
secondSlice.getFormat().getFill().getSolidFillColor().setColor(new Color(165, 42, 42));
secondSlice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
secondSlice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
secondSlice.getFormat().getLine().setWidth(3.0);
secondSlice.getFormat().getLine().setStyle(LineStyle.Single);
secondSlice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot);
IChartDataPoint thirdSlice = series.getDataPoints().get_Item(2);
thirdSlice.getFormat().getFill().setFillType(FillType.Solid);
thirdSlice.getFormat().getFill().getSolidFillColor().setColor(new Color(255, 127, 80));
thirdSlice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
thirdSlice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED);
thirdSlice.getFormat().getLine().setWidth(2.0);
thirdSlice.getFormat().getLine().setStyle(LineStyle.ThinThin);
thirdSlice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot);
IDataLabel firstLabel = series.getDataPoints().get_Item(0).getLabel();
firstLabel.getDataLabelFormat().setShowValue(true);
IDataLabel secondLabel = series.getDataPoints().get_Item(1).getLabel();
secondLabel.getDataLabelFormat().setShowValue(true);
secondLabel.getDataLabelFormat().setShowLegendKey(true);
secondLabel.getDataLabelFormat().setShowPercentage(true);
IDataLabel thirdLabel = series.getDataPoints().get_Item(2).getLabel();
thirdLabel.getDataLabelFormat().setShowSeriesName(true);
thirdLabel.getDataLabelFormat().setShowPercentage(true);
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true);
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180);
presentation.save("PieChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

要改善饼图外部标签的可读性,请参阅 Format Leader Lines 示例,了解如何定位标签并自定义线条的颜色、宽度、透明度和虚线样式。
创建折线图
折线图(也称为折线图)最适合用于展示随时间变化的数值。使用折线图,您可以一次性比较大量数据,跟踪随时间的变化和趋势,突出数据系列中的异常等。
此示例使用 addChart 创建一个折线图,使用默认的系列和类别,然后将演示文稿保存为 PPTX。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart lineChart = slide.getShapes().addChart(ChartType.Line, 20, 20, 500, 300);
presentation.save("lineChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
默认情况下,折线图的点通过直线连续相连。如果希望点之间使用虚线相连,请调用 setDashStyle,并传入来自 LineDashStyle 的值:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart lineChart = slide.getShapes().addChart(ChartType.Line, 20, 20, 500, 300);
for (IChartSeries series : lineChart.getChartData().getSeries())
{
series.getFormat().getLine().setDashStyle(LineDashStyle.Dash);
}
}
finally
{
presentation.dispose();
}
结果:

创建树形图
树形图在展示销售数据时最为合适,因为它可以显示数据类别的相对大小,并快速突出每个类别中贡献最大的项目。
此示例创建了一个包含八个叶子、四个主干和两个分支的树形图。setGroupingItem 方法在分组级别 1 上分配主干,在级别 2 上分配分支。数值决定矩形的相对大小,类别标签和重叠的父标签标识层级结构。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Treemap, 20, 20, 500, 300);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
// 分支 1
IChartDataCell categoryCell = workbook.getCell(0, "C1", "Leaf1");
IChartCategory leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem1");
leaf.getGroupingLevels().setGroupingItem(2, "Branch1");
categoryCell = workbook.getCell(0, "C2", "Leaf2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "C3", "Leaf3");
leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem2");
categoryCell = workbook.getCell(0, "C4", "Leaf4");
chart.getChartData().getCategories().add(categoryCell);
// 分支 2
categoryCell = workbook.getCell(0, "C5", "Leaf5");
leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem3");
leaf.getGroupingLevels().setGroupingItem(2, "Branch2");
categoryCell = workbook.getCell(0, "C6", "Leaf6");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "C7", "Leaf7");
leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem4");
categoryCell = workbook.getCell(0, "C8", "Leaf8");
chart.getChartData().getCategories().add(categoryCell);
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Treemap);
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(true);
IChartDataCell valueCell = workbook.getCell(0, "D1", 4);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D2", 5);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D3", 3);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D4", 6);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D5", 9);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D6", 9);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D7", 4);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
valueCell = workbook.getCell(0, "D8", 3);
series.getDataPoints().addDataPointForTreemapSeries(valueCell);
series.setParentLabelLayout(ParentLabelLayoutType.Overlapping);
presentation.save("Treemap.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建瀑布图
瀑布图展示正向和负向变化如何累计至最终值。例如,利润分解可以从收入开始,减去成本,再加上其他收入。变化列在前后累计值之间浮动,而总计列则从零延伸至累计值。
以下示例使用千美元为单位的金额:
| 类别 | 工作簿值 | 列类型 |
|---|---|---|
| Revenue | 1000 | Increase |
| Cost of sales | -400 | Decrease |
| Gross profit | 600 | Intermediate total |
| Operating expenses | -200 | Decrease |
| Other income | 50 | Increase |
| Net profit | 450 | Final total |
要将点标记为中间或最终总计,请将其 setSetAsTotal 方法的参数设为 true,并在该点的工作簿单元格中提供实际累计金额:600 表示毛利润(1000 - 400),450 表示净利润(600 - 200 + 50)。该方法仅标记列为总计,不会计算或填充值。请勿使用零作为占位符,也不要为总计点提供其他变化值。总计列会显示累计值,但不会再次将其加到运行值中。
此完整的 Java 示例使用 ChartType 的 Waterfall 成员创建图表,清除默认数据,并填充嵌入式工作簿。工作表索引 0 选择第一张工作表;A 列包含类别名称,B 列包含数值。每个数值单元格通过 addDataPointForWaterfallSeries 添加。基于零的点索引 2 和 5 标识中间和最终总计。调用 setShowValue 并传入 true 可在列上显示数额。此示例不需要输入演示文稿或工作簿。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Waterfall, 20, 20, 680, 200);
chart.setTitle(true);
chart.getChartTitle().addTextFrameForOverriding("Profit breakdown (USD thousands)");
chart.getChartTitle().setOverlay(false);
chart.setLegend(false);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
int worksheetIndex = 0;
workbook.clear(worksheetIndex);
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Profit");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, ChartType.Waterfall);
String[] categories = new String[] { "Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit" };
double[] values = new double[] { 1000, -400, 600, -200, 50, 450 };
for (int i = 0; i < categories.length; i++)
{
IChartDataCell categoryCell = workbook.getCell(worksheetIndex, i + 1, 0, categories[i]);
chart.getChartData().getCategories().add(categoryCell);
IChartDataCell valueCell = workbook.getCell(worksheetIndex, i + 1, 1, values[i]);
series.getDataPoints().addDataPointForWaterfallSeries(valueCell);
}
// 第三和第六个点表示总计,而不是额外的变化。
series.getDataPoints().get_Item(2).setSetAsTotal(true);
series.getDataPoints().get_Item(5).setSetAsTotal(true);
series.setShowConnectorLines(true);
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
presentation.save("WaterfallChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
在系列级别调用 setShowConnectorLines 并传入 true 可显示连接连续列的线,使运行值更易追踪。若要隐藏这些线,请在示例中将调用替换为 series.setShowConnectorLines(false); 再保存。此更改仅影响线的可见性,不影响数据或总计。
保存后的演示文稿显示毛利润 600 和净利润 450,两个总计均从零开始绘制:

创建股票图
股票图用于展示开盘价、最高价、最低价和收盘价等金融数据,帮助分析市场趋势和波动。它们为股票表现提供关键洞察,帮助投资者和分析师做出明智决策。
此示例创建了一个包含四个系列(顺序为开‑高‑低‑收)的股票图,并包含三个类别。setUpDownBars 方法启用上下柱线,getHiLowLinesFormat 控制显示价格区间的高低线,同时隐藏各系列线条和次要水平网格线。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.OpenHighLowClose, 20, 20, 500, 300, false);
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
IChartDataCell categoryCell = workbook.getCell(0, 1, 0, "A");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 2, 0, "B");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 3, 0, "C");
chart.getChartData().getCategories().add(categoryCell);
IChartDataCell seriesNameCell = workbook.getCell(0, 0, 1, "Open");
chart.getChartData().getSeries().add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.getCell(0, 0, 2, "High");
chart.getChartData().getSeries().add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.getCell(0, 0, 3, "Low");
chart.getChartData().getSeries().add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.getCell(0, 0, 4, "Close");
chart.getChartData().getSeries().add(seriesNameCell, ChartType.OpenHighLowClose);
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
IChartDataCell valueCell = workbook.getCell(0, 1, 1, 72);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 1, 25);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 1, 38);
series.getDataPoints().addDataPointForStockSeries(valueCell);
series = chart.getChartData().getSeries().get_Item(1);
valueCell = workbook.getCell(0, 1, 2, 172);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 2, 57);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 2, 57);
series.getDataPoints().addDataPointForStockSeries(valueCell);
series = chart.getChartData().getSeries().get_Item(2);
valueCell = workbook.getCell(0, 1, 3, 12);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 3, 12);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 3, 13);
series.getDataPoints().addDataPointForStockSeries(valueCell);
series = chart.getChartData().getSeries().get_Item(3);
valueCell = workbook.getCell(0, 1, 4, 25);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 4, 38);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 4, 50);
series.getDataPoints().addDataPointForStockSeries(valueCell);
chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars().setUpDownBars(true);
chart.getChartData().getSeriesGroups().get_Item(0).getHiLowLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
for (IChartSeries stockSeries : chart.getChartData().getSeries())
{
stockSeries.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
}
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
presentation.save("Stock-chart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建箱线图
箱线图用于通过汇总关键统计度量(如中位数、四分位数和潜在离群值)显示数据分布。它们在探索性数据分析和统计研究中尤其有价值,可快速了解数据变异性并识别异常。
此示例使用六个数值创建箱线图。setQuartileMethod 方法选择排他性四分位计算。示例还启用了均值线、均值标记、内部点和离群点。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.BoxAndWhisker, 20, 20, 500, 300);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
IChartDataCell categoryCell = workbook.getCell(0, "A1", "Category 1");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A2", "Category 2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A3", "Category 3");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A4", "Category 4");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A5", "Category 5");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A6", "Category 6");
chart.getChartData().getCategories().add(categoryCell);
IChartSeries series = chart.getChartData().getSeries().add(ChartType.BoxAndWhisker);
series.setQuartileMethod(QuartileMethodType.Exclusive);
series.setShowMeanLine(true);
series.setShowMeanMarkers(true);
series.setShowInnerPoints(true);
series.setShowOutlierPoints(true);
IChartDataCell valueCell = workbook.getCell(0, "B1", 15);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.getCell(0, "B2", 41);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.getCell(0, "B3", 16);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.getCell(0, "B4", 10);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.getCell(0, "B5", 23);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.getCell(0, "B6", 16);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(valueCell);
presentation.save("BoxAndWhisker.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
创建漏斗图
漏斗图用于可视化包含顺序阶段的流程,数据量随着步骤推进而递减。它们在分析转化率、识别瓶颈以及跟踪销售或营销流程效率方面特别有用。
此示例使用 addChart 向现有演示文稿的第一张幻灯片添加漏斗图。它用六个类别和从 50 到 500 的数值替换默认数据,然后保存更新后的演示文稿。输入演示文稿必须至少包含一张幻灯片。
import com.aspose.slides.*;
Presentation presentation = new Presentation("test.pptx");
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Funnel, 50, 50, 500, 400);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
IChartDataCell categoryCell = workbook.getCell(0, "A1", "Category 1");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A2", "Category 2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A3", "Category 3");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A4", "Category 4");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A5", "Category 5");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "A6", "Category 6");
chart.getChartData().getCategories().add(categoryCell);
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Funnel);
IChartDataCell valueCell = workbook.getCell(0, "B1", 50);
series.getDataPoints().addDataPointForFunnelSeries(valueCell);
valueCell = workbook.getCell(0, "B2", 100);
series.getDataPoints().addDataPointForFunnelSeries(valueCell);
valueCell = workbook.getCell(0, "B3", 200);
series.getDataPoints().addDataPointForFunnelSeries(valueCell);
valueCell = workbook.getCell(0, "B4", 300);
series.getDataPoints().addDataPointForFunnelSeries(valueCell);
valueCell = workbook.getCell(0, "B5", 400);
series.getDataPoints().addDataPointForFunnelSeries(valueCell);
valueCell = workbook.getCell(0, "B6", 500);
series.getDataPoints().addDataPointForFunnelSeries(valueCell);
presentation.save("Funnel.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建旭日图
旭日图用于可视化层级数据,将各层级显示为同心环。它们帮助说明部分与整体的关系,适用于以清晰紧凑的方式展示嵌套的类别和子类别。
此示例创建了一个包含八个叶子、四个主干和两个分支的旭日图。setGroupingItem 方法在分组级别 1 上分配主干,在级别 2 上分配分支。类别标签标识各段,数值决定相对大小。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Sunburst, 20, 20, 500, 300);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
// 分支 1
IChartDataCell categoryCell = workbook.getCell(0, "C1", "Leaf1");
IChartCategory leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem1");
leaf.getGroupingLevels().setGroupingItem(2, "Branch1");
categoryCell = workbook.getCell(0, "C2", "Leaf2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "C3", "Leaf3");
leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem2");
categoryCell = workbook.getCell(0, "C4", "Leaf4");
chart.getChartData().getCategories().add(categoryCell);
// 分支 2
categoryCell = workbook.getCell(0, "C5", "Leaf5");
leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem3");
leaf.getGroupingLevels().setGroupingItem(2, "Branch2");
categoryCell = workbook.getCell(0, "C6", "Leaf6");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "C7", "Leaf7");
leaf = chart.getChartData().getCategories().add(categoryCell);
leaf.getGroupingLevels().setGroupingItem(1, "Stem4");
categoryCell = workbook.getCell(0, "C8", "Leaf8");
chart.getChartData().getCategories().add(categoryCell);
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Sunburst);
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(true);
IChartDataCell valueCell = workbook.getCell(0, "D1", 4);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D2", 5);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D3", 3);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D4", 6);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D5", 9);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D6", 9);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D7", 4);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
valueCell = workbook.getCell(0, "D8", 3);
series.getDataPoints().addDataPointForSunburstSeries(valueCell);
presentation.save("Sunburst.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建直方图
直方图展示数值在区间(箱)中的分布情况。对订单处理时间的月度报告而言,使用相同的箱宽和阈值可保持区间一致,便于比较分布。
在水平坐标轴上调用 setAggregationType 并传入来自 AxisAggregationType 的模式。仅在 ByBinWidth 时使用 setBinWidth,仅在 ByNumberOfBins 时使用 setNumberOfBins。使用 Automatic 让图表自行决定箱数。
使用固定箱宽
此示例将处理时间分组为 10 分钟间隔,并为 ≤10 分钟和 >40 分钟的时间分别设置单独的箱。通过 setUnderflowBin 和 setOverflowBin 启用这些箱。当通过 setUnderflowBin 和 setOverflowBin 显式提供阈值时,需将 setAutomaticUnderflowBin 与 setAutomaticOverflowBin 的参数设为 false。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Histogram, 20, 20, 680, 200);
chart.setLegend(false);
chart.setTitle(false);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Histogram);
double[] values = { 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 };
for (int i = 0; i < values.length; i++)
{
IChartDataCell valueCell = workbook.getCell(0, i, 0, values[i]);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
}
IAxis axis = chart.getAxes().getHorizontalAxis();
axis.setAggregationType(AxisAggregationType.ByBinWidth);
axis.setBinWidth(10);
axis.setUnderflowBin(true);
axis.setAutomaticUnderflowBin(false);
axis.setUnderflowBin(10);
axis.setOverflowBin(true);
axis.setAutomaticOverflowBin(false);
axis.setOverflowBin(40);
presentation.save("Histogram-bin-width.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
PowerPoint 中显示的演示文稿的箱计数为 3、4、4、4、2。下溢箱包含 5, 10, 10,因此阈值为闭区间;上溢箱仅包含 41, 50,阈值为开区间;等于 40 的值仍属于前一个区间。内部箱包含大于下界且小于或等于上界的值。

指定箱数
若希望以固定的细节层级而非固定区间进行展示,可为相同数据请求 5 个箱。此示例未启用下溢和上溢箱。图表依据数据范围计算箱宽,因而边界可能随报告而变化。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Histogram, 20, 20, 680, 200);
chart.setLegend(false);
chart.setTitle(false);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Histogram);
double[] values = { 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 };
for (int i = 0; i < values.length; i++)
{
IChartDataCell valueCell = workbook.getCell(0, i, 0, values[i]);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
}
IAxis axis = chart.getAxes().getHorizontalAxis();
axis.setAggregationType(AxisAggregationType.ByNumberOfBins);
axis.setNumberOfBins(5);
presentation.save("Histogram-bin-count.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
这五个箱的计数为 4、4、4、4、1。将此分布与上面的固定宽度结果进行比较:输入值相同,但边界不同。

创建雷达图
雷达图用于在二维平面上展示多变量数据,便于同时比较多个变量。它们在识别模式、优势和劣势方面尤为有用,适用于对多个绩效指标或属性进行综合评估。
此示例使用 addChart 创建默认系列和类别的雷达图,然后将演示文稿保存为 PPTX。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
slide.getShapes().addChart(ChartType.Radar, 20, 20, 500, 300);
presentation.save("Radar-chart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建多类别图表
多类别图表用于展示涉及多个分类分组的数据,使您能够在多个维度上同时比较数值。它们在分析复杂、多层次数据集的趋势和关系时特别有帮助。
此示例创建了一个簇状柱形图,包含四对共八个类别。setGroupingItem 方法在分组级别 1 上为每对分配共享标签,从而在类别轴上同时显示单独标签和分组标签。
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);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
int worksheetIndex = 0;
IChartDataCell categoryCell = workbook.getCell(0, "c2", "A");
IChartCategory category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group1");
categoryCell = workbook.getCell(0, "c3", "B");
category = chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "c4", "C");
category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group2");
categoryCell = workbook.getCell(0, "c5", "D");
category = chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "c6", "E");
category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group3");
categoryCell = workbook.getCell(0, "c7", "F");
category = chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "c8", "G");
category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group4");
categoryCell = workbook.getCell(0, "c9", "H");
category = chart.getChartData().getCategories().add(categoryCell);
IChartDataCell seriesNameCell = workbook.getCell(0, "D1", "Series 1");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, ChartType.ClusteredColumn);
IChartDataCell valueCell = workbook.getCell(worksheetIndex, "D2", 10);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D3", 20);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D4", 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D5", 40);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D6", 50);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D7", 60);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D8", 70);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D9", 80);
series.getDataPoints().addDataPointForBarSeries(valueCell);
presentation.save("AsposeChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

创建地图图表
地图图表用于通过将信息映射到特定地点(如国家、州或城市)来可视化地理数据。它们在分析地区趋势、人口统计数据和空间分布时尤为直观且具吸引力。
此示例使用 addChart 创建默认地理数据的地图图表,并保存以供在 PowerPoint 中查看。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.Map, 20, 20, 500, 300);
presentation.save("mapChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
结果:

Note
上图显示了在 PowerPoint 中打开的已保存演示文稿。Aspose.Slides 正确写入了地图图表及其数据,但本身不绘制地图图表:当包含地图图表的幻灯片渲染为图像或转换为 PDF、SVG 时,图表区域会空白。同一幻灯片上的其他形状不受影响。创建组合图表
组合图表(或 combo 图表)在同一图表中合并两种或多种图表类型。该图表可帮助您突出、比较或检查多个数据集之间的差异,从而识别它们之间的关系。

此示例将两个簇状柱形系列与一个折线系列在四个类别上组合。柱形使用主垂直坐标轴,而 setPlotOnSecondAxis 将折线放置在右侧的次坐标轴上。辅助函数配置标题、图例、坐标轴标签和网格线。
import com.aspose.slides.*;
import java.awt.Color;
class ComboChartExample
{
void createComboChart()
{
Presentation presentation = new Presentation();
try
{
IChart chart = createChartWithFirstSeries(presentation.getSlides().get_Item(0));
addSecondSeriesToChart(chart);
addThirdSeriesToChart(chart);
setPrimaryAxesFormat(chart);
setSecondaryAxesFormat(chart);
presentation.save("combo-chart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
}
IChart createChartWithFirstSeries(ISlide slide)
{
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.setTitle(true);
chart.getChartTitle().addTextFrameForOverriding("Chart Title");
chart.getChartTitle().setOverlay(false);
IPortionFormat portionFormat = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
portionFormat.setFontBold(NullableBool.False);
portionFormat.setFontHeight(18f);
chart.getLegend().setPosition(LegendPositionType.Bottom);
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12f);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
int worksheetIndex = 0;
IChartDataCell categoryCell = workbook.getCell(worksheetIndex, 1, 0, "Category 1");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 2, 0, "Category 2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 3, 0, "Category 3");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 4, 0, "Category 4");
chart.getChartData().getCategories().add(categoryCell);
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Series 1");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
series.getParentSeriesGroup().setOverlap((byte) -25);
series.getParentSeriesGroup().setGapWidth(220);
IChartDataCell valueCell = workbook.getCell(worksheetIndex, 1, 1, 4.3);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 1, 2.5);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 1, 3.5);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 4, 1, 4.5);
series.getDataPoints().addDataPointForBarSeries(valueCell);
return chart;
}
void addSecondSeriesToChart(IChart chart)
{
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
final int worksheetIndex = 0;
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 0, 2, "Series 2");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, ChartType.ClusteredColumn);
series.getParentSeriesGroup().setOverlap((byte) -25);
series.getParentSeriesGroup().setGapWidth(220);
IChartDataCell valueCell = workbook.getCell(worksheetIndex, 1, 2, 2.4);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 2, 4.4);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 2, 1.8);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 4, 2, 2.8);
series.getDataPoints().addDataPointForBarSeries(valueCell);
}
void addThirdSeriesToChart(IChart chart)
{
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
final int worksheetIndex = 0;
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 0, 3, "Series 3");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, ChartType.Line);
IChartDataCell valueCell = workbook.getCell(worksheetIndex, 1, 3, 2.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 3, 2.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 3, 3.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 4, 3, 5.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
series.setPlotOnSecondAxis(true);
}
void setPrimaryAxesFormat(IChart chart)
{
IAxis horizontalAxis = chart.getAxes().getHorizontalAxis();
horizontalAxis.getTextFormat().getPortionFormat().setFontHeight(12f);
horizontalAxis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
setAxisTitle(horizontalAxis, "X Axis");
IAxis verticalAxis = chart.getAxes().getVerticalAxis();
verticalAxis.getTextFormat().getPortionFormat().setFontHeight(12f);
verticalAxis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
setAxisTitle(verticalAxis, "Y Axis 1");
ILineFillFormat majorGridLinesFormat = verticalAxis.getMajorGridLinesFormat().getLine().getFillFormat();
majorGridLinesFormat.setFillType(FillType.Solid);
majorGridLinesFormat.getSolidFillColor().setColor(new Color(217, 217, 217));
}
void setSecondaryAxesFormat(IChart chart)
{
IAxis secondaryHorizontalAxis = chart.getAxes().getSecondaryHorizontalAxis();
secondaryHorizontalAxis.setPosition(AxisPositionType.Bottom);
secondaryHorizontalAxis.setCrossType(CrossesType.Maximum);
secondaryHorizontalAxis.setVisible(false);
secondaryHorizontalAxis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
secondaryHorizontalAxis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
IAxis secondaryVerticalAxis = chart.getAxes().getSecondaryVerticalAxis();
secondaryVerticalAxis.setPosition(AxisPositionType.Right);
secondaryVerticalAxis.getTextFormat().getPortionFormat().setFontHeight(12f);
secondaryVerticalAxis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
secondaryVerticalAxis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
secondaryVerticalAxis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
setAxisTitle(secondaryVerticalAxis, "Y Axis 2");
}
void setAxisTitle(IAxis axis, String axisTitle)
{
axis.setTitle(true);
axis.getTitle().setOverlay(false);
IPortionFormat titlePortionFormat = axis.getTitle().addTextFrameForOverriding(axisTitle).getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
titlePortionFormat.setFontBold(NullableBool.False);
titlePortionFormat.setFontHeight(12f);
}
}
ComboChartExample example = new ComboChartExample();
example.createComboChart();
更新图表
Aspose.Slides for Java 使您能够通过修改图表数据、格式和样式来更新 PowerPoint 图表。此功能简化了保持演示文稿与动态内容同步的过程,并确保图表准确反映当前数据和视觉规范。
此示例更新现有演示文稿第一张幻灯片中名为 “My chart” 的图表。该图表需要两列系列,每列包含三个数据点,类别标签位于 A 列,系列名称位于第一张工作表的首行。示例使用 getCell 更改工作簿单元格,更新系列值,添加第三个系列,并通过 setType 将图表改为簇状圆柱体。
import com.aspose.slides.*;
final String chartName = "My chart";
Presentation presentation = new Presentation("ExistingChart.pptx");
try
{
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes())
{
if (shape instanceof IChart && chartName.equals(shape.getName()))
{
IChart chart = (IChart) shape;
int worksheetIndex = 0;
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.getCell(worksheetIndex, 1, 0, "Modified Category 1");
workbook.getCell(worksheetIndex, 2, 0, "Modified Category 2");
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
workbook.getCell(worksheetIndex, 0, 1, "New_Series 1");
series.getDataPoints().get_Item(0).getValue().setData(90);
series.getDataPoints().get_Item(1).getValue().setData(123);
series.getDataPoints().get_Item(2).getValue().setData(44);
series = chart.getChartData().getSeries().get_Item(1);
workbook.getCell(worksheetIndex, 0, 2, "New_Series 2");
series.getDataPoints().get_Item(0).getValue().setData(23);
series.getDataPoints().get_Item(1).getValue().setData(67);
series.getDataPoints().get_Item(2).getValue().setData(99);
IChartDataCell seriesNameCell = workbook.getCell(worksheetIndex, 0, 3, "Series 3");
series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
IChartDataCell valueCell = workbook.getCell(worksheetIndex, 1, 3, 20);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 3, 50);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 3, 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
chart.setType(ChartType.ClusteredCylinder);
}
}
presentation.save("AsposeChartModified.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
为图表设置数据范围
要查看现有图表已使用的范围,请参阅 Retrieve a Chart’s Data Range。
Aspose.Slides for Java 提供了从工作表中指定特定数据范围作为图表数据源的灵活性。这意味着您可以直接映射工作表的某一部分到图表,从而控制哪些单元格贡献于图表的系列和类别。这样,您可以轻松更新并同步图表,使其始终反映工作表中的最新数据,确保 PowerPoint 演示文稿内容准确。
此示例使用 setRange 将名为 “My chart” 的图表(位于现有演示文稿的第一张幻灯片)的数据源设为 Sheet1!A1:B4。图表工作簿必须包含该工作表,并且该范围需已填充:第一行是系列标题,A 列是类别标签,B 列是三个数值。
import com.aspose.slides.*;
final String chartName = "My chart";
Presentation presentation = new Presentation("ExistingChart.pptx");
try
{
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes())
{
if (shape instanceof IChart && chartName.equals(shape.getName()))
{
IChart chart = (IChart) shape;
chart.getChartData().setRange("Sheet1!A1:B4");
}
}
presentation.save("SetDataRange.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
在图表中使用默认标记
当在图表中使用默认标记时,每个系列会自动获得不同的默认标记符号。
此示例使用 addChart 创建包含两个折线系列和图例的图表。通过 getMarker 访问的标记设置保留了自动分配的标记符号。第一系列包含一个负值和一个缺失值(null),第二系列则为每个类别提供了数值。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try
{
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 10, 10, 400, 400);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
IChartDataCell seriesNameCell = workbook.getCell(0, 0, 1, "Series 1");
IChartSeries series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
IChartDataCell categoryCell = workbook.getCell(0, 1, 0, "C1");
chart.getChartData().getCategories().add(categoryCell);
IChartDataCell valueCell = workbook.getCell(0, 1, 1, 24);
series.getDataPoints().addDataPointForLineSeries(valueCell);
categoryCell = workbook.getCell(0, 2, 0, "C2");
chart.getChartData().getCategories().add(categoryCell);
valueCell = workbook.getCell(0, 2, 1, 23);
series.getDataPoints().addDataPointForLineSeries(valueCell);
categoryCell = workbook.getCell(0, 3, 0, "C3");
chart.getChartData().getCategories().add(categoryCell);
valueCell = workbook.getCell(0, 3, 1, -10);
series.getDataPoints().addDataPointForLineSeries(valueCell);
categoryCell = workbook.getCell(0, 4, 0, "C4");
chart.getChartData().getCategories().add(categoryCell);
valueCell = workbook.getCell(0, 4, 1, null);
series.getDataPoints().addDataPointForLineSeries(valueCell);
seriesNameCell = workbook.getCell(0, 0, 2, "Series 2");
IChartSeries secondSeries = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
valueCell = workbook.getCell(0, 1, 2, 30);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(0, 2, 2, 10);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(0, 3, 2, 60);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(0, 4, 2, 40);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
chart.setLegend(true);
chart.getLegend().setOverlay(false);
presentation.save("DefaultMarkersInChart.pptx", SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
常见问题
Aspose.Slides for Java 支持哪些图表类型?
Aspose.Slides for Java 支持包括柱形图、折线图、饼图、区域图、散点图、直方图、雷达图等在内的多种图表类型。这为您的数据可视化需求提供了灵活的选择。
如何向幻灯片添加新图表?
要添加图表,首先创建 Presentation 实例,使用索引获取目标幻灯片,然后调用添加图表的方法,指定图表类型和初始数据。此过程可将图表直接集成到演示文稿中。
如何更新图表中显示的数据?
您可以通过访问其数据工作簿 (IChartDataWorkbook),清除任何默认系列和类别,然后添加自定义数据,从而编程方式刷新图表以反映最新数据。
是否可以自定义图表的外观?
可以,Aspose.Slides for Java 提供了丰富的自定义选项。您可以修改颜色、字体、标签、图例以及其他格式元素,以满足特定的设计需求。