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

Free Support Forum - aspose.com

Aspose.Cells java - chart.toImage generated image fails to show GradientFill applied on series and shows wrong legend colors in legend box

Hi,
Plaese run the below example and find the error mentioned below. Please refer attached image for more details.

1. chart.toImage generated image fails to show GradientFill applied on series and shows wrong legend colors in legend box

-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.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 VerticalBarChartMaker {

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, 60,
43, -29, 69, 26,
24, 40, -38, 25 };

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

dataSheet.getCells().getCell("A1").setValue("IN");
dataSheet.getCells().getCell("A2").setValue("US");
dataSheet.getCells().getCell("A3").setValue("UK");
dataSheet.getCells().getCell("A4").setValue("JP");
dataSheet.getCells().getCell("A5").setValue("CH");

//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 = 600;
int chartHeight = 400;

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;

System.out.println("OLE SIZE = Cols: " + chartWidthInCols + ", Rows: " + chartHeightInRows);
System.out.println("CHART SIZE = chartWidth: " + chartWidth + ", chartHeight: " + chartHeight);

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

GradientFill gf = new GradientFill();
gf.setGradientColorType(GradientColorType.TWO_COLORS);
gf.setColor1(wb.getPalette().getColor(i));
gf.setColor2(wb.getPalette().getColor(i * 2));
gf.setDirection(GradientDirectionType.FROM_CENTER);
series.getArea().setFill(gf);
}

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

chart.getCategoryAxis().setDisplayUnitLabelShown(false);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.NONE);

chart.getTitle().setText("Aspose.Cells");

//Trendlines
chart.getNSeries().get(0).getTrendlines().addTrendline(TrendlineType.LINEAR);


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

pres.write(new FileOutputStream("D:\\Temp\\output.ppt"));
System.out.println("successfully completed!!!");

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

}

Hi,

Well, I have afraid it is not a bug but rather a limitation for the chart to image feature. Gradient FillFormat effect is not supported at the moment. Check the document: http://www.aspose.com/documentation/java-components/aspose.cells-for-java/converting-chart-to-image.html

I have logged it into our issue tracking system with an issue id: CELLSJAVA-14624. We will let you know about it when we sorted out.

Thank you.

Can you atleast show exact legend colors instead of all coming black?

Hi,

Thanks for your suggestion.

Yes, we will surely check if this can be done at the moment for your need, we will figure it out soon.

Thank you.

Hi,

Please try the attached version/fix.

In the new fix we have made the legend colors same with corresponding series. For GradientFill, it is a bit complex task and we will support it
in our future versions.

Thank you.

Hi,
Legend entry color is fine now.
Thanks

-Muhammed

Hi,

Can you run the above example by adding the following line, to apply GradientFill color for series Border as well?

series.getBorder().setFill(gf);

See the screenshot for details.

Thanks
Muhammed

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

We have found your mentioned issue. We will look into it and get back to you soon.

Thank You & Best Regards,

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


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

Hi,

GradientFill is perfectly applied in the image generated. Thanks a lot.

I found two more issues in the image generated with GradientFill.

1. Legend entries in image still shows solid fill.
2. Border fill is not reflected in image

Please find the attachment.

Thanks
Muhammed

Hi,

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

Thank you for the feedback.

We will further look into your reported issues and get back to you soon. Your issues have been registered in our internal issue tracking system with issue id: CELLSJAVA-16109.

Thank You & Best Regards,

Hi,

Please try the attached version. We have fixed the issue now.

Thank you.

Hi,

The following issues are closed.

1. Legend entries in image still shows solid fill.
2. Border fill is not reflected in image

BUT, the legend marker shape differs in actual chart and image when I try the sample with version 2.2.1.2

Please find screen image attached.
Thanks
Muhammed

Hi,

Yes, we noticed the difference in legend box, we will figure your issue out soon.

I have re-opened your issue.

Thank you.

Hi,

After further analysis of your issue, we find the legend marker shapes differ because the chart in Excel file has trend line in it. I am afraid, we cannot support trend line at the moment. We might look into it in future versions.

Thank you.