图表中的形状

向图表添加标签控件

标签提供了一种向用户提供有关电子表格内容的信息的方法。 Aspose.Cells 允许您甚至在图表中添加和操作标签。

Aspose.Cells.Drawing.ShapeCollection类提供了一个名为在图表中添加标签用于向图表添加标签控件。下面是用于该方法的参数列表:

  • 最佳– 标签距左上角的垂直偏移量,以图表区域的 1/4000 为单位。
  • 剩下– 标签距左上角的垂直偏移量,以图表区域的 1/4000 为单位。
  • 高度– 标签的高度,以图表区域的 1/4000 为单位。
  • 宽度 – 标签的宽度,以图表区域的 1/4000 为单位。

该方法返回Aspose.Cells.Drawing.Label目的。这标签类代表图表中的标签。它有一些重要的成员:

  • 文本(property) – 指定标签的标题字符串。
  • 充满 (property) – 指定填充颜色属性。

以下示例显示如何向图表添加标签。该示例使用设计器文件 (exp_piechart.xls) 里面有一张图表。我们使用此文件将标签插入图表中。下面是向图表添加标签的原始代码。执行代码时会生成以下输出。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing file.
Workbook workbook = new Workbook(dataDir + "chart.xls");
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[1];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Add a new label to the chart.
Aspose.Cells.Drawing.Label label = chart.Shapes.AddLabelInChart(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 = Aspose.Cells.Drawing.PlacementType.FreeFloating;
// Save the excel file.
workbook.Save(dataDir + "chart.out.xls");

将文本框控件添加到图表

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

  • 最佳 – 文本框距左上角的垂直偏移量,以图表区域的 1/4000 为单位。
  • 剩下 – 文本框距左上角的垂直偏移量,以图表区域的 1/4000 为单位。
  • 高度– 文本框的高度,以图表区域的 1/4000 为单位。
  • 宽度 – 文本框的宽度,以图表区域的 1/4000 为单位。

该方法返回Aspose.Cells.Drawing.TextBox目的。这文本框类表示图表中的文本框。

以下示例显示如何向图表添加文本框。该示例使用以前的设计器文件 (exp_piechart.xls) 里面有一张图表。我们使用此文件在图表中插入一个文本框以显示图表标题。下面是将文本框添加到图表的原始代码。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing file.
Workbook workbook = new Workbook(dataDir + "chart.xls");
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[1];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Add a new textbox to the chart.
Aspose.Cells.Drawing.TextBox textbox0 = chart.Shapes.AddTextBoxInChart(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.IsBold = true;
// Set the font size.
textbox0.Font.Size = 14;
// Set font attribute to italic.
textbox0.Font.IsItalic = true;
// Get the filformat of the textbox.
Aspose.Cells.Drawing.FillFormat fillformat = textbox0.Fill;
// Get the lineformat type of the textbox.
Aspose.Cells.Drawing.LineFormat lineformat = textbox0.Line;
// Set the line weight.
lineformat.Weight = 2;
// Set the dash style to solid.
lineformat.DashStyle = Aspose.Cells.Drawing.MsoLineDashStyle.Solid;
// Save the excel file.
workbook.Save(dataDir + "chart.out.xls");

添加图片到图表

Aspose.Cells 允许您将图像插入图表。例如,添加图片以强调或赋予图表或其内容更多含义,或插入品牌图像文件。

Aspose.Cells.Drawing.ShapeCollection类提供了一个名为添加画中画用于向图表添加图片对象。以下是该方法使用的参数列表:

  • 最佳– 图片距左上角的垂直偏移量,以图表区域的 1/4000 为单位。
  • 剩下– 图片距左上角的垂直偏移量,以图表区域的 1/4000 为单位。
  • 溪流– 包含图像数据的流对象。
  • 宽度比例– 图像宽度的比例,一个百分比值。
  • 高度比例– 图像高度的比例,一个百分比值。

该方法返回一个Aspose.Cells.Drawing.Picture目的。这图片类代表图表中的图片对象。

下面的示例演示如何将图片添加到图表中。该示例利用以前的设计器文件 (exp_piechart.xls) 里面有一张图表。我们使用此文件将图像插入图表中。下面是将图片添加到图表的原始代码。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing file.
Workbook workbook = new Workbook(dataDir + "chart.xls");
// Get an image file to the stream.
FileStream stream = new FileStream(dataDir + "logo.jpg", FileMode.Open, FileAccess.Read);
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[0];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Add a new picture to the chart.
Aspose.Cells.Drawing.Picture pic0 = chart.Shapes.AddPictureInChart(50, 50, stream, 40, 40);
// Get the lineformat type of the picture.
Aspose.Cells.Drawing.LineFormat lineformat = pic0.Line;
// Set the dash style.
lineformat.DashStyle = Aspose.Cells.Drawing.MsoLineDashStyle.Solid;
// Set the line weight.
lineformat.Weight = 4;
// Save the excel file.
workbook.Save(dataDir + "chart.out.xls");

在图表中添加复选框

Aspose.Cells 允许您使用Mso绘图类型枚举。以下示例演示了向图表工作表添加复选框。

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

待办事项:图片_替代_文本

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a chart to the worksheet
int index = workbook.Worksheets.Add(SheetType.Chart);
Worksheet sheet = workbook.Worksheets[index];
sheet.Charts.AddFloatingChart(ChartType.Column, 0, 0, 1024, 960);
sheet.Charts[0].NSeries.Add("{1,2,3}", false);
// Add checkbox to the chart.
sheet.Charts[0].Shapes.AddShapeInChart(MsoDrawingType.CheckBox, PlacementType.Move, 400, 400, 1000, 600);
sheet.Charts[0].Shapes[0].Text = "CheckBox 1";
// Save the excel file.
workbook.Save(outputDir + "InsertCheckboxInChartSheet_out.xlsx");

推进主题