ValidateChartLayout Method Throws a FormatException for a Bubble Chart

I’m trying to create a bubble chart with no valid data points. And when I do validateChartLayout, it throws the following exception.

Exception in thread “main” class com.aspose.slides.exceptions.FormatException: Input string was not in the correct format
com.aspose.slides.internal.pr.wn.jy(Unknown Source)
com.aspose.slides.internal.pr.wn.jy(Unknown Source)
com.aspose.slides.internal.pr.wn.jy(Unknown Source)
com.aspose.slides.internal.pr.wn.jy(Unknown Source)
com.aspose.slides.ms.System.n4.jy(Unknown Source)
com.aspose.slides.bk.jy(Unknown Source)
com.aspose.slides.bk.jy(Unknown Source)
com.aspose.slides.ni0.j3(Unknown Source)
com.aspose.slides.ni0.ur(Unknown Source)
com.aspose.slides.ValueAxisBase.t8(Unknown Source)
com.aspose.slides.xt.jy(Unknown Source)
com.aspose.slides.v3.vz(Unknown Source)
com.aspose.slides.v3.fg(Unknown Source)
com.aspose.slides.zp.t7(Unknown Source)
com.aspose.slides.zp.t7(Unknown Source)
com.aspose.slides.zp.jy(Unknown Source)
com.aspose.slides.v3.vz(Unknown Source)
com.aspose.slides.v3.fg(Unknown Source)
com.aspose.slides.fa.jy(Unknown Source)
com.aspose.slides.v3.vz(Unknown Source)
com.aspose.slides.v3.fg(Unknown Source)
com.aspose.slides.ll.qg(Unknown Source)
com.aspose.slides.Chart.validateChartLayout(Unknown Source)
myProject.tempFile.main(tempFile.java:30)
at com.aspose.slides.internal.pr.wn.jy(Unknown Source)
at com.aspose.slides.internal.pr.wn.jy(Unknown Source)
at com.aspose.slides.internal.pr.wn.jy(Unknown Source)
at com.aspose.slides.internal.pr.wn.jy(Unknown Source)
at com.aspose.slides.ms.System.n4.jy(Unknown Source)
at com.aspose.slides.bk.jy(Unknown Source)
at com.aspose.slides.bk.jy(Unknown Source)
at com.aspose.slides.ni0.j3(Unknown Source)
at com.aspose.slides.ni0.ur(Unknown Source)
at com.aspose.slides.ValueAxisBase.t8(Unknown Source)
at com.aspose.slides.xt.jy(Unknown Source)
at com.aspose.slides.v3.vz(Unknown Source)
at com.aspose.slides.v3.fg(Unknown Source)
at com.aspose.slides.zp.t7(Unknown Source)
at com.aspose.slides.zp.t7(Unknown Source)
at com.aspose.slides.zp.jy(Unknown Source)
at com.aspose.slides.v3.vz(Unknown Source)
at com.aspose.slides.v3.fg(Unknown Source)
at com.aspose.slides.fa.jy(Unknown Source)
at com.aspose.slides.v3.vz(Unknown Source)
at com.aspose.slides.v3.fg(Unknown Source)
at com.aspose.slides.ll.qg(Unknown Source)
at com.aspose.slides.Chart.validateChartLayout(Unknown Source)
at myProject.tempFile.main(tempFile.java:30)

Sample code to reproduce the issue.

Presentation pres = new Presentation();

ISlide slide = pres.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(com.aspose.slides.ChartType.Bubble, 0, 0, 400, 400);
int defaultWorksheetIndex = 0;

IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();

chart.getChartData().getSeries().clear();
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());

IChartSeries series = chart.getChartData().getSeries().get_Item(0);
series.getDataPoints().addDataPointForBubbleSeries( fact.getCell(defaultWorksheetIndex, 2, 2, null),fact.getCell(defaultWorksheetIndex, 2, 3, null),fact.getCell(defaultWorksheetIndex, 2, 4, 11));

chart.setDisplayBlanksAs(DisplayBlanksAsType.Gap);
chart.validateChartLayout();

I’m not sure why I’m getting the exception. Please suggest.
Thanks in advance

@manavjaiswal

The exception you are encountering when calling validateChartLayout() is likely due to the fact that you are trying to create a bubble chart without valid data points. In your code, you are adding a data point for the bubble series, but it seems that the values you are providing may not be valid, particularly since you are using null for one of the data points.

To resolve this issue, ensure that you are providing valid numeric values for the X, Y, and Size data points when adding a data point for the bubble series. The null value in your code could be causing the FormatException since the chart expects valid numeric inputs.

Here’s a modified version of your code that ensures valid data points are provided:

