使用Sparklines和设置3D格式
使用迷你图
Microsoft Excel 2010可以以前所未有的方式分析信息。它允许用户使用新的数据分析和可视化工具跟踪和突出重要的数据趋势。 Sparklines是迷你图,可以放置在单元格内,以便您可以在同一张表格中查看数据和图表。当Sparklines被正确使用时,数据分析更快捷、更简洁。它们还提供信息的简单视图,避免了拥挤的工作表和繁忙的图表。
Aspose.Cells提供了用于操作电子表格中迷你图的API。
Microsoft Excel 中的迷你图
如何在 Microsoft Excel 2010 中插入迷你图:
- 选择要显示迷你图的单元格。为了方便查看,选择数据旁边的单元格。
- 在功能区上单击插入,然后在迷你图组中选择柱。
- 选择或输入包含源数据的工作表中的单元格范围。图表将出现。
迷你图可帮助您查看趋势,例如垒球联赛的胜负记录。迷你图甚至可以总结联赛中每支队伍整个赛季的情况。
使用 Aspose.Cells 创建迷你图
开发人员可以使用Aspose.Cells提供的API创建、删除或读取表格中的Sparklines。管理Sparklines的类包含在Aspose.Cells.Charts命名空间中,因此在使用这些功能之前,需要导入此命名空间。
通过为给定的数据范围添加自定义图形,开发人员可以自由地向选定的单元区域添加不同类型的迷你图。
下面的示例演示了迷你图功能。该示例显示了如何:
- 打开一个简单的模板文件。
- 读取工作表的迷你图信息。
- 为给定的数据范围向单元区域添加新的迷你图。
- 将 Excel 文件保存到磁盘。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Instantiate a Workbook | |
// Open a template file | |
Workbook book = new Workbook("Source.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Use the following lines if you need to read the Sparklines | |
// Read the Sparklines from the template file (if it has) | |
for(SparklineGroup g : (Iterable<SparklineGroup>)sheet.getSparklineGroupCollection()) | |
{ | |
// Display the Sparklines group information e.g type, number of sparklines items | |
System.out.println("sparkline group: type:" + g.getType() + ", sparkline items count:" + g.getSparklineCollection().getCount()); | |
for (Sparkline s: (Iterable<Sparkline>)g.getSparklineCollection()) | |
{ | |
// Display the individual Sparkines and the data ranges | |
System.out.println("sparkline: row:" + s.getRow() + ", col:" + s.getColumn() + ", dataRange:" + s.getDataRange()); | |
} | |
} | |
// Add Sparklines | |
// Define the CellArea D2:D10 | |
CellArea ca = new CellArea(); | |
ca.StartColumn = 4; | |
ca.EndColumn = 4; | |
ca.StartRow = 1; | |
ca.EndRow = 7; | |
// Add new Sparklines for a data range to a cell area | |
int idx = sheet.getSparklineGroupCollection().add(SparklineType.COLUMN, "Sheet1!B2:D8", false, ca); | |
SparklineGroup group = sheet.getSparklineGroupCollection().get(idx); | |
// Create CellsColor | |
CellsColor clr = book.createCellsColor(); | |
clr.setColor(Color.getOrange()); | |
group.setSeriesColor (clr); | |
// Save the workbook | |
book.save("output.xlsx"); |
设置 3D 格式
您可能需要 3D 图表样式,以便可以为您的特定情景获得恰当的结果。Aspose.Cells 提供了相关的 API 来应用 Microsoft Excel 2007 的 3D 格式。
下面给出了一个完整的示例,演示如何创建图表并应用 Microsoft Excel 2007 的 3D 格式。执行示例代码后,工作表中将添加一个带有 3D 效果的柱状图。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// 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 3-D 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(2); | |
bevel.setWidth(5); | |
// Specify Surface material type | |
fmt3d.setSurfaceMaterialType (PresetMaterialType.WARM_MATTE); | |
// Specify surface lighting type | |
fmt3d.setSurfaceLightingType(LightRigType.THREE_POINT); | |
// Specify lighting angle | |
fmt3d.setLightingAngle(0); | |
// 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("3d_format.out.xlsx"); |