图表中的形状

向图表添加标签控件

标签提供了一种向用户提供有关电子表格内容信息的方式。 Aspose.Cells for Python via .NET 允许你向图表中添加和操作标签。

aspose.cells.drawing.ShapeCollection类提供了一个名为add_label_in_chart的方法,用于向图表添加标签控件。以下是该方法使用的参数列表:

  • top – 以图表区域的1/4000单位为垂直偏移的标签。
  • left – 以图表区域的1/4000单位为水平偏移的标签。
  • height – 标签的高度,以图表区域的1/4000单位为单位。
  • width – 标签的宽度,单位为图表区域的1/4000。

该方法返回aspose.cells.drawing.Label对象。Label类代表图表中的标签,具有一些重要成员:

  • text(属性)– 指定标签的标题字符串。
  • fill(属性)– 指定填充颜色属性。

以下示例演示如何向图表添加标签。示例使用了一个设计文件(exp_piechart.xls),其中包含一个图表。我们使用此文件向图表插入标签。以下是添加标签到图表的原始代码。执行该代码时生成以下输出。

from aspose.cells import Workbook
from aspose.cells.drawing import PlacementType
# 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(".")
# Open the existing file.
workbook = Workbook(dataDir + "chart.xls")
# Get the designer chart in the second sheet.
sheet = workbook.worksheets[1]
chart = sheet.charts[0]
# Add a new label to the chart.
label = chart.shapes.add_label_in_chart(100, 100, 350, 900)
# Set the caption of the label.
label.text = "A Label In Chart"
# Set the Placement Type, the way the
# Label is attached to the cells.
label.placement = PlacementType.FREE_FLOATING
# Save the excel file.
workbook.save(dataDir + "chart.out.xls")

将文本框控件添加到图表

在报告中突出显示重要信息的一种方法是使用文本框。例如,输入文本以突出显示公司名称或指示销售额最高的地理区域。aspose.cells.drawing.ShapeCollection类提供了一个名为add_text_box_in_chart的方法,用于向图表添加文本框控件。以下是该方法使用的参数列表:

  • top – 文本框与图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
  • left – 文本框距离图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
  • height – 文本框的高度,单位为图表区域的1/4000。
  • width – 文本框的宽度,单位为图表区域的1/4000。

该方法返回一个 aspose.cells.drawing.TextBox 对象。TextBox 类表示图表中的文本框。

以下示例显示了如何向图表添加文本框。该示例使用之前的设计文件(exp_piechart.xls),其中包含一个图表。我们使用这个文件向图表中插入文本框以显示图表标题。以下是添加文本框到图表的原始代码。

from aspose.cells import Workbook
from aspose.cells.drawing import MsoLineDashStyle
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(".")
# Open the existing file.
workbook = Workbook(dataDir + "chart.xls")
# Get the designer chart in the second sheet.
sheet = workbook.worksheets[1]
chart = sheet.charts[0]
# Add a new textbox to the chart.
textbox0 = chart.shapes.add_text_box_in_chart(100, 1100, 350, 2550)
# Fill the text.
textbox0.text = "Sales By Region"
# Get the textbox text frame.
# Aspose.Cells.Drawing.MsoTextFrame textframe0 = textbox0.TextFrame;
# Set the textbox to adjust it according to its contents.
# textframe0.AutoSize = true;
# Set the font color.
textbox0.font.color = Color.maroon
# Set the font to bold.
textbox0.font.is_bold = True
# Set the font size.
textbox0.font.size = 14
# Set font attribute to italic.
textbox0.font.is_italic = True
# Get the filformat of the textbox.
fillformat = textbox0.fill
# Get the lineformat type of the textbox.
lineformat = textbox0.line
# Set the line weight.
lineformat.weight = 2.0
# Set the dash style to solid.
lineformat.dash_style = MsoLineDashStyle.SOLID
# Save the excel file.
workbook.save(dataDir + "chart.out.xls")

向图表添加图片

Aspose.Cells for Python via .NET 允许你在图表中插入图片。例如,添加图片以强调或赋予图表内容更多意义,或插入品牌图像文件。

aspose.cells.drawing.ShapeCollection 类提供了一个名为 add_picture_in_chart 的方法,用于向图表添加图片对象。以下是该方法使用的参数列表。

  • top – 图片与图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
  • left – 图片与图表区域左上角的水平偏移量,单位为图表区域的1/4000。
  • stream - 一个包含图像数据的流对象。
  • widthScale - 图像宽度的比例值,以百分比表示。
  • heightScale - 图像高度的比例值,以百分比表示。

该方法返回一个 aspose.cells.drawing.Picture 对象。Picture 类表示图表中的图片对象。

以下示例显示了如何向图表添加图片。该示例利用之前的设计文件(exp_piechart.xls),其中包含一个图表。我们使用这个文件向图表中插入图片。以下是添加图片到图表的原始代码。

from aspose.cells import Workbook
from aspose.cells.drawing import MsoLineDashStyle
# 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(".")
# Open the existing file.
workbook = Workbook(dataDir + "chart.xls")
# Get an image file to the stream.
stream = open(dataDir + "logo.jpg", "rb")
# Get the designer chart in the second sheet.
sheet = workbook.worksheets[0]
chart = sheet.charts[0]
# Add a new picture to the chart.
pic0 = chart.shapes.add_picture_in_chart(50, 50, stream, 40, 40)
# Get the lineformat type of the picture.
lineformat = pic0.line
# Set the dash style.
lineformat.dash_style = MsoLineDashStyle.SOLID
# Set the line weight.
lineformat.weight = 4.0
# Save the excel file.
workbook.save(dataDir + "chart.out.xls")

在图表中添加复选框

Aspose.Cells for Python via .NET 允许你通过 MsoDrawingType 枚举在图表工作表中插入复选框。以下示例演示如何为图表工作表添加复选框。

以下图像显示了输出文件中带有复选框的图表工作表。

todo:image_alt_text

以下代码片段生成的输出文件已附上,供您参考。

from aspose.cells import SheetType, Workbook
from aspose.cells.charts import ChartType
from aspose.cells.drawing import MsoDrawingType, PlacementType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiating a Workbook object
workbook = Workbook()
# Adding a chart to the worksheet
index = workbook.worksheets.add(SheetType.CHART)
sheet = workbook.worksheets[index]
sheet.charts.add_floating_chart(ChartType.COLUMN, 0, 0, 1024, 960)
sheet.charts[0].n_series.add("{1,2,3}", False)
# Add checkbox to the chart.
sheet.charts[0].shapes.add_shape_in_chart(MsoDrawingType.CHECK_BOX, PlacementType.MOVE, 400, 400, 1000, 600)
sheet.charts[0].shapes[0].text = "CheckBox 1"
# Save the excel file.
workbook.save(outputDir + "InsertCheckboxInChartSheet_out.xlsx")

高级主题