Controls in Charts
Adding Label Control to the Chart
Labels provide a means for giving information to users about a spreadsheet’s content. Aspose.Cells allows you to add and manipulate labels even into charts.
The ShapeCollection class provides a method named addLabelInChart, used to add a label control to a chart. Below is a list of the parameters used for the method:
- top – the vertical offset of the label from the upper left corner in units of 1/4000 of the chart area.
- left – the vertical offset of the label from the upper left corner in units of 1/4000 of the chart area.
- height – the height of the label, in units of 1/4000 of the chart area.
- width – the width of the label, in units of 1/4000 of the chart area.
The method returns an object of the Label class, where the Label class represents a label in the chart. It has some important members as detailed below:
- Text property specifies a label’s caption string.
- Fill property specifies the fill color attributes.
The following example shows how to add a label to the chart. The example uses a designer file which has a chart in it. We use this file to insert a label into the chart.
Below is a screenshot of the designer file.
The designer chart
Below is the original code for adding a label to the chart. The following output is generated when executing the code.
A label is added in the chart
// 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."); |
Adding TextBox Control to the Chart
One way to highlight important information in a report is to use a text box. For example, enter text to highlight the company name or to indicate the geographic region with the highest sales. The ShapeCollection class provides a method named addTextBoxInChart, which is used to add a text box control to a chart. Following is the parameters list used for the method:
- top – the vertical offset of the text box from the upper left corner in units of 1/4000 of the chart area.
- left – the vertical offset of the text box from the upper left corner in units of 1/4000 of the chart area.
- height – the height of text box, in units of 1/4000 of the chart area.
- width – the width of the text box, in units of 1/4000 of the chart area.
The method returns an object of the TextBox class where the TextBox class represents a text box in the chart.
The following example shows how to add a text box to a chart. The example uses the previous designer file which has a chart in it. We use this file to insert a text box into the chart to show the chart title.
Below is the original code for adding a text box to the chart. The following output is generated when executing the code.
A text box is added in the chart
// 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."); |
Adding Picture to the Chart
Aspose.Cells allows you to insert images into a chart. For example, add a picture to emphasize or give more meaning to a chart or its contents, or insert a brand image file.
The ShapeCollection class provides a method named addPictureInChart, which is used to add a picture object to the chart. Following is the parameters list used for the method:
- top – the vertical offset of the picture from the upper left corner in units of 1/4000 of the chart area.
- left – the vertical offset of the picture from the upper left corner in units of 1/4000 of the chart area.
- stream – a stream object which contains the image data.
- widthScale – the scale of image width, a percentage value.
- heightScale – the scale of image height, a percentage value.
The method returns an object of the Picture class where the Picture class represents a picture object in the chart.
The following example shows how to add a picture to the chart. The example utilizes the previous designer file which has a chart in it. We use this file to insert an image into the chart.
Below is the original code for adding a picture to the chart. The following output is generated when executing the code
A picture is inserted into the chart
// 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."); |
Adding Checkbox in the Chart
Aspose.Cells allows you to insert checkboxes into a chart sheet by using MsoDrawingType enumeration. The following example demonstrates adding a checkbox to a chart sheet.
The following image shows the chart sheet with the checkbox in the output file.
The output file generated by the following code snippet is attached for your reference.
// 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"); |