Formatting Charts

Formatting Chart Entities

Formatting Chart Entities

Aspose.Slides for Java provides a simple API for managing different chart entities and formatting them using custom values:

  1. Create an instance of the Presentation class.
  2. Obtain a slide’s reference by its index.
  3. Add a chart with default data along with the any of desired type (in this example we will use ChartType.LineWithMarkers).
  4. Access the chart Value Axis and set the following properties:
    1. Setting Line format for Value Axis Major Grid lines
    2. Setting Line format for Value Axis Minor Grid lines
    3. Setting Number Format for Value Axis
    4. Setting Min, Max, Major and Minor units for Value Axis
    5. Setting Text Properties for Value Axis data
    6. Setting Title for Value Axis
    7. Setting Line Format for Value Axis
  5. Access the chart Category Axis and set the following properties:
    1. Setting Line format for Category Axis Major Grid lines
    2. Setting Line format for Category Axis Minor Grid lines
    3. Setting Text Properties for Category Axis data
    4. Setting Title for Category Axis
    5. Setting Label Positioning for Category Axis
    6. Setting Rotation Angle for Category Axis labels
  6. Access the chart Legend and set the Text Properties for them
  7. Set show chart Legends without overlapping chart
  8. Access the chart Secondary Value Axis and set the following properties:
    1. Enable the Secondary Value Axis
    2. Setting Line Format for Secondary Value Axis
    3. Setting Number Format for Secondary Value Axis
    4. Setting Min, Max, Major and Minor units for Secondary Value Axis
  9. Now plot the first chart series on Secondary Value Axis
  10. Set the chart back wall fill color
  11. Set the chart plot area fill color
  12. Write the modified presentation to a PPTX file

The above code snipped will create a chart like the one shown below.

todo:image_alt_text
Figure: Formatted chart added to the slide

Change the type of chart’s category axis

New methods getCategoryAxisType() and setCategoryAxisType() have been added to IAxis and Axis classes. Below are the properties to determine category axis type.

  • CategoryAxisType.Text - category axis type is Text
  • CategoryAxisType.Date - category axis type is DateTime However, the CategoryAxisType.Auto is not supported at the moment.

Display chart labels as callouts

New methods getShowLabelAsDataCallout() and setShowLabelAsDataCallout() have been added to DataLabelFormat class and IDataLabelFormat interface. These methods determine either specified chart’s data label will be displayed as data callout or as data label.

Set Pie Chart Sector Colors

Aspose.Slides for Java provides an API for creating and filling pie charts in an easy way. To create a chart on a slide:

  1. Create an instance of the Presentation class.
  2. Obtain a slide’s reference by its index.
  3. Add a chart with default data along with the desired type (ChartType.Pie).
  4. Access the chart data IChartDataWorkbook.
  5. Clear the default series and categories.
  6. Add new series and categories.
  7. Add new chart data for the chart series.
  8. Add new points for charts and add custom colors for the pie chart’s sectors.
  9. Set labels for series.
  10. Set leader lines for series labels.
  11. Set the rotation angle for pie chart slides.
  12. Write the modified presentation to a PPTX file

The above code snippet create a chart like the one shown below.

todo:image_alt_text
Figure: Pie chart added to the slide

Apply Color to Data Points

You can apply color to data points in the chart using Aspose.Slides for Java. IChartDataPointLevelsManager and IChartDataPointLevel classes have been added to get access to properties of data point levels. This article demonstrates how you can access and apply color to data points in a chart.

Setting Number Format for Chart Data Cell

Aspose.Slides for Java provides a simple API for managing chart data format:

  1. Create an instance of the Presentation class.
  2. Obtain a slide’s reference by its index.
  3. Add a chart with default data along with the any of desired type (this example uses ChartType.ClusteredColumn).
  4. Set the preset number format from the possible preset values.
  5. Traverse through the chart data cell in every chart series and set the chart data number format.
  6. Save the presentation.
  7. Set the custom number format.
  8. Traverse through chart data cell inside every chart series and setting a different chart data number format.
  9. Save the presentation.

The above code snippet create a chart like the one shown below.

todo:image_alt_text
Figure: Sample chart with different number formats
 The possible preset number format values along with their preset index and that can be used are given below:
0General
10
20.00
3#,##0
4#,##0.00
5$#,##0;$-#,##0
6$#,##0;Red$-#,##0
7$#,##0.00;$-#,##0.00
8$#,##0.00;Red$-#,##0.00
90%
100.00%
110.00E+00
12# ?/?
13# /
14m/d/yy
15d-mmm-yy
16d-mmm
17mmm-yy
18h:mm AM/PM
19h:mm:ss AM/PM
20h:mm
21h:mm:ss
22m/d/yy h:mm
37#,##0;-#,##0
38#,##0;Red-#,##0
39#,##0.00;-#,##0.00
40#,##0.00;Red-#,##0.00
41_ * #,##0_ ;_ * “_ ;_ @_
42_ $* #,##0_ ;_ $* “_ ;_ @_
43_ * #,##0.00_ ;_ * “??_ ;_ @_
44_ $* #,##0.00_ ;_ $* “??_ ;_ @_
45mm:ss
46h :mm:ss
47mm:ss.0
48##0.0E+00
49@

Setting Automatic Series Fill Color

Aspose.Slides for Java provides an API for setting automatic fill color for chart series inside plot area:

  1. Create an instance of the Presentation class.
  2. Obtain a slide’s reference by its index.
  3. Add a chart with default data along with the any of desired type (this example uses ChartType.ClusteredColumn).
  4. Accessing the chart series and setting the fill color to Automatic.
  5. Save the presentation to a PPTX file.