Presentation pres = new Presentation();
ISlide slide = pres.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(com.aspose.slides.ChartType.Bubble, 0, 0, 400, 400);
int defaultWorksheetIndex = 0;

IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();

// Clear existing series
chart.getChartData().getSeries().clear();

// Add a new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());

IChartSeries series = chart.getChartData().getSeries().get_Item(0);

// Ensure valid numeric values are provided for the data points
series.getDataPoints().addDataPointForBubbleSeries(
    fact.getCell(defaultWorksheetIndex, 2, 2, 1), // X value
    fact.getCell(defaultWorksheetIndex, 2, 3, 2), // Y value
    fact.getCell(defaultWorksheetIndex, 2, 4, 11) // Size value
);

chart.setDisplayBlanksAs(DisplayBlanksAsType.Gap);

// Validate the chart layout
chart.validateChartLayout();

In this example, I replaced the null value with a valid numeric value (e.g., 1 for the X value and 2 for the Y value). Make sure to adjust these values according to your data requirements.

If you continue to experience issues, please double-check the data being passed to ensure that all values are valid and appropriate for a bubble chart.

@Professionalize.Discourse
This doesn’t clarify my query.
Note 1: I’m using “setDisplayBlanksAs(DisplayBlanksAsType.Gap)” to restrict taking null as zero for population of chart. If I don’t use this then there is no such exception even if I pass null for x or y value.
Note 2: If I’m passing (x=null) then I can see exception, but if I pass (y= null) then there is no exception thrown, please take a look at the following code (which doesn’t throw exception)

Presentation pres = new Presentation();

ISlide slide = pres.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(com.aspose.slides.ChartType.Bubble, 0, 0, 400, 400);
int defaultWorksheetIndex = 0;

IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();

chart.getChartData().getSeries().clear();
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());

IChartSeries series = chart.getChartData().getSeries().get_Item(0);
series.getDataPoints().addDataPointForBubbleSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 2), fact.getCell(defaultWorksheetIndex, 2, 3, null), fact.getCell(defaultWorksheetIndex, 2, 4, 11));

chart.setDisplayBlanksAs(DisplayBlanksAsType.Gap);
chart.validateChartLayout();

Here, I’m able to save ppt succesfully.

@manavjaiswal,
Thank you for the details. I’ve reproduced the problem you described.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39609

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@manavjaiswal,
Our developers have investigated the case. The exception occurs because we cannot automatically calculate axis values ​​for NULLs. To fix this, we suggest setting the axis values ​​manually:

Presentation pres = new Presentation();

ISlide slide = pres.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(com.aspose.slides.ChartType.Bubble, 0, 0, 400, 400);
int defaultWorksheetIndex = 0;

IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();

chart.getChartData().getSeries().clear();
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());

setChartAxisValues(chart.getAxes(), -1, 6, 2, 0.5, -1, 3, 2, 0.5);

IChartSeries series = chart.getChartData().getSeries().get_Item(0);
series.getDataPoints().addDataPointForBubbleSeries(fact.getCell(defaultWorksheetIndex, 2, 2, null), fact.getCell(defaultWorksheetIndex, 2, 3, null), fact.getCell(defaultWorksheetIndex, 2, 4, 11));

chart.setDisplayBlanksAs(DisplayBlanksAsType.Gap);
chart.validateChartLayout();
void setChartAxisValues(IAxesManager axisManager,
                        double minX,
                        double maxX,
                        double majorUnitX,
                        double minorUnitX,
                        double minY,
                        double maxY,
                        double majorUnitY,
                        double minorUnitY
)
{
    axisManager.getVerticalAxis().setAutomaticMaxValue(false);
    axisManager.getVerticalAxis().setAutomaticMinValue(false);
    axisManager.getVerticalAxis().setMinValue(minX);
    axisManager.getVerticalAxis().setMaxValue(maxX);
    axisManager.getVerticalAxis().setAutomaticMajorUnit(false);
    axisManager.getVerticalAxis().setAutomaticMinorUnit(false);
    axisManager.getVerticalAxis().setMajorUnit(majorUnitX);
    axisManager.getVerticalAxis().setMinorUnit(minorUnitX);

    axisManager.getHorizontalAxis().setAutomaticMaxValue(false);
    axisManager.getHorizontalAxis().setAutomaticMinValue(false);
    axisManager.getHorizontalAxis().setMinValue(minY);
    axisManager.getHorizontalAxis().setMaxValue(maxY);
    axisManager.getHorizontalAxis().setAutomaticMajorUnit(false);
    axisManager.getHorizontalAxis().setAutomaticMinorUnit(false);
    axisManager.getHorizontalAxis().setMajorUnit(majorUnitY);
    axisManager.getHorizontalAxis().setMinorUnit(minorUnitY);
}

Chart Axis|Aspose.Slides Documentation