图表格式
设置图表外观
在图表类型,我们简单介绍了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 documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingChartArea.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the newly added worksheet by passing its | |
// sheet index | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
// Adding a sample value to "A1" cell | |
cells.get("A1").setValue(50); | |
// Adding a sample value to "A2" cell | |
cells.get("A2").setValue(100); | |
// Adding a sample value to "A3" cell | |
cells.get("A3").setValue(150); | |
// Adding a sample value to "B1" cell | |
cells.get("B1").setValue(60); | |
// Adding a sample value to "B2" cell | |
cells.get("B2").setValue(32); | |
// Adding a sample value to "B3" cell | |
cells.get("B3").setValue(50); | |
// Adding a chart to the worksheet | |
ChartCollection charts = worksheet.getCharts(); | |
// Accessing the instance of the newly added chart | |
int chartIndex = charts.add(ChartType.COLUMN, 5, 0, 15, 7); | |
Chart chart = charts.get(chartIndex); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell | |
SeriesCollection nSeries = chart.getNSeries(); | |
nSeries.add("A1:B3", true); | |
// Setting the foreground color of the plot area | |
ChartFrame plotArea = chart.getPlotArea(); | |
Area area = plotArea.getArea(); | |
area.setForegroundColor(Color.getBlue()); | |
// Setting the foreground color of the chart area | |
ChartArea chartArea = chart.getChartArea(); | |
area = chartArea.getArea(); | |
area.setForegroundColor(Color.getYellow()); | |
// Setting the foreground color of the 1st NSeries area | |
Series aSeries = nSeries.get(0); | |
area = aSeries.getArea(); | |
area.setForegroundColor(Color.getRed()); | |
// Setting the foreground color of the area of the 1st NSeries point | |
ChartPointCollection chartPoints = aSeries.getPoints(); | |
ChartPoint point = chartPoints.get(0); | |
point.getArea().setForegroundColor(Color.getCyan()); | |
// Save the Excel file | |
workbook.save(dataDir + "SettingChartArea_out.xls"); | |
// Print message | |
System.out.println("ChartArea is settled successfully."); |
设置图表线
开发人员还可以在线条或数据标记上应用不同类型的样式系列合集如下例所示。执行示例代码会在工作表中添加一个柱形图,如下所示:
应用线条样式后的柱形图
// 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(SettingChartLines.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the newly added worksheet by passing its | |
// sheet index | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
// Adding a chart to the worksheet | |
ChartCollection charts = worksheet.getCharts(); | |
Chart chart = charts.get(0); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell | |
SeriesCollection nSeries = chart.getNSeries(); | |
nSeries.add("A1:B3", true); | |
Series aSeries = nSeries.get(0); | |
Line line = aSeries.getSeriesLines(); | |
line.setStyle(LineType.DOT); | |
// Applying a triangular marker style on the data markers of an NSeries | |
aSeries.getMarker().setMarkerStyle(ChartMarkerType.TRIANGLE); | |
// Setting the weight of all lines in an NSeries to medium | |
aSeries = nSeries.get(1); | |
line = aSeries.getSeriesLines(); | |
line.setWeight(WeightType.MEDIUM_LINE); | |
// Save the Excel file | |
workbook.save(dataDir + "SettingChartLines_out.xls"); | |
// Print message | |
System.out.println("ChartArea is settled successfully."); |
将 Microsoft Excel 2007/2010 主题应用于图表
开发人员可以应用不同的 Microsoft Excel 主题和颜色到系列合集或其他图表对象,如下例所示。
// 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(ApplyingThemes.class) + "charts/"; | |
// Instantiate the workbook to open the file that contains a chart | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get the first chart in the sheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Specify the FilFormat's type to Solid Fill of the first series | |
chart.getNSeries().get(0).getArea().getFillFormat().setFillType(FillType.SOLID); | |
// Get the CellsColor of SolidFill | |
CellsColor cc = chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().getCellsColor(); | |
// Create a theme in Accent style | |
cc.setThemeColor(new ThemeColor(ThemeColorType.ACCENT_6, 0.6)); | |
// Apply the them to the series | |
chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().setCellsColor(cc); | |
// Save the Excel file | |
workbook.save(dataDir + "AThemes_out.xlsx"); |
设置图表或坐标轴的标题
您可以使用 Microsoft Excel 在所见即所得环境中设置图表的标题及其轴,如下所示。
使用 Microsoft Excel 设置图表及其轴的标题
Aspose.Cells 还允许开发人员在运行时设置图表的标题及其轴。所有图表及其轴都包含一个标题.setText可用于设置其标题的方法,如下例所示。执行示例代码后,将在工作表中添加一个柱形图,如下所示:
设置标题后的柱状图
// 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(SettingTitlesAxes.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the newly added worksheet by passing its | |
// sheet index | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
// Adding a chart to the worksheet | |
ChartCollection charts = worksheet.getCharts(); | |
// Accessing the instance of the newly added chart | |
int chartIndex = charts.add(ChartType.COLUMN, 5, 0, 15, 7); | |
Chart chart = charts.get(chartIndex); | |
// Setting the title of a chart | |
Title title = chart.getTitle(); | |
title.setText("ASPOSE"); | |
// Setting the font color of the chart title to blue | |
Font font = title.getFont(); | |
font.setColor(Color.getBlue()); | |
// Setting the title of category axis of the chart | |
Axis categoryAxis = chart.getCategoryAxis(); | |
title = categoryAxis.getTitle(); | |
title.setText("Category"); | |
// Setting the title of value axis of the chart | |
Axis valueAxis = chart.getValueAxis(); | |
title = valueAxis.getTitle(); | |
title.setText("Value"); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell | |
SeriesCollection nSeries = chart.getNSeries(); | |
nSeries.add("A1:B3", true); | |
// Setting the foreground color of the plot area | |
ChartFrame plotArea = chart.getPlotArea(); | |
Area area = plotArea.getArea(); | |
area.setForegroundColor(Color.getBlue()); | |
// Setting the foreground color of the chart area | |
ChartArea chartArea = chart.getChartArea(); | |
area = chartArea.getArea(); | |
area.setForegroundColor(Color.getYellow()); | |
// Setting the foreground color of the 1st NSeries area | |
Series aSeries = nSeries.get(0); | |
area = aSeries.getArea(); | |
area.setForegroundColor(Color.getRed()); | |
// Setting the foreground color of the area of the 1st NSeries point | |
ChartPointCollection chartPoints = aSeries.getPoints(); | |
ChartPoint point = chartPoints.get(0); | |
point.getArea().setForegroundColor(Color.getCyan()); | |
// Save the Excel file | |
workbook.save(dataDir + "SettingTitlesAxes_out.xls"); | |
// Print message | |
System.out.println("Chart Title is changed successfully."); |
设置主要网格线
隐藏主要网格线
开发人员可以通过使用设置可见的方法线目的。隐藏主要网格线后,添加到工作表的柱形图具有以下外观:
隐藏主要网格线的柱形图
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Hiding the major gridlines of value axis | |
Axis valueAxis = chart.getValueAxis(); | |
Line majorGridLines = valueAxis.getMajorGridLines(); | |
majorGridLines.setVisible(false); |
更改主要网格线设置
开发人员不仅可以控制主要网格线的可见性,还可以控制其他属性,包括颜色等。设置主要网格线的颜色后,添加到工作表的柱形图将具有以下外观:
带有彩色主要网格线的柱形图
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Setting the color of major gridlines of value axis to silver | |
Axis categoryAxis = chart.getCategoryAxis(); | |
categoryAxis.getMajorGridLines().setColor(Color.getSilver()); |
设置背墙和侧墙的边框
自Microsoft Excel 2007发布以来,3D图表的墙被分为侧墙和后墙两部分,所以我们必须使用两个墙壁对象来分别表示它们,您可以使用图表.getBackWall()和图表.getSideWall().
下面给出的示例显示了如何使用不同的属性设置侧壁的边框。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Get the side wall border line | |
Line sideLine = chart.getSideWall().getBorder(); | |
// Make it visible | |
sideLine.setVisible(true); | |
// Set the solid line | |
sideLine.setStyle(LineType.SOLID); | |
// Set the line width | |
sideLine.setWeight(10); | |
// Set the color | |
sideLine.setColor(Color.getBlack()); |
更改图表位置和大小
有时,您想要更改工作表中新图表或现有图表的位置或大小。 Aspose.Cells 提供了图表.getChartObject()财产来实现这一点。您可以使用它的子属性来重新调整图表的大小高度和宽度或用新的重新定位X 和Y 坐标。
修改图表的位置和大小
要更改图表的位置(X、Y 坐标)和大小(高度、宽度),请使用以下属性:
- Chart.getChartObject().get/setWidth() 方法
- 图表.getChartObject().get/setHeight()
- 图表.getChartObject().get/setX()
- 图表.getChartObject().get/setY()
下面的例子解释了上述属性的用法。它加载现有工作簿,该工作簿在其第一个工作表中包含一个图表。然后重新调整图表的大小和位置并保存工作簿。
在示例代码执行之前,源文件如下所示:
示例代码执行前的图表大小和位置
执行后,输出文件如下所示:
示例代码执行后的图表大小和位置
// 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(ChangeChartPositionAndSize.class) + "charts/"; | |
String filePath = dataDir + "book1.xls"; | |
Workbook workbook = new Workbook(filePath); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Load the chart from source worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Resize the chart | |
chart.getChartObject().setWidth(400); | |
chart.getChartObject().setHeight(300); | |
// Reposition the chart | |
chart.getChartObject().setX(250); | |
chart.getChartObject().setY(150); | |
// Output the file | |
workbook.save(dataDir + "ChangeChartPositionAndSize_out.xls"); | |
// Print message | |
System.out.println("Position and Size of Chart is changed successfully."); |
操纵设计师图表
有时您可能需要操作或修改设计器模板文件中的图表。 Aspose.Cells 完全支持使用其内容和元素来操作设计器图表。可以准确保存数据、图表内容、背景图像和格式。
在模板文件中操作设计器图表
要在模板文件中操作设计器图表,请使用所有与图表相关的 API 调用。例如,使用工作表.getCharts属性以获取模板文件中的现有图表集合。
创建图表
以下示例显示了如何创建饼图。稍后我们将操作此图表。以下输出由代码生成。
输入饼图
操纵图表
以下示例显示如何操作现有图表。在这个例子中,我们修改上面创建的图表。以下输出由代码生成。请注意,图表标题的颜色已从蓝色变为黑色,并且“England 30000”已更改为“United Kingdom, 30K”。
饼图已修改
// 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(ModifyPieChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "ModifyCharts.xlsx"); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(1); | |
// Load the chart from source worksheet | |
Chart chart = sheet.getCharts().get(0); | |
DataLabels datalabels = chart.getNSeries().get(0).getPoints().get(0).getDataLabels(); | |
datalabels.setText("aspose"); | |
// Saving the Excel file | |
workbook.save(dataDir + "ModifyPieChart_out.xls"); | |
// Print message | |
System.out.println("Pie chart is successfully modified."); |
在设计器模板中操作折线图
在这个例子中,我们将操作一个折线图。我们将向现有图表添加一些数据系列并更改它们的线条颜色。
首先,看一下设计器折线图。
输入折线图
现在我们操作折线图(包含在折线图.xls文件)使用以下代码。以下输出由代码生成。
操纵的折线图
// 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(ModifyLineChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(0); | |
// Load the chart from source worksheet | |
Chart chart = sheet.getCharts().get(0); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell to "B3" | |
SeriesCollection serieses = chart.getNSeries(); | |
serieses.add("{20,40,90}", true); | |
serieses.add("{110,70,220}", true); | |
// Saving the Excel file | |
workbook.save(dataDir + "ModifyLineChart_out.xls"); | |
// Print message | |
System.out.println("Line chart is successfully modified."); |
使用迷你图
Microsoft Excel 2010 可以以比以往更多的方式分析信息。它允许用户使用新的数据分析和可视化工具跟踪和突出重要的数据趋势。迷你图是可以放置在单元格内的迷你图表,以便您可以在同一个表格中查看数据和图表。当正确使用迷你图时,数据分析会更快、更切题。它们还提供了一个简单的信息视图,避免了带有大量繁忙图表的过度拥挤的工作表。
Aspose.Cells 提供了一个 API 用于在电子表格中操作迷你图。
Microsoft Excel 中的迷你图
要在 Microsoft Excel 2010 中插入迷你图:
- 选择要显示迷你图的单元格。为了便于查看,请选择数据旁边的单元格。
- 点击插入在功能区上,然后选择柱子在里面迷你图团体。
- 选择或输入工作表中包含源数据的单元格范围。 图表出现。
例如,迷你图可帮助您查看趋势或垒球联赛的输赢记录。迷你图甚至可以总结联盟中每支球队的整个赛季。
使用 Aspose.Cells 的迷你图
开发人员可以使用 API 提供的 API 创建、删除或读取迷你图(在模板文件中)。通过为给定数据范围添加自定义图形,开发人员可以自由地将不同类型的微型图表添加到选定的单元格区域。
下面的示例演示了迷你图功能。该示例显示了如何:
- 打开一个简单的模板文件。
- 阅读工作表的迷你图信息。
- 将给定数据范围的新迷你图添加到单元格区域。
- 将 Excel 文件保存到磁盘。
将 3D 格式应用于图表
您可能需要 3D 图表样式,以便您可以只获得场景的结果。 Aspose.Cells API 提供相关 API 以应用 Microsoft Excel 2007 3D 格式,如本文中所演示。
为图表设置 3D 格式
下面给出了一个完整的示例来演示如何创建图表和应用 Microsoft Excel 2007 3D 格式。执行上述示例代码后,工作表中将添加一个柱形图(具有 3D 效果),如下所示。
具有 3D 格式的柱形图