如何在演示文稿中创建图表
Contents
[
Hide
]
一款新的 Aspose.Slides for Java API 已经发布,现在这个单一产品支持从头生成 PowerPoint 文档以及编辑现有文档的功能。
对旧代码的支持
为了使用使用 Aspose.Slides for Java 早于 14.x.x 版本开发的旧代码,您需要在代码中进行一些小的更改,代码将如之前一样工作。所有在旧版 Aspose.Slides for Java 中位于 com.aspose.slides 和 com.aspose.slides.pptx 命名空间下的类现在合并为单一的 com.aspose.slides 命名空间。请查看以下简单代码片段,该片段使用旧版 Aspose.Slides API 从头创建普通图表,并遵循描述如何迁移到新合并 API 的步骤。
旧版 Aspose.Slides for Java 方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Instantiate PresentationEx class that represents PPTX file | |
PresentationEx pres = new PresentationEx(); | |
//Access first slide | |
SlideEx sld = pres.getSlides().get_Item(0); | |
// Add chart with default data | |
ChartEx chart = sld.getShapes().addChart(ChartTypeEx.ClusteredColumn, 0, 0, 500, 500); | |
//Setting chart Title | |
chart.getChartTitle().getText().setText("Sample Title"); | |
chart.getChartTitle().getText().setCenterText(true); | |
chart.getChartTitle().setHeight(20f); | |
chart.hasTitle(true); | |
//Set first series to Show Values | |
chart.getChartData().getSeries().get_Item(0).getLabels().setShowValue(true); | |
//Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
//Getting the chart data worksheet | |
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory(); | |
//Delete default generated series and categories | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
int s = chart.getChartData().getSeries().getCapacity(); | |
//Adding new series | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType()); | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType()); | |
//Adding new categories | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1")); | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2")); | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3")); | |
//Take first chart series | |
ChartSeriesEx series = chart.getChartData().getSeries().get_Item(0); | |
//Now populating series data | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 1, 20)); | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 1, 50)); | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 1, 30)); | |
//Setting fill color for series | |
series.getFormat().getFill().setFillType(FillTypeEx.Solid); | |
series.getFormat().getFill().getSolidFillColor().setColor(new java.awt.Color(com.aspose.slides.PresetColorEx.Red)); | |
//Take second chart series | |
series = chart.getChartData().getSeries().get_Item(1); | |
//Now populating series data | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 2, 30)); | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 2, 10)); | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 2, 60)); | |
//Setting fill color for series | |
series.getFormat().getFill().setFillType(FillTypeEx.Solid); | |
series.getFormat().getFill().getSolidFillColor().setColor(new java.awt.Color(PresetColorEx.Green)); | |
//create custom lables for each of categories for new series | |
//first label will be show Category name | |
DataLabelEx lbl = new DataLabelEx(series); | |
lbl.setShowCategoryName( true); | |
lbl.setId (0); | |
series.getLabels().add(lbl); | |
//Show series name for second label | |
lbl = new DataLabelEx(series); | |
lbl.setShowSeriesName(true); | |
lbl.setId(1); | |
series.getLabels().add(lbl); | |
//show value for third label | |
lbl = new DataLabelEx(series); | |
lbl.setShowValue(true); | |
lbl.setShowSeriesName(true); | |
lbl.setSeparator("/"); | |
lbl.setId( 2); | |
series.getLabels().add(lbl); | |
// show value and custom text | |
lbl = new DataLabelEx(series); | |
lbl.getTextFrame().setText( "My text"); | |
lbl.setId( 3); | |
series.getLabels().add(lbl); | |
// Save presentation with chart | |
pres.write("D:\\Aspose Data\\AsposeChart.pptx"); |
新 Aspose.Slides for Java 14.x.x 方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Instantiate Presentation class that represents PPTX file | |
Presentation pres = new Presentation(); | |
//Access first slide | |
ISlide sld = pres.getSlides().get_Item(0); | |
// Add chart with default data | |
IChart chart = sld.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500); | |
//Setting chart Title | |
//chart.getChartTitle().TextFrameForOverriding.Text = "Sample Title"; | |
chart.getChartTitle().addTextFrameForOverriding("Sample Title"); | |
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True); | |
chart.getChartTitle().setHeight(20); | |
chart.hasTitle(true); | |
//Set first series to Show Values | |
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true); | |
//Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
//Getting the chart data worksheet | |
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook(); | |
//Delete default generated series and categories | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
int s = chart.getChartData().getSeries().size(); | |
s = chart.getChartData().getCategories().size(); | |
//Adding new series | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType()); | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType()); | |
//Adding new categories | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1")); | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2")); | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3")); | |
//Take first chart series | |
IChartSeries series = chart.getChartData().getSeries().get_Item(0); | |
//Now populating series data | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30)); | |
//Setting fill color for series | |
series.getFormat().getFill().setFillType(FillType.Solid); | |
series.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.RED); | |
//Take second chart series | |
series = chart.getChartData().getSeries().get_Item(1); | |
//Now populating series data | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60)); | |
//Setting fill color for series | |
series.getFormat().getFill().setFillType(FillType.Solid); | |
series.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.GREEN); | |
//create custom labels for each of categories for new series | |
//first label will be show Category name | |
IDataLabel lbl = series.getDataPoints().get_Item(0).getLabel(); | |
lbl.getDataLabelFormat().setShowCategoryName(true); | |
lbl = series.getDataPoints().get_Item(1).getLabel(); | |
lbl.getDataLabelFormat().setShowSeriesName(true); | |
//Show value for third label | |
lbl = series.getDataPoints().get_Item(2).getLabel(); | |
lbl.getDataLabelFormat().setShowValue(true); | |
lbl.getDataLabelFormat().setShowSeriesName(true); | |
lbl.getDataLabelFormat().setSeparator("/"); | |
//Save presentation with chart | |
pres.save("AsposeChart.pptx",SaveFormat.Pptx); |
请查看以下简单代码片段,该片段使用旧版 Aspose.Slides API 从头创建散点图,并了解如何使用新合并 API 实现。
旧版 Aspose.Slides for Java 方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Instantiate PresentationEx class that represents PPTX file | |
PresentationEx pres = new PresentationEx("D:\\AsposeChart.pptx"); | |
//Access first slide | |
SlideEx sld = pres.getSlides().get_Item(0); | |
// Add chart with default data | |
ChartEx chart = (ChartEx)sld.getShapes().get_Item(0); | |
//Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
//Getting the chart data worksheet | |
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory(); | |
//Take first chart series | |
ChartSeriesEx series = chart.getChartData().getSeries().getItem(0); | |
//Now updating series data | |
fact.getCell(defaultWorksheetIndex, 0, 1, "New_Series1");//modifying series name | |
series.getValues().get_Item(0).setValue(90); | |
series.getValues().get_Item(1).setValue(123); | |
series.getValues().get_Item(2).setValue(44); | |
//Take Second chart series | |
series = chart.getChartData().getSeries().getItem(1); | |
//Now updating series data | |
fact.getCell(defaultWorksheetIndex, 0, 2, "New_Series2");//modifying series name | |
series.getValues().get_Item(0).setValue(23); | |
series.getValues().get_Item(1).setValue(67); | |
series.getValues().get_Item(2).setValue(99); | |
//Now, Adding a new series | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 3, "Series 3"), chart.getType()); | |
//Take 3rd chart series | |
series = chart.getChartData().getSeries().getItem(2); | |
//Now populating series data | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 3, 20)); | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 3, 50)); | |
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 3, 30)); | |
chart.setType(ChartTypeEx.ClusteredCylinder); | |
// Save presentation with chart | |
pres.write("D:\\AsposeChartMoodified.pptx"); |
新 Aspose.Slides for Java 14.x.x 方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Presentation pres = new Presentation(); | |
ISlide slide = pres.getSlides().get_Item(0); | |
//Creating the default chart | |
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400); | |
//Getting the default chart data worksheet index | |
int defaultWorksheetIndex = 0; | |
//Accessing the chart data worksheet | |
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook(); | |
//Delete demo series | |
chart.getChartData().getSeries().clear(); | |
//Add new series | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType()); | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.getType()); | |
//Take first chart series | |
IChartSeries series = chart.getChartData().getSeries().get_Item(0); | |
//Add new point (1:3) there. | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3)); | |
//Add new point (2:10) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10)); | |
//Edit the type of series | |
series.setType (ChartType.ScatterWithStraightLinesAndMarkers); | |
//Changing the chart series marker | |
series.getMarker().setSize(10); | |
series.getMarker().setSymbol(MarkerStyleType.Star); | |
//Take second chart series | |
series = chart.getChartData().getSeries().get_Item(1); | |
//Add new point (5:2) there. | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2)); | |
//Add new point (3:1) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1)); | |
//Add new point (2:2) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2)); | |
//Add new point (5:1) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1)); | |
//Changing the chart series marker | |
series.getMarker().setSize(10); | |
series.getMarker().setSymbol (MarkerStyleType.Circle); | |
pres.save("AsposeScatterChart.pptx",SaveFormat.Pptx); |