图表格式设置

设置图表外观

图表类型中,我们简要介绍了Aspose.Cells提供的各种图表类型和图表对象。

在本文中,我们讨论了如何通过设置多种不同属性来自定义图表的外观:

设置图表区域

图表中存在不同类型的区域,Aspose.Cells提供了修改每个区域外观的灵活性。开发人员可以通过改变前景色、背景色和填充格式等方式在区域上应用不同的格式设置。

在下面的示例中,我们在图表的不同区域应用了不同的格式设置。这些区域包括:

执行示例代码后,工作表将添加一个柱形图,如下所示:

具有填充区域的柱形图

todo:image_alt_text

// 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.");

设置图表线条

开发人员还可以在下面的示例中对SeriesCollection的线条或数据标记应用不同类型的样式。执行示例代码将在工作表中添加柱形图,如下所示:

应用线条样式后的柱形图

todo:image_alt_text

// 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主题应用于图表

开发人员可以在下面的示例中为SeriesCollection或其他图表对象应用不同的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设置图表及其坐标轴的标题

todo:image_alt_text

Aspose.Cells还允许开发人员在运行时设置图表及其坐标轴的标题。所有图表及其坐标轴都包含一个Title.setText方法,可用于设置它们的标题,如下示例所示。执行示例代码后,工作表将添加一个柱形图,如下所示:

设置标题后的柱形图

todo:image_alt_text

// 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.");

设置主要网格线

隐藏主要网格线

开发人员可以使用Line对象的setVisible方法来控制主要网格线的可见性。隐藏主要网格线后,工作表中添加的柱形图将具有以下外观:

带有隐藏主要网格线的柱形图

todo:image_alt_text

// 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);

更改主要网格线设置

开发人员不仅可以控制主要网格线的可见性,还可以控制其他属性,包括其颜色等。设置主要网格线的颜色后,工作表中添加的柱形图将具有以下外观:

带有彩色主要网格线的柱形图

todo:image_alt_text

// 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图表的墙壁分为两部分:侧壁和背壁,因此我们必须使用两个Walls对象分别表示它们,您可以使用Chart.getBackWall()Chart.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提供**Chart.getChartObject()**属性来实现这一点。您可以使用其子属性重新调整图表的新**高度**和**宽度**,或者使用新的**X**和**Y**坐标来重新定位它。

修改图表的位置和大小

要更改图表的位置(X、Y坐标)和大小(高度、宽度),请使用以下属性:

  1. Chart.getChartObject().get/setWidth()
  2. Chart.getChartObject().get/setHeight()
  3. Chart.getChartObject().get/setX()
  4. Chart.getChartObject().get/setY()

以下示例解释了上述属性的用法。它加载包含图表的现有工作簿的第一个工作表,然后重新调整大小和重新定位图表,并保存工作簿。

在运行示例代码之前,源文件看起来像这样:

执行示例代码前的图表大小和位置

todo:image_alt_text

执行后,输出文件如下所示:

执行示例代码后的图表大小和位置

todo:image_alt_text

// 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调用。例如,使用**Worksheet.getCharts**属性来获取模板文件中的现有图表集合。

创建图表

以下示例演示了如何创建饼图。稍后我们将操作该图表。代码生成了以下输出。

输入饼图

todo:image_alt_text

// 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(CreateChart.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(50);
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 + "CreateChart_out.xls");
// Print message
System.out.println("Workbook with chart is successfully created.");

操作图表

以下示例展示如何操作现有的图表。在此示例中,我们修改了上面创建的图表。代码生成了以下输出。请注意,图表标题的颜色已从蓝色变为黑色,‘England 30000’ 已更改为 ‘United Kingdom, 30K’。

饼图已被修改

todo:image_alt_text

// 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.");

在设计师模板中操作折线图

在这个例子中,我们将操作一个折线图。我们将向现有图表添加一些数据系列,并改变它们的线条颜色。

首先,看看设计师折线图。

输入折线图

todo:image_alt_text

现在我们使用以下代码操作折线图(包含在 linechart.xls 文件中)。代码生成了以下输出。

操作后的折线图

todo:image_alt_text

// 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可以以前所未有的方式分析信息。它允许用户使用新的数据分析和可视化工具跟踪和突出重要的数据趋势。 Sparklines是迷你图,可以放置在单元格内,以便您可以在同一张表格中查看数据和图表。当Sparklines被正确使用时,数据分析更快捷、更简洁。它们还提供信息的简单视图,避免了拥挤的工作表和繁忙的图表。

Aspose.Cells提供了用于操作电子表格中迷你图的API。

Microsoft Excel 中的迷你图

如何在 Microsoft Excel 2010 中插入迷你图:

  1. 选择要显示迷你图的单元格。为了方便查看,选择数据旁边的单元格。
  2. 在功能区上单击插入,然后在迷你图组中选择

todo:image_alt_text

  1. 选择或输入包含源数据的工作表中的单元格范围。 图表出现。

Sparklines可帮助您查看趋势,例如,软式联赛的胜利或失败记录。 Sparklines甚至可以总结联赛中每支球队整个赛季的情况。

todo:image_alt_text

使用 Aspose.Cells 创建迷你图

