Chart.toImage fails when legend box accomodate maximum space

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();
}
}

}

Hi,

After an initial test, we found the issue. We will figure it out soon.

Your issue has been logged into our internal issue tracking system with an issue id: CELLSJAVA-14918.

Thank you.

Hi,

We have fixed the issue of legend box that accommodates maximum space. Please try the attached version.

Thank you.

working.

Thanks
Muhammed

Hi,

similar issue happens for pie chart, when chart height is more and width is less.
Please run the following sample and see the screen shot attached.

Thanks
Muhammed

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import com.aspose.cells.ASeries;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartMarkerType;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.Legend;
import com.aspose.cells.LegendPositionType;
import com.aspose.cells.TickLabelPositionType;
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 PieChartLegend {

public static void main(String args[]) {

try {
String[] cellsName = new String[] { "B2","C2","D2","E2","F2","G2","H2","I2","J2",
"B3","C3","D3","E3","F3","G3","H3","I3","J3",
"B4","C4","D4","E4","F4","G4","H4","I4","J4",
};

//Array of cell data
double[] cellsValue = new double[] {7.027279E7,1744253.0,1473.0,395085.7,7.019342E7,17.0,1111239.0,0.0,5.218978E7,
9.530519E7,2848223.0,6885.9,395216.4,9.729046E7,18.5,900626.8,0.0,4.484255E7,
9.898876E7,1076122.0,1393.5,790165.4,8.850956E7,12.8,1151062.0,0.0,7.406841E7};

Workbook wb = new Workbook();

Worksheet dataSheet = wb.getWorksheets().getSheet(0);
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);

dataSheet.getCells().getCell("A2").setValue("Unit Sales");
dataSheet.getCells().getCell("A3").setValue("Value Sales");
dataSheet.getCells().getCell("A4").setValue("Volume Sales");
dataSheet.getCells().getCell("B1").setValue("BrandA Rinse Conditioners");
dataSheet.getCells().getCell("C1").setValue("BrandB Ltd Rinse Conditioners");
dataSheet.getCells().getCell("D1").setValue("BrandC Rinse Conditioners");
dataSheet.getCells().getCell("E1").setValue("BrandD Rinse Conditioners");
dataSheet.getCells().getCell("F1").setValue("BrandE Rinse Conditioners");
dataSheet.getCells().getCell("G1").setValue("BrandF Rinse Conditioners");
dataSheet.getCells().getCell("H1").setValue("BrandG Rinse Conditioners");
dataSheet.getCells().getCell("I1").setValue("BrandH Rinse Conditioners");
dataSheet.getCells().getCell("J1").setValue("BrandJ Rinse Conditioners");

//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
double cellValue = cellsValue[i];
dataSheet.getCells().getCell(cellName).setValue(cellValue);
}

//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 = 200;
int chartHeight = 500;

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;

double colsPerChart = (double)chartWidthInCols / 4;

int chartStartRow = 0;
int chartStartColumn = 0;
int chartLeft = 0;
int chartTop = 0;

Chart chart = chartSheet.getCharts().addChart(ChartType.PIE, chartStartRow, chartStartColumn, chartLeft, chartTop, chartWidth, chartHeight);
chart.getNSeries().add(dataSheetName + "!B2:J2", false);

//String categoryAreaRef = dataSheet.getName() + "!A2:A4";
String categoryAreaRef = dataSheet.getName() + "!B1:J1";
chart.getNSeries().setCategoryData(categoryAreaRef);

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"));

System.out.println("successfully completed!!!" + CellsHelper.getReleaseVersion());

}
catch (Exception e) {
e.printStackTrace();
}
}
}

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the sample code.

We have found your mentioned issue. We have reopend issue: CELLSJAVA-14918.We will look into it and get back to you soon.

Thank You & Best Regards,

Hi,

Please try the attached version, we have fixed the issue of maximum Legend space for
Pie chart.

Thank you.

Hi,

Working fine.

Thanks
Muhammed

Hi,

Your fix seems to be not perfect.

Please replace the below code in the above example and see. We can see part of pie still in image. Why can't you rely on the the chart area/plot area visibility while generating image?

/*REPLACE the folowing 4 lines with yellow highlighted code*/
legend.setX(100);
legend.setY(100);
legend.setWidth(3900);
legend.setHeight(3800);

legend.setX(50);
legend.setWidth(3750);
legend.setY(100);
legend.setHeight(2000);

Hi,

Thanks for your feedback.

Replacing the code segment with your highlighted lines, we can find the issue. We will figure it out soon.

I have re-opened your issue, we will let you know when it is sorted out.

Thank you.

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

For this issue, we think the generated image is just fine according with the code. The code "legend.setY(100)" should put the Legend to the top of chart area. In the generated excel file, if you drag the Legend to top of the chart, part of the PIE will be shown too just like the generated image. So, the chart in the generated excel file is not right and still the issue is about Legend position. The reason is same with what we have said in your other thread:

Thank You & Best Regards,

The issues you have found earlier (filed as 14918) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi,

This issue is still reproducible

Thanks
Muhammed

Hi,

We will check your issue and get back to you soon.

Thank You & Best Regards,