Hi,
Please find the below sample and see the issue mentioned in the attached image.
Here the requirement is to show legend only. For that, I am creating a dummy chart and hiding maximum possible components. Also, setting legend width and height maximum possible. It works fine for me, BUT the image generated just shows a line.
Thanks & Regards
Muhammed
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import com.aspose.cells.ASeries;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.DisplayUnitType;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.GradientColorType;
import com.aspose.cells.GradientDirectionType;
import com.aspose.cells.GradientFill;
import com.aspose.cells.GradientFillType;
import com.aspose.cells.GradientStyleType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.Legend;
import com.aspose.cells.LegendPositionType;
import com.aspose.cells.PatternFill;
import com.aspose.cells.TickLabelPositionType;
import com.aspose.cells.TrendlineType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.OleObjectFrame;
import com.aspose.slides.Presentation;
import com.aspose.slides.Slide;
public class CommonLegendPOC {
public static void main(String args[]) {
try {
Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().getSheet(0);
//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();
//Add a chart in ChartSheet with data series from DataSheet
int chartWidth = 600;
int chartHeight = 50;
int chartHeightInRows = 0;
int chartWidthInCols = 0;
int cellWidth = dataSheet.getCells().getColumnWidthPixel(0);
int cellHeight = dataSheet.getCells().getRowHeightPixel(0);
chartWidthInCols = (int)(chartWidth / cellWidth);
chartHeightInRows = (int)(chartHeight / cellHeight);
chartWidth = chartWidthInCols * cellWidth;
chartHeight = chartHeightInRows * cellHeight;
Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, 0, 0, 0, chartWidth, chartHeight);
chart.getNSeries().add("{0.0}", true);
chart.getNSeries().add("{0.0}", true);
chart.getNSeries().add("{0.0}", true);
chart.getNSeries().add("{0.0}", true);
chart.getNSeries().add("{0.0}", true);
//chart.getNSeries().add(dataSheetName + "!B1:F4", false);
for (int i = 0; i < chart.getNSeries().size(); i++) {
ASeries series = chart.getNSeries().get(i);
PatternFill pf = new PatternFill();
pf.setBackgroundColor(wb.getPalette().getColor(i));
pf.setForegroundColor(wb.getPalette().getColor(i));
series.getArea().setFill(pf);
}
chart.getCategoryAxis().setVisible(false);
chart.getCategoryAxis().setDisplayUnitLabelShown(false);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.NONE);
chart.getCategoryAxis().getMajorGridLines().setVisible(false);
chart.getCategoryAxis().getMinorGridLines().setVisible(false);
chart.getValueAxis().setVisible(false);
chart.getValueAxis().setDisplayUnitLabelShown(false);
chart.getValueAxis().setTickLabelPosition(TickLabelPositionType.NONE);
chart.getValueAxis().getMajorGridLines().setVisible(false);
chart.getValueAxis().getMinorGridLines().setVisible(false);
chart.getPlotArea().getArea().setVisible(false);
chart.getPlotArea().getBorder().setVisible(false);
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.TOP);
legend.setX(100);
legend.setY(100);
legend.setWidth(3900);
legend.setHeight(3800);
legend.getBorder().setVisible(false);
//Get Chart as image.
ImageOptions imgOpts = new ImageOptions();
imgOpts.setImageFormat(ImageFormat.PNG);
imgOpts.setFashion(FileFormatType.EXCEL2003);
chart.toImage(new FileOutputStream("D:\\Temp\\chart.png"), imgOpts);
wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartHeightInRows - 1, 0, chartWidthInCols - 1);
//Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
wb.save(new FileOutputStream("D:\\Temp\\output.xls"));
Presentation pres = new Presentation();
Slide sld = pres.getSlideByPosition(1);
int slideWidth = (int) pres.getSlideSize().getX();
int slideHeight = (int) pres.getSlideSize().getY();
int x = (slideWidth - chartWidth * 8) / 2;
int y = (slideHeight - chartHeight * 8) / 2;
OleObjectFrame oof = sld.getShapes().addOleObjectFrame(800, 500, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId = pres.getPictures().add(pic);
oof.setPictureId(picId);
//Write the presentation on disk
pres.write(new FileOutputStream("D:\\Temp\\output.ppt"));
System.out.println("successfully completed!!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}