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

Free Support Forum - aspose.com

Chart.toImage - Axis labels in image are organized differently from actual chart

Hi,

Please find the sample and check the below issue mentioned.

1. My chart contains 10 items on category axis. My chart area is too small to accomodate all items in the chart. Generated excel chart intelligently - vertically align them, and show items in a particular interval.
But the chart.toImage generated image place all items horizontally and overlap each other, which resulted in ugly image. is it possible to ensure exact look and feel of excel chart in the image as well?

Note: I am using latest version - Aspose.CellsV2.1.2.1.zip

-Muhammed

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.lang.reflect.Array;

import com.aspose.cells.Chart;

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.PatternFill;

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 DualAxisChartMaker {

public static void main(String args[]) {

try {

String[] cellsName = new String[] { "A2", "A3", "A4",

"B2", "B3", "B4",

"C2", "C3", "C4",

"D2", "D3", "D4",

"E2", "E3", "E4",

"F2", "F3", "F4",

"G2", "G3", "G4",

"H2", "H3", "H4",

"I2", "I3", "I4",

"J2", "J3", "J4",

"K2", "K3", "K4"

};

//Array of cell data

int[] cellsValue = new int[] { 60, 80, 60,

67, 86, 68,

44, 64, 89,

46, 97, 78,

43, 29, 69,

24, 40, 38,

40, 90, 70,

42, 92, 72,

44, 64, 89,

46, 97, 78,

43, 29, 69

};

Workbook wb = new Workbook();

Worksheet dataSheet = wb.getWorksheets().addSheet();

wb.getPalette().setColor(55, Color.RED);

wb.getPalette().setColor(54, Color.GREEN);

wb.getPalette().setColor(53, Color.YELLOW);

String dataSheetName = "DataSheet";

dataSheet.setName(dataSheetName);

//Populate DataSheet with data

dataSheet.getCells().getCell("A1").setValue("United States");

dataSheet.getCells().getCell("B1").setValue("United Kingdom");

dataSheet.getCells().getCell("C1").setValue("Italy");

dataSheet.getCells().getCell("D1").setValue("Japan");

dataSheet.getCells().getCell("E1").setValue("India");

dataSheet.getCells().getCell("F1").setValue("Canada");

dataSheet.getCells().getCell("G1").setValue("Brazil");

dataSheet.getCells().getCell("H1").setValue("Mexico");

dataSheet.getCells().getCell("I1").setValue("China");

dataSheet.getCells().getCell("J1").setValue("Germany");

dataSheet.getCells().getCell("K1").setValue("Australia");

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 chartRows = 25, chartCols = 15;

Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, 0, 1, 1, 250, 200);

chart.getNSeries().add(dataSheetName + "!A2:H4", false);

chart.getNSeries().get(0).setGapWidth(5);

PatternFill pf = new PatternFill();

pf.setBackgroundColor(wb.getPalette().getColor(53));

pf.setForegroundColor(wb.getPalette().getColor(53));

chart.getNSeries().get(0).getArea().setFill(pf);

chart.getNSeries().get(1).setPlotOnSecondAxis(true);

chart.getNSeries().get(1).setType(ChartType.LINE);

chart.getNSeries().get(1).getBorder().setColor(wb.getPalette().getColor(54));

chart.getNSeries().get(2).setPlotOnSecondAxis(true);

chart.getNSeries().get(2).setType(ChartType.LINE);

chart.getNSeries().get(2).getBorder().setColor(wb.getPalette().getColor(55));

String categoryAreaRef = dataSheetName + "!A1:H1";

chart.getNSeries().setCategoryData(categoryAreaRef);

chart.getNSeries().setSecondCategoryData(categoryAreaRef);

//chart.getCategoryAxis().setRotation(90);

//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, chartRows, 0, chartCols);

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

int slideWidth = (int) pres.getSlideSize().getX() - 1500;

int slideHeight = (int) pres.getSlideSize().getY();

int x = 1500 / 2;

OleObjectFrame oof = sld.getShapes().addOleObjectFrame(x, 0, slideWidth, slideHeight, "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("DualAxisChartMaker successfully completed!!!");

}

catch (Exception e) {

e.printStackTrace();

}

}

}

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

We have found your mentioned issue after an initial test. We will look into it and get back to you soon. Your issue has been registered in our internal issue tracking system with issue id CELLSJAVA-14572.

Thank You & Best Regards,

Hi,

Is there any update on this issue? Can you provide me an ETA for this?

Thanks

Hi,

We will try to provide you a fix in 2-3 days.

Thank you.

Hi,

We are still working on it and will give a new fix in the next
week.

Thank you.

Hi,

We need this very urgently. Is there any update on this issue?

Thanks,

Hi,



As we told you, we are working on your issue and hopefully we can provide you a fix in this week.



Thanks

Hi.

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

For the issue of axis labels, it is a complex issue because the axis label’s layout is different for different size of the charts. The axis labels may be rotated, split into multiple lines, or trimmed automatically according to the chart's size. To make the generated image to be same as shown in MSEXCEL, I am afraid we need more time to study the rules used in MSEXCEL. But before that we will try to provide a new fix soon to make some improvement for the generated image and fix some other issues you have reported.

Thank You & Best Regards,

Hi,

Please try the attached version.

We have made some improvements for the generated image of such kind of Chart. But, as we has said, to remove the issue of overlapped axis labels completely, we need more time to figure it out.
Thanks for your understanding!

Hi,

The axis in chart and images are not similar always. The issue can be categorized into three.
1) Axis label orientation in image is different from actual chart(In chart it may be horizontal whereas in image is is 45 degree rotated - and diff orientation combinations)
2) Axis label orientation in image is same as that of chart, but labels are overlapped.
3) Axis label are shown in chart at a particular interval, where its is shown in image with different interval unit.(Eg:- In chart 5 tick labels are shown, whereas in image 10 labels are shown)

This type of issues are extensively happening in most of the cases, which make the image and chart not compatible.

Thanks & Regards

Muhammed

Hi,

Any update on this?
This is very critical for us. We are facing issues related to this in most of the chart image we generate using aspose.cells.

How can we deliver a product with chart/image conflict as like in the attached image(Label Worst Overlap.PNG) to a customer?

Please try to give a fix asap.

Thanks
Muhammed

Hi,

Please try the attached version.

We have improved the generated image for your issue of overlapped tick
labels. And, we are still working on it and trying to make the tick labels in
generated image look more like what are shown in MS EXCEL in our future
fixes/versions.

Thanks for your understanding!

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


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

It’s much better now.

Still, axis label orientation in image is not same as actual chart axis label orientation.
(Actual chart its vertical, where as image, its 45 degree rotated)
Thanks
Muhammed

Hi,

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

Thank you for the feedback.

Yes, we have found your issue. We will further look into it and get back to you soon.

Thank You & Best Regards,

Hi,

Any update on "axis label orientation in image is not same as actual chart axis label orientation. (Actual chart its vertical, where as image, its 45 degree rotated)".

The issue is reproducible on version 2.2.1.2

Thanks
Muhammed

Hi,

We will try to provide the fix in about 2 days time.

Thank you.

Hi,

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

Please try the attached latest version. We have made certain improvements in image generation as per your reported issue.

Thank You & Best Regards,