使用Sparklines和设置3D格式
使用迷你图
Microsoft Excel 2010可以以前所未有的方式分析信息。它允许用户使用新的数据分析和可视化工具跟踪和突出重要的数据趋势。 Sparklines是迷你图,可以放置在单元格内,以便您可以在同一张表格中查看数据和图表。当Sparklines被正确使用时,数据分析更快捷、更简洁。它们还提供信息的简单视图,避免了拥挤的工作表和繁忙的图表。
Aspose.Cells for Python via .NET提供了操作电子表格中迷你图的API。
Microsoft Excel 中的迷你图
如何在 Microsoft Excel 2010 中插入迷你图:
- 选择要显示迷你图的单元格。为了方便查看,选择数据旁边的单元格。
- 在功能区上单击插入,然后在迷你图组中选择柱。
- 选择或输入包含源数据的工作表中的单元格范围。图表将出现。
迷你图可帮助您查看趋势,例如垒球联赛的胜负记录。迷你图甚至可以总结联赛中每支队伍整个赛季的情况。
使用Aspose.Cells for Python via .NET制作迷你图
开发者可以使用Aspose.Cells for Python via .NET的API创建、删除或读取模板文件中的迷你图(在模板文件中)。管理迷你图的类包含在aspose.cells.charts命名空间中,因此在使用这些功能之前需要导入该命名空间。
通过为给定的数据范围添加自定义图形,开发人员可以自由地向选定的单元区域添加不同类型的迷你图。
下面的示例演示了迷你图功能。该示例显示了如何:
- 打开一个简单的模板文件。
- 读取工作表的迷你图信息。
- 为给定的数据范围向单元区域添加新的迷你图。
- 将 Excel 文件保存到磁盘。
from aspose.cells import CellArea, Workbook | |
from aspose.cells.charts import SparklineType | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a Workbook | |
# Open a template file | |
book = Workbook(dataDir + "Book1.xlsx") | |
# Get the first worksheet | |
sheet = book.worksheets[0] | |
# Use the following lines if you need to read the Sparklines | |
# Read the Sparklines from the template file (if it has) | |
for g in sheet.sparkline_groups: | |
# Display the Sparklines group information e.g type, number of sparklines items | |
print("sparkline group: type:" + str(g.type) + ", sparkline items count:" + str(len(g.sparklines))) | |
for s in g.sparklines: | |
# Display the individual Sparkines and the data ranges | |
print("sparkline: row:" + str(s.row) + ", col:" + str(s.column) + ", dataRange:" + s.data_range) | |
# Add Sparklines | |
# Define the CellArea D2:D10 | |
ca = CellArea() | |
ca.start_column = 4 | |
ca.end_column = 4 | |
ca.start_row = 1 | |
ca.end_row = 7 | |
# Add new Sparklines for a data range to a cell area | |
idx = sheet.sparkline_groups.add(SparklineType.COLUMN, "Sheet1!B2:D8", False, ca) | |
group = sheet.sparkline_groups[idx] | |
# Create CellsColor | |
clr = book.create_cells_color() | |
clr.color = Color.orange | |
group.series_color = clr | |
# Save the excel file | |
book.save(dataDir + "Book1.out.xlsx") |
设置 3D 格式
您可能需要3D图表样式,以便专门获取适合您的场景的结果。Aspose.Cells for Python via .NET提供了相关API,用于应用Microsoft Excel 2007 3D格式。
下面给出了一个完整的示例,演示如何创建图表并应用 Microsoft Excel 2007 的 3D 格式。执行示例代码后,工作表中将添加一个带有 3D 效果的柱状图。
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
from aspose.cells.drawing import BevelPresetType, LightRigType, PresetMaterialType | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiate a new Workbook | |
book = Workbook() | |
# Add a Data Worksheet | |
dataSheet = book.worksheets.add("DataSheet") | |
# Add Chart Worksheet | |
sheet = book.worksheets.add("MyChart") | |
# Put some values into the cells in the data worksheet | |
dataSheet.cells.get("B1").put_value(1) | |
dataSheet.cells.get("B2").put_value(2) | |
dataSheet.cells.get("B3").put_value(3) | |
dataSheet.cells.get("A1").put_value("A") | |
dataSheet.cells.get("A2").put_value("B") | |
dataSheet.cells.get("A3").put_value("C") | |
# Define the Chart Collection | |
charts = sheet.charts | |
# Add a Column chart to the Chart Worksheet | |
chartSheetIdx = charts.add(ChartType.COLUMN, 5, 0, 25, 15) | |
# Get the newly added Chart | |
chart = book.worksheets[2].charts[0] | |
# Set the background/foreground color for PlotArea/ChartArea | |
chart.plot_area.area.background_color = Color.white | |
chart.chart_area.area.background_color = Color.white | |
chart.plot_area.area.foreground_color = Color.white | |
chart.chart_area.area.foreground_color = Color.white | |
# Hide the Legend | |
chart.show_legend = False | |
# Add Data Series for the Chart | |
chart.n_series.add("DataSheet!B1:B3", True) | |
# Specify the Category Data | |
chart.n_series.category_data = "DataSheet!A1:A3" | |
# Get the Data Series | |
ser = chart.n_series[0] | |
# Apply the 3-D formatting | |
spPr = ser.shape_properties | |
fmt3d = spPr.format_3d | |
# Specify Bevel with its height/width | |
bevel = fmt3d.top_bevel | |
bevel.type = BevelPresetType.CIRCLE | |
bevel.height = 2.0 | |
bevel.width = 5.0 | |
# Specify Surface material type | |
fmt3d.surface_material_type = PresetMaterialType.WARM_MATTE | |
# Specify surface lighting type | |
fmt3d.surface_lighting_type = LightRigType.THREE_POINT | |
# Specify lighting angle | |
fmt3d.lighting_angle = 20.0 | |
# Specify Series background/foreground and line color | |
ser.area.background_color = Color.maroon | |
ser.area.foreground_color = Color.maroon | |
ser.border.color = Color.maroon | |
# Save the Excel file | |
book.save(dataDir + "3d_format.out.xlsx") |