Setting the Label Distance From Category Axis

In order to set the Label Distance. Please follow the steps below:

  1. Create an instance of Presentation class.
  2. Get reference of the slide.
  3. Adding a chart on slide.
  4. Setting the position of label from axis.
  5. Write the presentation as a PPTX file.

In the example given below, we have set the label distance from category axis.

Setting Custom Location and Size for Chart Legend

In order to set the legend properties. Please follow the steps below:

  1. Create an instance of Presentation class.
  2. Get reference of the slide.
  3. Adding a chart on slide.
  4. Setting the properties of legend.
  5. Write the presentation as a PPTX file.

In the example given below, we have set the position and size for Chart legend.

Specifying Doughnut chart hole size

In order to specify the size of the hole in a doughnut chart, please follow the steps below:

  1. Instantiate Presentation object.
  2. Add doughnut chart on the slide.
  3. Specify the size of the hole in a doughnut chart.
  4. Write presentation to disk.

In the example given below, we have set the size of the hole in a doughnut chart.

Second plot options for Pie of Pie and Bar of Pie chart

In order to specify the properties, please follow the steps below:

  1. Instantiate Presentation object.
  2. Add chart on the slide.
  3. Specify the second plot options of chart.
  4. Write presentation to disk.

In the example given below, we have set different properties of Pie of Pie chart.

Get chart external data source woorkbook path

Aspose.Slides for Java provides a simple API for getting value from WorkBook Cell used as DataLabel:

  1. Create an instance of the Presentation class.
  2. Obtain a slide’s reference by its index.
  3. Create object for chart shape
  4. Create object for source type of ChartDataSourceType which represents data source of the chart.
  5. If Source Type is equal to external workbook the get chart external data source workbook path.

In the example given below, we have set the label distance from category axis.

Bubble chart size scaling

Aspose.Slides for Java provides support for Bubble chart size scaling. In Aspose.Slides for Java IChartSeries.setBubbleSizeScale()/getBubbleSizeScale() and IChartSeriesGroup.setBubbleSizeScale()/getBubbleSizeScale() methods have been added. Below sample example is given. 

Setting Font Size of Legend

The Aspose.Slides for Java lets developers allow to set font size of legend. Please follow the steps below: 

  • Instantiate Presentation class.
  • Creating the default chart.
  • Set the Font Size.
  • Set minimum axis value.
  • Set maximum axis value.
  • Write presentation to disk.

Setting font size of individual Legend

The Aspose.Slides for Java lets developers allow to set font size of individual legend entries. Please follow the steps below: 

  • Instantiate Presentation class.
  • Creating the default chart.
  • Access legend entry.
  • Set the Font Size.
  • Set minimum axis value.
  • Set maximum axis value.
  • Write presentation to disk.

Setting InvertIfNegative Property for Individual Series

The Aspose.Slides for Java lets developers allow to set inverts. IChartDataPoint.InvertIfNegative and ChartDataPoint.InvertIfNegative properties have been added. This Specifies the data point shall invert its colors if the value is negative. Sample code is given below.

Represent Data As Bubble Chart Sizes

The Aspose.Slides for Java added a method getBubbleSizeRepresentation to IChartSeries, IChartSeriesGroup interfaces, and related classes. getBubbleSizeRepresentation specifies how the bubble size values are represented in the bubble chart. Possible values are: BubbleSizeRepresentationType.Area and BubbleSizeRepresentationType.Width. Accordingly, BubbleSizeRepresentationType enum has been added to specify the possible ways to represent data as bubble chart sizes. Sample code is given below.

Adding Custom Lines

Aspose.Slides for Java provides a simple API to add custom lines in a chart. To add a simple plain line to a selected slide of the presentation, please follow the steps below:

  • Create an instance of Presentation class
  • Obtain the reference of a slide by using its Index
  • Create a new chart using AddChart method exposed by Shapes object
  • Add an AutoShape of Line type using AddAutoShape method exposed by Shapes object
  • Set the Color of the shape lines.
  • Write the modified presentation as a PPTX file

The following code is used to create a chart with Custom Lines.

Clear Specific Chart Series Data Points Data

Aspose.Slides for Java provides a simple API to clear specific chart series DataPoints data. To clear specific chart series DataPoints data, please follow the steps below:

  • Create an instance of Presentation class and load the desired presentation.
  • Obtain the reference of a slide by using its Index
  • Obtain the reference of a chart by using its Index
  • Iterate through all the DataPoints of chart and set XValue and YValue to null.
  • Remove all DataPoints of specific chart series
  • Write the modified presentation to a PPTX file

Sample code is given below.

Setting Layout Mode of Chart Plot Area

Aspose.Slides for Java provides a simple API to set the layout mode of the chart plot area. Property LayoutTargetType has been added to ChartPlotArea and IChartPlotArea classes. If the layout of the plot area defined manually this property specifies whether to layout the plot area by its inside (not including axis and axis labels) or outside (including axis and axis labels). There are two possible values which are defined in LayoutTargetType enum.

  • LayoutTargetType.Inner - specifies that the plot area size shall determine the size of the plot area, not including the tick marks and axis labels.
  • LayoutTargetType.Outer - specifies that the plot area size shall determine the size of the plot area, the tick marks, and the axis labels.

Sample code is given below.

You can apply color to data points in the chart using Aspose.Slides for Java. IChartDataPointLevelsManager and IChartDataPointLevel classes have been added to get access to properties of data point levels. This article demonstrates how you can access and apply color to data points in a chart.