Chart Formatting
Format Chart Entities
Aspose.Slides for Java lets developers add custom charts to their slides from scratch. This article explains how to format different chart entities including chart category and value axis.
Aspose.Slides for Java provides a simple API for managing different chart entities and formatting them using custom values:
- Create an instance of the Presentation class.
- Obtain a slide’s reference by its index.
- Add a chart with default data along with the any of desired type (in this example we will use ChartType.LineWithMarkers).
- Access the chart Value Axis and set the following properties:
- Setting Line format for Value Axis Major Grid lines
- Setting Line format for Value Axis Minor Grid lines
- Setting Number Format for Value Axis
- Setting Min, Max, Major and Minor units for Value Axis
- Setting Text Properties for Value Axis data
- Setting Title for Value Axis
- Setting Line Format for Value Axis
- Access the chart Category Axis and set the following properties:
- Setting Line format for Category Axis Major Grid lines
- Setting Line format for Category Axis Minor Grid lines
- Setting Text Properties for Category Axis data
- Setting Title for Category Axis
- Setting Label Positioning for Category Axis
- Setting Rotation Angle for Category Axis labels
- Access the chart Legend and set the Text Properties for them
- Set show chart Legends without overlapping chart
- Access the chart Secondary Value Axis and set the following properties:
- Enable the Secondary Value Axis
- Setting Line Format for Secondary Value Axis
- Setting Number Format for Secondary Value Axis
- Setting Min, Max, Major and Minor units for Secondary Value Axis
- Now plot the first chart series on Secondary Value Axis
- Set the chart back wall fill color
- Set the chart plot area fill color
- Write the modified presentation to a PPTX file
// Create an instance of Presentation class
Presentation pres = new Presentation();
try {
// Accessing the first slide
ISlide slide = pres.getSlides().get_Item(0);
// Adding the sample chart
IChart chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 50, 50, 500, 400);
// Setting Chart Title
chart.hasTitle();
chart.getChartTitle().addTextFrameForOverriding("");
IPortion chartTitle = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0);
chartTitle.setText("Sample Chart");
chartTitle.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
chartTitle.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
chartTitle.getPortionFormat().setFontHeight(20);
chartTitle.getPortionFormat().setFontBold(NullableBool.True);
chartTitle.getPortionFormat().setFontItalic(NullableBool.True);
// Setting Major grid lines format for value axis
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().setWidth(5);
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().setDashStyle(LineDashStyle.DashDot);
// Setting Minor grid lines format for value axis
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED);
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().setWidth(3);
// Setting value axis number format
chart.getAxes().getVerticalAxis().isNumberFormatLinkedToSource();
chart.getAxes().getVerticalAxis().setDisplayUnit(DisplayUnitType.Thousands);
chart.getAxes().getVerticalAxis().setNumberFormat("0.0%");
// Setting chart maximum, minimum values
chart.getAxes().getVerticalAxis().isAutomaticMajorUnit();
chart.getAxes().getVerticalAxis().isAutomaticMaxValue();
chart.getAxes().getVerticalAxis().isAutomaticMinorUnit();
chart.getAxes().getVerticalAxis().isAutomaticMinValue();
chart.getAxes().getVerticalAxis().setMaxValue(15f);
chart.getAxes().getVerticalAxis().setMinValue(-2f);
chart.getAxes().getVerticalAxis().setMinorUnit(0.5f);
chart.getAxes().getVerticalAxis().setMajorUnit(2.0f);
// Setting Value Axis Text Properties
IChartPortionFormat txtVal = chart.getAxes().getVerticalAxis().getTextFormat().getPortionFormat();
txtVal.setFontBold(NullableBool.True);
txtVal.setFontHeight(16);
txtVal.setFontItalic(NullableBool.True);
txtVal.getFillFormat().setFillType(FillType.Solid);
txtVal.getFillFormat().getSolidFillColor().setColor(new Color(PresetColor.DarkGreen));
txtVal.setLatinFont(new FontData("Times New Roman"));
// Setting value axis title
chart.getAxes().getVerticalAxis().hasTitle();
chart.getAxes().getVerticalAxis().getTitle().addTextFrameForOverriding("");
IPortion valtitle = chart.getAxes().getVerticalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0);
valtitle.setText("Primary Axis");
valtitle.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
valtitle.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
valtitle.getPortionFormat().setFontHeight(20);
valtitle.getPortionFormat().setFontBold(NullableBool.True);
valtitle.getPortionFormat().setFontItalic(NullableBool.True);
// Setting Major grid lines format for Category axis
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GREEN);
chart.getAxes().getHorizontalAxis().getMajorGridLinesFormat().getLine().setWidth(5);
// Setting Minor grid lines format for Category axis
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.YELLOW);
chart.getAxes().getHorizontalAxis().getMinorGridLinesFormat().getLine().setWidth(3);
// Setting Category Axis Text Properties
IChartPortionFormat txtCat = chart.getAxes().getHorizontalAxis().getTextFormat().getPortionFormat();
txtCat.setFontBold(NullableBool.True);
txtCat.setFontHeight(16);
txtCat.setFontItalic(NullableBool.True);
txtCat.getFillFormat().setFillType(FillType.Solid);
txtCat.getFillFormat().getSolidFillColor().setColor(Color.BLUE);
txtCat.setLatinFont(new FontData("Arial"));
// Setting Category Title
chart.getAxes().getHorizontalAxis().hasTitle();
chart.getAxes().getHorizontalAxis().getTitle().addTextFrameForOverriding("");
IPortion catTitle = chart.getAxes().getHorizontalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getPortions().get_Item(0);
catTitle.setText("Sample Category");
catTitle.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
catTitle.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
catTitle.getPortionFormat().setFontHeight(20);
catTitle.getPortionFormat().setFontBold(NullableBool.True);
catTitle.getPortionFormat().setFontItalic(NullableBool.True);
// Setting category axis label position
chart.getAxes().getHorizontalAxis().setTickLabelPosition(TickLabelPositionType.Low);
// Setting category axis label rotation angle
chart.getAxes().getHorizontalAxis().setTickLabelRotationAngle(45);
// Setting Legends Text Properties
IChartPortionFormat txtleg = chart.getLegend().getTextFormat().getPortionFormat();
txtleg.setFontBold(NullableBool.True);
txtleg.setFontHeight(16);
txtleg.setFontItalic(NullableBool.True);
txtleg.getFillFormat().setFillType(FillType.Solid);
txtleg.getFillFormat().getSolidFillColor().setColor(new Color(PresetColor.DarkRed));
// Set show chart legends without overlapping chart
chart.getLegend().setOverlay(true);
// chart.ChartData.Series[0].PlotOnSecondAxis=true;
chart.getChartData().getSeries().get_Item(0).setPlotOnSecondAxis(true);
// Setting secondary value axis
chart.getAxes().getSecondaryVerticalAxis().isVisible();
chart.getAxes().getSecondaryVerticalAxis().getFormat().getLine().setStyle(LineStyle.ThickBetweenThin);
chart.getAxes().getSecondaryVerticalAxis().getFormat().getLine().setWidth(20);
// Setting secondary value axis Number format
chart.getAxes().getSecondaryVerticalAxis().isNumberFormatLinkedToSource();
chart.getAxes().getSecondaryVerticalAxis().setDisplayUnit(DisplayUnitType.Hundreds);
chart.getAxes().getSecondaryVerticalAxis().setNumberFormat("0.0%");
// Setting chart maximum, minimum values
chart.getAxes().getSecondaryVerticalAxis().isAutomaticMajorUnit();
chart.getAxes().getSecondaryVerticalAxis().isAutomaticMaxValue();
chart.getAxes().getSecondaryVerticalAxis().isAutomaticMinorUnit();
chart.getAxes().getSecondaryVerticalAxis().isAutomaticMinValue();
chart.getAxes().getSecondaryVerticalAxis().setMaxValue(20f);
chart.getAxes().getSecondaryVerticalAxis().setMinValue(-5f);
chart.getAxes().getSecondaryVerticalAxis().setMinorUnit(0.5f);
chart.getAxes().getSecondaryVerticalAxis().setMajorUnit(2.0f);
// Setting chart back wall color
chart.getBackWall().setThickness(1);
chart.getBackWall().getFormat().getFill().setFillType(FillType.Solid);
chart.getBackWall().getFormat().getFill().getSolidFillColor().setColor(Color.ORANGE);
chart.getFloor().getFormat().getFill().setFillType(FillType.Solid);
chart.getFloor().getFormat().getFill().getSolidFillColor().setColor(Color.RED);
// Setting Plot area color
chart.getPlotArea().getFormat().getFill().setFillType(FillType.Solid);
chart.getPlotArea().getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.LightCyan));
// Save Presentation
pres.save("FormattedChart.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Set Font Properties for Chart
Aspose.Slides for Java provides support for setting the font related properties for the chart. Please follow the steps below for setting the font properties for chart.
- Instantiate Presentation class object.
- Add chart on the slide.
- Set font height.
- Save modified presentation.
Below sample example is given.
// Create an instance of Presentation class
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 500, 400);
chart.getTextFormat().getPortionFormat().setFontHeight(20);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
pres.save("FontPropertiesForChart.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Set Format of Numerics
Aspose.Slides for Java provides a simple API for managing chart data format:
- Create an instance of the Presentation class.
- Obtain a slide’s reference by its index.
- Add a chart with default data along with the any of desired type (this example uses ChartType.ClusteredColumn).
- Set the preset number format from the possible preset values.
- Traverse through the chart data cell in every chart series and set the chart data number format.
- Save the presentation.
- Set the custom number format.
- Traverse through chart data cell inside every chart series and setting a different chart data number format.
- Save the presentation.
// Create an instance of Presentation class
Presentation pres = new Presentation();
try {
// Access the first presentation slide
ISlide slide = pres.getSlides().get_Item(0);
// Adding a default clustered column chart
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 500, 400);
// Accessing the chart series collection
IChartSeriesCollection series = chart.getChartData().getSeries();
// Traverse through every chart series
for (IChartSeries ser : series)
{
// Traverse through every data cell in series
for (IChartDataPoint cell : ser.getDataPoints())
{
// Setting the number format
cell.getValue().getAsCell().setPresetNumberFormat((byte) 10); // 0.00%
}
}
// Saving presentation
pres.save("PresetNumberFormat.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
The possible preset number format values along with their preset index and that can be used are given below:
0 | General |
---|---|
1 | 0 |
2 | 0.00 |
3 | #,##0 |
4 | #,##0.00 |
5 | $#,##0;$-#,##0 |
6 | $#,##0;Red$-#,##0 |
7 | $#,##0.00;$-#,##0.00 |
8 | $#,##0.00;Red$-#,##0.00 |
9 | 0% |
10 | 0.00% |
11 | 0.00E+00 |
12 | # ?/? |
13 | # / |
14 | m/d/yy |
15 | d-mmm-yy |
16 | d-mmm |
17 | mmm-yy |
18 | h:mm AM/PM |
19 | h:mm:ss AM/PM |
20 | h:mm |
21 | h:mm:ss |
22 | m/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_ ;_ $* “??_ ;_ @_ |
45 | mm:ss |
46 | h :mm:ss |
47 | mm:ss.0 |
48 | ##0.0E+00 |
49 | @ |
Set Chart Area Rounded Borders
Aspose.Slides for Java provides support for setting chart area. Methods hasRoundedCorners and setRoundedCorners have been added to IChart interface and Chart class.
- Instantiate Presentation class object.
- Add chart on the slide.
- Set fill type and fill color of chart
- Set round corner property True.
- Save modified presentation.
Below sample example is given.
// Create an instance of Presentation class
Presentation pres = new Presentation();
try {
ISlide slide = pres.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 100, 600, 400);
chart.getLineFormat().getFillFormat().setFillType(FillType.Solid);
chart.getLineFormat().setStyle(LineStyle.Single);
chart.setRoundedCorners(true);
pres.save("output.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}