Error Appears when Setting Legend Position in PowerPoint Chart in Node.js

Hi Support,

I’m getting this error while setting position of chart legend.

var java = require('java');
var aspose = aspose || {};

asposeSlides = require("aspose.slides.via.java");

// var license = new asposeSlides.License();
// license.setLicense("Aspose.SlidesforNode.jsviaJava.lic");

var pres = new asposeSlides.Presentation("Plane Type Model Template v3.pptx");

sld = pres.getSlides().get_Item(3);

// Creates the default chart
chart = sld.getShapes().addChart(asposeSlides.ChartType.Line, 0, 0, 400, 400);

// Gets the default chart data worksheet index
defaultWorksheetIndex = 0;

// Gets the chart data worksheet
fact = chart.getChartData().getChartDataWorkbook();
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();

 chart.getLegend().setX(50 / chart.getWidth());
    chart.getLegend().setY(50 / chart.getHeight());
    chart.getLegend().setWidth(100 / chart.getWidth());
    chart.getLegend().setHeight(100 / chart.getHeight());

chart
  .getChartData()
  .getSeries()
  .add(fact.getCell(0, 0, 1, "Series 1"), chart.getType());
series = chart.getChartData().getSeries().get_Item(0);

chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "c1"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 1, 1, 24));
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "c2"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 2, 1, 23));
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "c3"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 3, 1, 10));
chart.getChartData().getCategories().add(fact.getCell(0, 4, 0, "c4"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 4, 1, null));

chart
  .getChartData()
  .getSeries()
  .add(fact.getCell(0, 0, 2, "Series 2"), chart.getType());
//Take second chart series
series2 = chart.getChartData().getSeries().get_Item(1);

//Now populating series data
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 1, 2, 30));
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 2, 2, 10));
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 3, 2, 60));
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 4, 2, 40));

// chart.setLegend(true);
// chart.getLegend().setOverlay(false);

pres.save("out9.pptx", asposeSlides.SaveFormat.Pptx);
console.log("Done");

image.png (35.2 KB)

@karanmarsh,
Thank you for contacting support. I am working on the issue and will get back to you soon.

@karanmarsh,
Thank you for your patience. Please take a look at the error description you encountered:

Error: Could not find method “setX(java.lang.Double)” on class “class com.aspose.slides.Legend”. Possible matches:
public final void com.aspose.slides.Legend.setX(float)

The setX, setY, setWidth, and setHeight methods accept a float parameter. Therefore, you should use java.newFloat as follows:

chart.getLegend().setX(java.newFloat(50 / chart.getWidth()));
chart.getLegend().setY(java.newFloat(50 / chart.getHeight()));
chart.getLegend().setWidth(java.newFloat(100 / chart.getWidth()));
chart.getLegend().setHeight(java.newFloat(100 / chart.getHeight()));

@andrey.potapov Thanks. Error resolved but can you please assist me how can I set this legend to bottom and top of the chart as shown in below image.
image.png (3.5 KB)

image.png (13.0 KB)

@karanmarsh,
I am working on the question and will get back to you soon.

@karanmarsh,

Instead of

chart.getLegend().setX(java.newFloat(50 / chart.getWidth()));
chart.getLegend().setY(java.newFloat(50 / chart.getHeight()));
chart.getLegend().setWidth(java.newFloat(100 / chart.getWidth()));
chart.getLegend().setHeight(java.newFloat(100 / chart.getHeight()));

you can set the legend to bottom of the chart as follows:

chart.setLegend(true);
chart.getLegend().setPosition(asposeSlides.LegendPositionType.Bottom);

In a similar way, you can also set the legend at the top of the chart.

@andrey.potapov
Resolved. Thanks.

@karanmarsh,
Thank you for using Aspose.Slides.