自定义图表
创建图表
可以使用 Aspose.Cells 向电子表格添加各种图表。Aspose.Cells 提供了许多灵活的图表对象。本主题讨论 Aspose.Cells' 图表对象。
简单地创建一个图表
使用以下示例代码创建带有 Aspose.Cells 的图表很简单:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the output directory. | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Adding sample values to cells | |
worksheet.getCells().get("A2").putValue("Category1"); | |
worksheet.getCells().get("A3").putValue("Category2"); | |
worksheet.getCells().get("A4").putValue("Category3"); | |
worksheet.getCells().get("B1").putValue("Column1"); | |
worksheet.getCells().get("B2").putValue(4); | |
worksheet.getCells().get("B3").putValue(20); | |
worksheet.getCells().get("B4").putValue(50); | |
worksheet.getCells().get("C1").putValue("Column2"); | |
worksheet.getCells().get("C2").putValue(50); | |
worksheet.getCells().get("C3").putValue(100); | |
worksheet.getCells().get("C4").putValue(150); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.getCharts().add(ChartType.COLUMN, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Chart chart = worksheet.getCharts().get(chartIndex); | |
// Setting chart data source as the range "A1:C4" | |
chart.setChartDataRange("A1:C4", true); | |
workbook.save( "ColumnChart.xlsx", SaveFormat.XLSX); |
创建图表的注意事项
在创建图表之前,了解一些在使用 Aspose.Cells 创建图表时有用的基本概念非常重要。
图表对象
Aspose.Cells 提供了一组特殊的类,用于创建各种图表。这些类用于创建图表对象,它们充当图表构建块。图表对象如下所列:
- 轴, 图表的轴。
- 图表,单个 Excel 图表。
- 图表区,工作表中的图表区域。
- 图表数据表图表数据表。
- 图表框图表中的框架对象。
- 图表点图表系列中的单个点。
- 图表点集合,一个包含一个系列中所有点的集合。
- 图表集合 的集合图表对象。
- DataLabels,指定的DataLabels系列, 图表点, 趋势线, ETC。
- 填充格式形状的填充格式。
- 地面3D 图表的底部。
- 传奇图表图例。
- 线, 图表线。
- 系列合集 的集合系列对象。
- 系列, 代表图表中的单个数据系列。
- 刻度标签,与图表轴上的刻度线关联的刻度线标签。
- 标题图表或轴的标题。
- 趋势线, 图表中的趋势线。
- 趋势线系列指定数据系列的所有趋势线对象的集合。
- 墙壁, 3D 图表的墙壁。
使用图表对象
如上所述,所有图表对象都是其各自类的实例,并提供特定的属性和方法来执行特定的任务。使用图表对象创建图表。
使用图表集合收藏。中的每一项图表集合集合代表一个图表目的。一种图表对象封装了自定义图表外观所需的所有图表对象。下一节将展示如何使用一些基本的图表对象来创建一个简单的图表。
创建一个简单的图表
使用 Aspose.Cells 可以创建许多不同类型的图表。Aspose.Cells 支持的所有标准图表都预定义在一个名为图表类型.预定义的图表类型是:
图表类型 | 描述 |
---|---|
柱子 | 表示簇状柱形图 |
列堆叠 | 表示堆积柱形图 |
列 100% 堆叠 | 表示 100% 堆积柱形图 |
Column3D簇状 | 表示 3D 簇状柱形图 |
列 3D 堆叠 | 表示 3D 堆积柱形图 |
Column3D100PercentStacked | 表示 3D 100% 堆积柱形图 |
立柱三维 | 表示 3D 柱形图 |
酒吧 | 表示簇状条形图 |
酒吧堆叠 | 表示堆积条形图 |
Bar100Percent 堆叠 | 表示 100% 堆积条形图 |
Bar3DClustered | 表示 3D 簇状条形图 |
Bar3D堆叠 | 表示 3D 堆积条形图 |
Bar3D100PercentStacked | 表示 3D 100% 堆积条形图 |
线 | 代表折线图 |
线堆叠 | 表示堆叠折线图 |
Line100Percent 堆叠 | 表示 100% 堆叠折线图 |
带数据标记的线 | 表示带有数据标记的折线图 |
LineStackedWithDataMarkers | 表示带有数据标记的堆积折线图 |
Line100PercentStackedWithDataMarkers | 表示带有数据标记的 100% 堆叠折线图 |
直线三维 | 表示 3D 折线图 |
馅饼 | 代表饼图 |
Pie3D | 表示 3D 饼图 |
派派 | 表示饼图的饼图 |
馅饼爆炸 | 代表分解饼图 |
饼图3D分解 | 表示 3D 爆炸饼图 |
馅饼吧 | 代表饼图条 |
分散 | 代表散点图 |
ScatterConnectedByCurvesWithDataMarker | 表示由曲线连接的散点图,带有数据标记 |
ScatterConnectedByCurvesWithoutDataMarker | 表示由曲线连接的散点图,没有数据标记 |
ScatterConnectedByLinesWithDataMarker | 表示由线连接的散点图,带有数据标记 |
ScatterConnectedByLinesWithoutDataMarker | 表示由线连接的散点图,没有数据标记 |
区域 | 代表面积图 |
面积堆叠 | 表示堆积面积图 |
面积 100% 堆叠 | 表示 100% 堆积面积图 |
三维区域 | 表示 3D 面积图 |
Area3D堆叠 | 表示 3D 堆积面积图 |
Area3D100PercentStacked | 表示 3D 100% 堆积面积图 |
油炸圈饼 | 代表甜甜圈图 |
甜甜圈爆炸 | 代表爆炸甜甜圈图 |
雷达 | 代表雷达图 |
带数据标记的雷达 | 代表带有数据标记的雷达图 |
雷达填充 | 表示填充雷达图 |
Surface3D | 表示 3D 曲面图 |
表面线框3D | 表示线框 3D 曲面图 |
表面轮廓 | 代表等高线图 |
表面轮廓线框 | 表示线框等高线图 |
气泡 | 代表气泡图 |
泡泡3D | 代表3D气泡图 |
圆柱 | 表示柱面图 |
圆柱叠层 | 表示堆积柱面图 |
圆柱100%堆叠 | 表示 100% 堆积柱面图 |
圆柱棒 | 表示圆柱形条形图。 |
圆柱形条堆叠 | 表示堆积柱形条形图 |
CylindricalBar100PercentStacked | 表示 100% 堆积柱形条形图 |
圆柱3D | 表示 3D 圆柱柱形图 |
锥体 | 代表圆锥图 |
圆锥堆叠 | 表示堆叠圆锥图 |
Cone100Percent堆叠 | 表示 100% 堆积圆锥图 |
锥形棒 | 代表锥形条形图 |
锥形条堆叠 | 表示堆叠圆锥形条形图 |
ConicalBar100PercentStacked | 表示 100% 堆积锥形条形图 |
锥形柱3D | 表示 3D 锥形柱形图 |
金字塔 | 代表金字塔图 |
金字塔堆叠 | 表示堆积金字塔图 |
金字塔100%堆积 | 表示 100% 堆积金字塔图 |
金字塔酒吧 | 代表金字塔条形图 |
金字塔酒吧堆叠 | 表示堆积金字塔条形图 |
PyramidBar100PercentStacked | 表示 100% 堆积金字塔条形图 |
金字塔柱3D | 表示 3D 金字塔柱形图 |
要使用 Aspose.Cells 创建图表: |
- 将一些数据添加到工作表单元格中Cell对象的设定值方法。 这将用作图表的数据源。
- 通过调用将图表添加到工作表图表集合收藏的[添加](https://reference.aspose.com/cells/java/com.aspose.cells/chartcollection#add(int,%20int,%20int,%20int,%20int) 方法,封装在工作表目的。
- 指定图表类型图表类型枚举。 例如,该示例使用ChartType.金字塔值作为图表类型。
- 访问新的图表对象来自图表集合通过传递其索引进行收集。
- 使用封装在图表对象来管理图表。 下面的示例使用系列合集图表对象指定图表的数据源。
向图表添加源数据时,数据源可以是单元格区域(如“A1:C3”),也可以是不连续的单元格序列(如“A1,A3,A5”),也可以是值(例如“1,2,3”)。
这些一般步骤允许您创建任何类型的图表。使用不同的图表对象来创建不同的图表。
执行示例代码时,金字塔图将添加到工作表中,如下所示。
金字塔图及其数据源
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(HowToCreatePyramidChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(0); | |
// Adding some sample value to cells | |
Cells cells = sheet.getCells(); | |
Cell cell = cells.get("A1"); | |
cell.setValue(50); | |
cell = cells.get("A2"); | |
cell.setValue(100); | |
cell = cells.get("A3"); | |
cell.setValue(150); | |
cell = cells.get("B1"); | |
cell.setValue(4); | |
cell = cells.get("B2"); | |
cell.setValue(20); | |
cell = cells.get("B3"); | |
cell.setValue(180); | |
cell = cells.get("C1"); | |
cell.setValue(320); | |
cell = cells.get("C2"); | |
cell.setValue(110); | |
cell = cells.get("C3"); | |
cell.setValue(180); | |
cell = cells.get("D1"); | |
cell.setValue(40); | |
cell = cells.get("D2"); | |
cell.setValue(120); | |
cell = cells.get("D3"); | |
cell.setValue(250); | |
ChartCollection charts = sheet.getCharts(); | |
// Adding a chart to the worksheet | |
int chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5); | |
Chart chart = charts.get(chartIndex); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell to "B3" | |
SeriesCollection serieses = chart.getNSeries(); | |
serieses.add("A1:B3", true); | |
// Saving the Excel file | |
workbook.save(dataDir + "HToCPyramidChart_out.xls"); | |
// Print message | |
System.out.println("Pyramid chart is successfully created."); |
要创建气泡图,图表类型必须设置为图表类型.BUBBLE并且需要相应地设置一些额外的属性,例如 BubbleSizes、Values 和 XValues。执行以下代码后,气泡图将添加到工作表中,如下所示。
气泡图及其数据源
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(HowToCreateBubbleChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(0); | |
// Adding some sample value to cells | |
Cells cells = sheet.getCells(); | |
Cell cell = cells.get("A1"); | |
cell.setValue(50); | |
cell = cells.get("A2"); | |
cell.setValue(100); | |
cell = cells.get("A3"); | |
cell.setValue(150); | |
cell = cells.get("B1"); | |
cell.setValue(4); | |
cell = cells.get("B2"); | |
cell.setValue(20); | |
cell = cells.get("B3"); | |
cell.setValue(180); | |
cell = cells.get("C1"); | |
cell.setValue(320); | |
cell = cells.get("C2"); | |
cell.setValue(110); | |
cell = cells.get("C3"); | |
cell.setValue(180); | |
cell = cells.get("D1"); | |
cell.setValue(40); | |
cell = cells.get("D2"); | |
cell.setValue(120); | |
cell = cells.get("D3"); | |
cell.setValue(250); | |
ChartCollection charts = sheet.getCharts(); | |
// Adding a chart to the worksheet | |
int chartIndex = charts.add(ChartType.BUBBLE, 5, 0, 15, 5); | |
Chart chart = charts.get(chartIndex); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell to "B3" | |
SeriesCollection serieses = chart.getNSeries(); | |
serieses.add("A1:B3", true); | |
// Set bubble sizes | |
chart.getNSeries().get(0).setBubbleSizes("B2:D2"); | |
chart.getNSeries().get(0).setXValues("B3:D3"); | |
chart.getNSeries().get(0).setValues("B1:D1"); | |
// Saving the Excel file | |
workbook.save(dataDir + "HToCrBChart_out.xls"); | |
// Print message | |
System.out.println("Bubble chart is successfully created."); |
符合数据标记图表
要创建带有数据标记图表的线条,图表类型必须设置为ChartType.LINE_WITH_DATA_MARKERS并且需要相应地设置一些额外的属性,例如背景区域、系列标记、值和 XValues。执行以下代码后,带有数据标记图表的行将添加到工作表中。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Instantiate a workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Set columns title | |
worksheet.getCells().get(0, 0).setValue("X"); | |
worksheet.getCells().get(0, 1).setValue("Y"); | |
// Create random data and save in the cells | |
for (int i = 1; i < 21; i++) | |
{ | |
worksheet.getCells().get(i, 0).setValue(i); | |
worksheet.getCells().get(i, 1).setValue(0.8); | |
} | |
for (int i = 21; i < 41; i++) | |
{ | |
worksheet.getCells().get(i, 0).setValue(i - 20); | |
worksheet.getCells().get(i, 1).setValue(0.9); | |
} | |
// Add a chart to the worksheet | |
int idx = worksheet.getCharts().add(ChartType.LINE_WITH_DATA_MARKERS, 1, 3, 20, 20); | |
// Access the newly created chart | |
Chart chart = worksheet.getCharts().get(idx); | |
// Set chart style | |
chart.setStyle(3); | |
// Set autoscaling value to true | |
chart.setAutoScaling(true); | |
// Set foreground color white | |
chart.getPlotArea().getArea().setForegroundColor(Color.getWhite()); | |
// Set Properties of chart title | |
chart.getTitle().setText("Sample Chart"); | |
// Set chart type | |
chart.setType(ChartType.LINE_WITH_DATA_MARKERS); | |
// Set Properties of categoryaxis title | |
chart.getCategoryAxis().getTitle().setText("Units"); | |
//Set Properties of nseries | |
int s2_idx = chart.getNSeries().add("A2: A2", true); | |
int s3_idx = chart.getNSeries().add("A22: A22", true); | |
// Set IsColorVaried to true for varied points color | |
chart.getNSeries().setColorVaried(true); | |
// Set properties of background area and series markers | |
chart.getNSeries().get(s2_idx).getArea().setFormatting(FormattingType.CUSTOM); | |
chart.getNSeries().get(s2_idx).getMarker().getArea().setForegroundColor(Color.getYellow()); | |
chart.getNSeries().get(s2_idx).getMarker().getBorder().setVisible(false); | |
// Set X and Y values of series chart | |
chart.getNSeries().get(s2_idx).setXValues("A2: A21"); | |
chart.getNSeries().get(s2_idx).setValues("B2: B21"); | |
// Set properties of background area and series markers | |
chart.getNSeries().get(s3_idx).getArea().setFormatting(FormattingType.CUSTOM); | |
chart.getNSeries().get(s3_idx).getMarker().getArea().setForegroundColor(Color.getGreen()); | |
chart.getNSeries().get(s3_idx).getMarker().getBorder().setVisible(false); | |
// Set X and Y values of series chart | |
chart.getNSeries().get(s3_idx).setXValues("A22: A41"); | |
chart.getNSeries().get(s3_idx).setValues("B22: B41"); | |
// Save the workbook | |
workbook.save(outDir + "LineWithDataMarkerChart.xlsx", SaveFormat.XLSX); |
创建自定义图表
到目前为止,当我们讨论图表时,我们已经查看了具有标准格式设置的标准图表。我们只定义数据源,设置一些属性,并使用默认格式设置创建图表。但 Aspose.Cells 还支持创建自定义图表,允许开发者创建具有自己格式设置的图表。
创建自定义图表
开发人员可以使用 Aspose.Cells 简单 API 在运行时创建自定义图表。
图表由数据系列组成。 Aspose.Cells 中的每个数据系列都由一个系列对象而系列合集对象作为集合系列对象。创建自定义图表时,开发人员可以自由地为不同的数据系列(收集在一个系列合集目的)。
下面的示例代码演示了如何创建自定义图表。在此示例中,我们将为第一个数据系列使用柱形图,为第二个系列使用折线图。结果是我们向工作表添加了一个柱形图和一个折线图。
组合柱形图和折线图的自定义图表
编程实例
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(HowToCreateCustomChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(0); | |
// Adding some sample value to cells | |
Cells cells = sheet.getCells(); | |
Cell cell = cells.get("A1"); | |
cell.setValue(50); | |
cell = cells.get("A2"); | |
cell.setValue(100); | |
cell = cells.get("A3"); | |
cell.setValue(150); | |
cell = cells.get("B1"); | |
cell.setValue(4); | |
cell = cells.get("B2"); | |
cell.setValue(20); | |
cell = cells.get("B3"); | |
cell.setValue(180); | |
cell = cells.get("C1"); | |
cell.setValue(320); | |
cell = cells.get("C2"); | |
cell.setValue(110); | |
cell = cells.get("C3"); | |
cell.setValue(180); | |
cell = cells.get("D1"); | |
cell.setValue(40); | |
cell = cells.get("D2"); | |
cell.setValue(120); | |
cell = cells.get("D3"); | |
cell.setValue(250); | |
ChartCollection charts = sheet.getCharts(); | |
// Adding a chart to the worksheet | |
int chartIndex = charts.add(ChartType.COLUMN_3_D, 5, 0, 15, 5); | |
Chart chart = charts.get(chartIndex); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell to "B3" | |
SeriesCollection serieses = chart.getNSeries(); | |
serieses.add("A1:B3", true); | |
// Saving the Excel file | |
workbook.save(dataDir + "HTCCustomChart_out.xls"); | |
// Print message | |
System.out.println("Customized chart is successfully created."); |