图表中的控件
向图表添加标签控件
标签提供了向用户提供关于电子表格内容的信息的方法。Aspose.Cells 允许您甚至在图表中添加和操作标签。
ShapeCollection类提供了一个名为addLabelInChart的方法,用于向图表添加标签控件。以下是用于该方法的参数列表:
- top – 以图表区域的1/4000单位为垂直偏移的标签。
- left – 以图表区域的1/4000单位为水平偏移的标签。
- height – 标签的高度,以图表区域的1/4000单位为单位。
- width – 标签的宽度,以图表区域的1/4000单位为单位。
该方法返回Label类的对象,其中Label类表示图表中的标签。它具有以下重要成员:
以下示例显示了如何向图表添加标签。该示例使用一个设计文件,其中包含一个图表。我们使用此文件将一个标签插入到图表中。
以下是设计文件的截图。
设计师图表
以下是添加标签到图表的原始代码。执行该代码时会生成以下输出。
图表中添加了一个标签
// 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(AddingLabelControl.class) + "charts/"; | |
String filePath = dataDir + "chart.xls"; | |
Workbook workbook = new Workbook(filePath); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Load the chart from source worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
Label label = chart.getShapes().addLabelInChart(100, 100, 350, 900); | |
label.setText("Write Label here"); | |
label.setPlacement(PlacementType.FREE_FLOATING); | |
label.getFill().getSolidFill().setColor(Color.getChocolate()); | |
// Output the file | |
workbook.save(dataDir + "ALControl_out.xls"); | |
// Print message | |
System.out.println("Label added to chart successfully."); |
将文本框控件添加到图表
在报告中突出显示重要信息的一种方法是使用文本框。例如,输入文本以突出显示公司名称或指示销售额最高的地理区域。ShapeCollection类提供了一个名为addTextBoxInChart的方法,用于向图表添加文本框控件。以下是该方法使用的参数列表:
- top – 文本框与图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
- left – 文本框与图表区域左上角的水平偏移量,单位为图表区域的1/4000。
- height – 文本框的高度,单位为图表区域的1/4000。
- width – 文本框的宽度,单位为图表区域的1/4000。
该方法返回一个 TextBox 类的对象,其中 TextBox 类代表图表中的一个文本框。
以下示例显示了如何向图表添加一个文本框。该示例使用上述包含图表的设计文件。我们使用该文件将一个文本框插入到图表中以显示图表标题。
以下是向图表添加文本框的原始代码。执行该代码时将生成以下输出。
图表中添加了一个文本框
// 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(AddingTextBoxControl.class) + "charts/"; | |
String filePath = dataDir + "chart.xls"; | |
// Create a new Workbook. | |
// Open the existing file. | |
Workbook workbook = new Workbook(filePath); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Load the chart from source worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Add a new textbox to the chart. | |
TextBox txt = chart.getShapes().addTextBoxInChart(100, 100, 850, 2500); | |
txt.setText("Aspose"); | |
txt.getFont().setItalic(true); | |
txt.getFont().setSize(20); | |
txt.getFont().setBold(true); | |
// Get the filformat of the textbox. | |
FillFormat fillformat = txt.getFill(); | |
fillformat.setFillType(FillType.SOLID); | |
fillformat.getSolidFill().setColor(Color.getSilver()); | |
// Get the lineformat type of the textbox. | |
LineFormat lineformat = txt.getLine(); | |
lineformat.setWeight(2); | |
lineformat.setDashStyle(MsoLineDashStyle.SOLID); | |
// Output the file | |
workbook.save(dataDir + "ATBoxControl_out.xls"); | |
// Print message | |
System.out.println("TextBox added to chart successfully."); |
向图表添加图片
Aspose.Cells 允许您将图像插入到图表中。例如,添加一张图片以强调或赋予图表或其内容更多的含义,或者插入一个品牌图片文件。
ShapeCollection 类提供了一个名为 addPictureInChart 的方法,用于向图表添加图片对象。以下是该方法使用的参数列表。
- top – 图片与图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
- left – 图片与图表区域左上角的水平偏移量,单位为图表区域的1/4000。
- stream - 一个包含图像数据的流对象。
- widthScale - 图像宽度的比例值,以百分比表示。
- heightScale - 图像高度的比例值,以百分比表示。
该方法返回一个Picture类的对象,其中Picture类表示图表中的图片对象。
以下示例显示如何向图表中添加图片。该示例利用之前包含图表的设计文件。我们使用该文件向图表中插入一幅图像。
以下是向图表中添加图片的原始代码。执行该代码后生成以下输出。
图片已插入到图表中
// 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(AddingPictureToChart.class) + "charts/"; | |
String filePath = dataDir + "chart.xls"; | |
FileInputStream stream = new FileInputStream(dataDir + "logo.jpg"); | |
Workbook workbook = new Workbook(filePath); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Load the chart from source worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
Picture pic = chart.getShapes().addPictureInChart(50, 50, stream, 40, 40); | |
LineFormat lineformat = pic.getLine(); | |
lineformat.setFillType(FillType.SOLID); | |
lineformat.getSolidFill().setColor(Color.getBlue()); | |
lineformat.setDashStyle(MsoLineDashStyle.DASH_DOT_DOT); | |
// Output the file | |
workbook.save(dataDir + "APToChart_out.xls"); | |
// Print message | |
System.out.println("Picture added to chart successfully."); |
在图表中添加复选框
Aspose.Cells允许您通过使用MsoDrawingType枚举向图表工作表插入复选框。以下示例演示如何向图表工作表添加复选框。
以下图像显示了输出文件中带有复选框的图表工作表。
附上以下代码片段生成的输出文件供您参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// directories | |
String outputDir = Utils.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a chart to the worksheet | |
int index = workbook.getWorksheets().add(SheetType.CHART); | |
Worksheet sheet = workbook.getWorksheets().get(index); | |
sheet.getCharts().addFloatingChart(ChartType.COLUMN, 0, 0, 1024, 960); | |
sheet.getCharts().get(0).getNSeries().add("{1,2,3}", false); | |
// Add checkbox to the chart. | |
sheet.getCharts().get(0).getShapes().addShapeInChart(MsoDrawingType.CHECK_BOX, PlacementType.MOVE, 400, 400, 1000, 600); | |
sheet.getCharts().get(0).getShapes().get(0).setText("CheckBox 1"); | |
// Convert chart to image with additional settings | |
workbook.save(outputDir + "InsertCheckboxInChartSheet_out.xlsx"); |