开发人员可以使用Aspose.Cells提供的API创建、删除或读取电子表格中的迷你图。通过为给定数据范围添加自定义图形,开发人员可以自由地向选定的单元区域添加不同类型的小图。

下面的示例演示了迷你图功能。该示例显示了如何:

  1. 打开一个简单的模板文件。
  2. 读取工作表的迷你图信息。
  3. 为给定的数据范围向单元区域添加新的迷你图。
  4. 将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(UsingSparklines.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();
// Obtaining the reference of the first worksheet
Worksheet worksheet = worksheets.get(0);
Cells cells = worksheet.getCells();
System.out.println("Sparkline count: " + worksheet.getSparklineGroupCollection().getCount());
for (int i = 0; i < worksheet.getSparklineGroupCollection().getCount(); i++) {
SparklineGroup g = worksheet.getSparklineGroupCollection().get(i);
System.out.println("sparkline group: type:" + g.getType());
for (int j = 0; j < g.getSparklineCollection().getCount(); i++) {
Sparkline gg = g.getSparklineCollection().get(i);
System.out.println("sparkline: row:" + gg.getRow() + ", col:" + gg.getColumn() + ", dataRange:"
+ gg.getDataRange());
}
}
// Add Sparklines
// Define the CellArea D2:D10
CellArea ca = new CellArea();
ca.StartColumn = 4;
ca.EndColumn = 4;
ca.StartRow = 1;
ca.EndRow = 7;
int idx = worksheet.getSparklineGroupCollection().add(SparklineType.COLUMN, "Sheet1!B2:D8", false, ca);
SparklineGroup group = worksheet.getSparklineGroupCollection().get(idx);
// Create CellsColor
CellsColor clr = workbook.createCellsColor();
clr.setColor(Color.getChocolate());
group.setSeriesColor(clr);
workbook.save(dataDir + "UsingSparklines_out.xls");
// Print message
System.out.println("Workbook with chart is created successfully.");

应用3D格式到图表

您可能需要3D图表样式,以便您可以获得与您的情景相符的结果。 Aspose.Cells API提供了相关API,以应用Microsoft Excel 2007 3D格式,正如本文所示。

设置图表的3D格式

下面的完整示例演示了如何创建一个图表并应用Microsoft Excel 2007的3D格式。在执行上述示例代码之后,将向工作表添加一个带有3D效果的柱形图,如下所示。

使用3D格式设置的柱状图

todo:image_alt_text

// 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(Applying3DFormat.class) + "charts/";
// Instantiate a new Workbook
Workbook book = new Workbook();
// Add a Data Worksheet
Worksheet dataSheet = book.getWorksheets().add("DataSheet");
// Add Chart Worksheet
Worksheet sheet = book.getWorksheets().add("MyChart");
// Put some values into the cells in the data worksheet
dataSheet.getCells().get("B1").putValue(1);
dataSheet.getCells().get("B2").putValue(2);
dataSheet.getCells().get("B3").putValue(3);
dataSheet.getCells().get("A1").putValue("A");
dataSheet.getCells().get("A2").putValue("B");
dataSheet.getCells().get("A3").putValue("C");
// Define the Chart Collection
ChartCollection charts = sheet.getCharts();
// Add a Column chart to the Chart Worksheet
int chartSheetIdx = charts.add(ChartType.COLUMN, 5, 0, 25, 15);
// Get the newly added Chart
Chart chart = book.getWorksheets().get(2).getCharts().get(0);
// Set the background/foreground color for PlotArea/ChartArea
chart.getPlotArea().getArea().setBackgroundColor(Color.getWhite());
chart.getChartArea().getArea().setBackgroundColor(Color.getWhite());
chart.getPlotArea().getArea().setForegroundColor(Color.getWhite());
chart.getChartArea().getArea().setForegroundColor(Color.getWhite());
// Hide the Legend
chart.setShowLegend(false);
// Add Data Series for the Chart
chart.getNSeries().add("DataSheet!B1:B3", true);
// Specify the Category Data
chart.getNSeries().setCategoryData("DataSheet!A1:A3");
// Get the Data Series
Series ser = chart.getNSeries().get(0);
// Apply the 3D formatting
ShapePropertyCollection spPr = ser.getShapeProperties();
Format3D fmt3d = spPr.getFormat3D();
// Specify Bevel with its height/width
Bevel bevel = fmt3d.getTopBevel();
bevel.setType(BevelPresetType.CIRCLE);
bevel.setHeight(5);
bevel.setWidth(9);
// Specify Surface material type
fmt3d.setSurfaceMaterialType(PresetMaterialType.WARM_MATTE);
// Specify surface lighting type
fmt3d.setSurfaceLightingType(LightRigType.THREE_POINT);
// Specify lighting angle
fmt3d.setLightingAngle(20);
// Specify Series background/foreground and line color
ser.getArea().setBackgroundColor(Color.getMaroon());
ser.getArea().setForegroundColor(Color.getMaroon());
ser.getBorder().setColor(Color.getMaroon());
// Save the Excel file
book.save(dataDir + "A3DFormat_out.xls");
// Print message
System.out.println("3D format is applied successfully.");

高级主题