We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Chart.toImage - Legend formatting not reflected in the image generated

Hi,
Please run the below sample and check the following issues.
1) Legend area in the image is filled with chartarea fill color
2) Legend formatting like bold, italic, color etc not reflected in the image

import java.io.*;
import java.lang.reflect.Array;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.*;
import com.aspose.slides.*;

public class LegendConflict {

public static void main(String args[]) {

try {
String[] cellsName = new String[] { "B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4",
"D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4",
"F1", "F2", "F3", "F4"
};

//Array of cell data
int[] cellsValue = new int[] { 67, 86, 68, 91,
44, 64, 89, 48,
46, 97, 78, 6,
43, 29, 69, 26,
24, 4, 38, 25 };

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

dataSheet.getCells().getCell("A1").setValue("India");
dataSheet.getCells().getCell("A2").setValue("United States");
dataSheet.getCells().getCell("A3").setValue("United Kingdom");
dataSheet.getCells().getCell("A4").setValue("Japan");
dataSheet.getCells().getCell("A5").setValue("China");

//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
int 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 = 400;
int chartHeight = 300;

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

String categoryAreaRef = dataSheet.getName() + "!A1:A5";
chart.getNSeries().setCategoryData(categoryAreaRef);

Font font = chart.getLegend().getFont();
font.setBold(true);
font.setItalic(true);
font.setColor(wb.getPalette().getColor(25));

PatternFill pf1 = new PatternFill();
pf1.setBackgroundColor(wb.getPalette().getColor(30));
pf1.setForegroundColor(wb.getPalette().getColor(30));
chart.getChartArea().getArea().setFill(pf1);

PatternFill pf2 = new PatternFill();
pf2.setBackgroundColor(wb.getPalette().getColor(40));
pf2.setForegroundColor(wb.getPalette().getColor(40));
chart.getChartArea().getArea().setFill(pf2);

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

}

Thanks

-Muhammed

Hi,

We have found both your mentioned issues, we will figure them out soon.

We have logged it into out issue tracking system with an issue id: CELLSJAVA-14682.

Thank you.

Hi,

Please try the attached version.

We have fixed the issue of legend formatting.

Thank you.

Hi,
Issue of legend formatting working fine.
But there are still conflict in the legendbox positioning.
Please find the attachment.
-Muhammed

Hi,



Yes, we noticed the issue, We have re-opened the issue again.



We will inform you when it is sorted out

Thank you.

Hi,

Please try the attached version. We have fixed the issue of Legend position.

Thank you.

Hi,

Legendbox positioning is fine. But still there are major gaps in legend box. Height of legendbox in the image is less compared to chart legendbox. Please find the image attached.

-Muhammed

Hi,

Yes, we noticed the legend’s height difference in the generated image comparing with the actual legend in the template chart. We will look into it soon.

Thank you.

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


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

Hi,

legend’s height difference is still reproducible.

Thanks
Muhammed

Hi,

Yes, apart from Legend’s height issue, other issues are fixed now. We will figure it out soon.

Thank you.