自定义图表
创建图表
使用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提供了一组特殊的类,用于创建各种类型的图表。 这些类用于创建图表对象,作为图表构建模块。 下面列出了图表对象:
- Axis,图表的轴。
- Chart,单个Excel图表。
- ChartArea,工作表中的图表区域。
- ChartDataTable,图表数据表。
- ChartFrame,图表中的框架对象。
- ChartPoint,图表系列中的单个点。
- ChartPointCollection,包含一个系列中所有点的集合。
- ChartCollection,包含 Chart 个对象的集合。
- DataLabels,指定 Series,ChartPoint,Trendline 等的 DataLabels。
- FillFormat,形状的填充格式。
- Floor,3D 图表的底板。
- Legend,图表图例。
- Line,图表线条。
- SeriesCollection,包含 Series 个对象的集合。
- Series,代表图表或坐标轴中的单个数据系列。
- TickLabels,与图表坐标轴上的刻度标签相关联的刻度标记标签。
- Title,图表或坐标轴的标题。
- Trendline,图表中的趋势线。
- TrendlineCollection,指定数据系列的所有趋势线对象的集合。
- Walls,3D 图表的墙。
使用图表对象
如上所述,所有的图表对象都是它们各自类的实例,并提供特定的属性和方法来执行特定的任务。使用图表对象来创建图表。
使用 ChartCollection 集合向工作表添加任何类型的图表。ChartCollection 集合中的每个项目代表一个 Chart 对象。一个 Chart 对象封装了所有自定义图表外观所需的图表对象。下一节将展示如何使用一些基本的图表对象来创建一个简单的图表。
创建一个简单的图表
使用 Aspose.Cells 可以创建许多不同类型的图表。Aspose.Cells 支持的所有标准图表都预定义在名为 ChartType 的枚举中。预定义的图表类型包括:
图表类型 | 描述 |
---|---|
Column | 代表分组柱状图 |
ColumnStacked | 代表堆积柱状图 |
Column100PercentStacked | 代表100%堆积柱状图 |
Column3DClustered | 代表3D分组柱状图 |
Column3DStacked | 表示3D堆叠柱形图 |
Column3D100PercentStacked | 表示3D 100%堆叠柱形图 |
Column3D | 表示3D柱形图 |
Bar | 表示分组条形图 |
BarStacked | 表示堆叠条形图 |
Bar100PercentStacked | 表示100%堆叠条形图 |
Bar3DClustered | 表示3D分组条形图 |
Bar3DStacked | 表示3D堆叠条形图 |
Bar3D100PercentStacked | 表示3D 100%堆叠条形图 |
Line | 表示折线图 |
LineStacked | 表示堆叠折线图 |
Line100PercentStacked | 表示100%堆叠折线图 |
LineWithDataMarkers | 表示带有数据标记的折线图 |
LineStackedWithDataMarkers | 表示带有数据标记的堆叠折线图 |
Line100PercentStackedWithDataMarkers | 表示带有数据标记的100%堆叠折线图 |
Line3D | 表示3D折线图 |
Pie | 表示饼图 |
Pie3D | 表示3D饼图 |
PiePie | 表示饼图中的饼图 |
PieExploded | 表示爆炸饼图 |
Pie3DExploded | 表示3D饼图(爆炸式) |
PieBar | 表示饼图的条形图 |
Scatter | 表示散点图 |
ScatterConnectedByCurvesWithDataMarker | 表示用曲线连接的散点图,带数据标记 |
ScatterConnectedByCurvesWithoutDataMarker | 表示用曲线连接的散点图,无数据标记 |
ScatterConnectedByLinesWithDataMarker | 表示用线连接的散点图,带数据标记 |
ScatterConnectedByLinesWithoutDataMarker | 表示用线连接的散点图,无数据标记 |
Area | 表示面积图 |
AreaStacked | 表示堆叠面积图 |
Area100PercentStacked | 表示百分比堆叠面积图 |
Area3D | 表示3D面积图 |
Area3DStacked | 表示3D堆叠面积图 |
Area3D100PercentStacked | 表示3D百分比堆叠面积图 |
Doughnut | 表示圆环图 |
DoughnutExploded | 表示爆炸式环形图 |
Radar | 表示雷达图 |
RadarWithDataMarkers | 表示带数据标记的雷达图 |
RadarFilled | 表示填充雷达图 |
Surface3D | 表示3D曲面图 |
SurfaceWireframe3D | 表示线框3D曲面图 |
SurfaceContour | 表示等高线图表 |
SurfaceContourWireframe | 表示线框等高线图表 |
Bubble | 表示气泡图表 |
Bubble3D | 表示3D气泡图表 |
Cylinder | 表示圆柱图表 |
CylinderStacked | 表示堆叠圆柱图表 |
Cylinder100PercentStacked | 表示100%堆叠圆柱图表 |
CylindricalBar | 表示圆柱形条形图。 |
CylindricalBarStacked | 表示堆叠圆柱形条形图 |
CylindricalBar100PercentStacked | 表示100%堆叠圆柱形条形图 |
CylindricalColumn3D | 表示3D圆柱形柱形图 |
Cone | 表示圆锥图表 |
ConeStacked | 表示堆叠圆锥图表 |
Cone100PercentStacked | 表示100%堆叠圆锥图表 |
ConicalBar | 表示圆锥形条形图 |
ConicalBarStacked | 表示堆叠圆锥形条形图 |
ConicalBar100PercentStacked | 表示100%堆叠圆锥形条形图 |
ConicalColumn3D | 表示3D圆锥形柱形图 |
Pyramid | 表示金字塔图表 |
PyramidStacked | 表示堆叠金字塔图表 |
Pyramid100PercentStacked | 代表100%的堆叠金字塔图表 |
PyramidBar | 代表堆叠金字塔柱状图 |
PyramidBarStacked | 代表堆叠金字塔柱状图 |
PyramidBar100PercentStacked | 代表100%堆叠金字塔柱状图 |
PyramidColumn3D | 代表3D金字塔柱图 |
使用Aspose.Cells创建图表: |
- 使用 Cell 对象的 setValue 方法将一些数据添加到工作表单元格中。 这将被用作图表的数据源。
- 通过在 Worksheet 对象中封装的 ChartCollection 集合的 add 方法添加图表到工作表单中。
- 使用 ChartType 枚举指定图表的类型。 例如,示例将 ChartType.PYRAMID 值用作图表类型。
- 通过传递其索引从 ChartCollection 集合中访问新的 Chart 对象。
- 使用封装在 Chart 对象中的任何绘图对象来管理图表。 下面的示例使用 SeriesCollection 绘图对象指定图表的数据源。
在向图表添加数据源时,数据源可以是单元格范围(如"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."); |
要创建气泡图,必须将 ChartType 设置为 ChartType.BUBBLE,并相应地设置几个额外属性,如气泡大小、值和 X 值。执行以下代码后,将向工作表添加气泡图。
带有数据源的气泡图
// 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设置为ChartType.LINE_WITH_DATA_MARKERS,并相应地设置一些额外的属性,如背景区域、系列标记、值和X值。执行以下代码后,工作表中将添加一个带有数据标记的折线图。
// 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中,每个数据系列由一个Series对象表示,而SeriesCollection对象则作为Series对象的集合。在创建自定义图表时,开发人员可以自由选择不同类型的图表用于不同的数据系列(收集在一个SeriesCollection对象中)。
下面的示例代码演示了如何创建自定义图表。在此示例中,我们将为第一个数据系列使用柱形图,为第二个系列使用折线图。结果是,我们在工作表中添加了一个柱形图,结合了一条折线图。
结合柱形图和折线图的自定义图表
编程示例
// 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."); |