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

Free Support Forum - aspose.com

Asian characters in chart appear as squares in chart.toImage in Java

Hi Guys,

When I create an image file from an excel pie chart using chart.toImage the legend labels appear as squares in the image (when the labels are in Chinese, Korean or Japanese).

I see this issue was raised in forum post 312946 some years back. It as listed as fix 28779 in Cells for Java 7.0.0 but I am getting it now in a product I’m working on that is using cells 7.7.1

Here’s what I’m doing:

ImageOrPrintOptions ImageOptions = new ImageOrPrintOptions();
ImageOptions.setImageFormat(ImageFormat.getPng());
ImageOptions.setQuality(100);

ByteArrayOutputStream out = new ByteArrayOutputStream();
chart.toImage(out, ImageOptions);
byte[] data = out.toByteArray();
BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));

return image;


Thanks

Hi,

Thanks for your posting and using Aspose.Cells.

First you need to know which of the fonts your chart is using. Suppose it is using four fonts abc, def, ghi, jkl.

Then you should place all of these fonts in some directory say d:\myfonts etc

Now before you execute your code, you should set the font directory like

CellsHelper.setFontDir(“d:\myfonts”);

Now your chart will be rendered fine.

Hi Shakeel,

Thanks for your reply. I still can’t seem to get this to work though. I have set the legend font to Times New Roman and put all the Times New Roman .tff files in a folder and then used setFontDir() as you suggested (at various points in the code before the chart is created). I have also tried pointing setFontDir() to my Windows.Fonts directory but still not joy.

The chart legend text comes out fine in the .xlsx file, it’s just when I use toImage() to create .png file that it fails to render properly in the .png.

Any suggestions?

Thanks.

Hi Stephen,


We are sorry to know that the proposed solution didn’t help in your scenario. Please note, you do not need to set the font directory repeatedly, you have to do it once before initializing the instance of License class (for older revisions of the API). Anyway, if you are not able to get the desired results, please provide us the sample spreadsheet along with the font files (TTFs) which are being used in the spreadsheet for formatting. We will perform tests on our end to further assist you in this regard. Please also provide the environment details as follow.

  1. Operating system version & architecture
  2. JDK vendor, version & architecture
  3. JVM arguments set for the process, if any
  4. Locale/Regional settings of your machine

Hi Babar,

I have attached an example project that just creates a worksheet with a chart and then saves the chart as a png. I have also included the times font files from my machine as well as the images created using this project with Cells 7.7.1 and 8.6.3.

The project is currently using Cells 7.7.1 and this produces the doughnut chart image with problem characters. If you switch the library to Cells 8.6.3 it works fine.

Unfortunately updating the library to 8.6.3 is not going to be an option for me with the product I am working on. If there is anything I could do to get it working in 7.7.1 that would be great.

My environment is:

1. Win 8.1 Pro 64-bit, x64
2. JDK 1.8, Oracle, 32-bit
3. No args passed to JVM
4. Location: United Kingdom, Language: English (United Kingdom)


Many Thanks.

Hi Stephen,


Thank you for providing further details and sample application.

I have evaluated the scenario while using Aspose.Cells for Java 7.7.1, 8.6.3 & 8.7.0.4 (latest). All aforementioned revisions except 7.7.1 are producing expected results (attached) that makes me believe that the problem is more related to the specific version of the API rather than missing or miss-configuration of the fonts. For now I can only suggest you to upgrade your project to use more recent revisions of the API.

Hi again,


I am able to get correct results by changing the legend’s font to Arial Unicode MS (available in Windows platform) while using Aspose.Cells for Java 7.7.1. Could you please try the following piece of code on your end as well?

Java

Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName(“Data”);
Cells cells = workbook.getWorksheets().get(0).getCells();

//Put some values into a cells of the Data sheet.
cells.get(“A1”).setValue(“Region”);
cells.get(“A2”).setValue(“France”);
cells.get(“A3”).setValue(“本评估”);
cells.get(“A4”).setValue(“England”);
cells.get(“A5”).setValue(“Sweden”);
cells.get(“A6”).setValue(“本评估”);
cells.get(“A7”).setValue(“Spain”);
cells.get(“A8”).setValue(“Portugal”);
cells.get(“B1”).setValue(“Sale”);

cells.get(“B2”).setValue(70000);
cells.get(“B3”).setValue(55000);
cells.get(“B4”).setValue(30000);
cells.get(“B5”).setValue(40000);
cells.get(“B6”).setValue(35000);
cells.get(“B7”).setValue(32000);
cells.get(“B8”).setValue(10000);

//Create chart
int chartIndex = sheet.getCharts().add(ChartType.DOUGHNUT, 12, 1, 33, 12);
Chart chart = sheet.getCharts().get(chartIndex);

//Set properties of chart title
chart.getTitle().setText(“Sales By Region”);
chart.getTitle().getTextFont().setBold(true);
chart.getTitle().getTextFont().setSize(12);

//Set properties of nseries
chart.getNSeries().add(“Data!B2:B8”, true);
chart.getNSeries().setCategoryData(“Data!A2:A8”);

//Set the fill colors for the series’s data points (France - Portugal(7 points))
ChartPointCollection chartPoints = chart.getNSeries().get(0).getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());
point = chartPoints.get(1);
point.getArea().setForegroundColor(Color.getBlue());
point = chartPoints.get(2);
point.getArea().setForegroundColor(Color.getYellow());
point = chartPoints.get(3);
point.getArea().setForegroundColor(Color.getRed());
point = chartPoints.get(4);
point.getArea().setForegroundColor(Color.getBlack());
point = chartPoints.get(5);
point.getArea().setForegroundColor(Color.getGreen());
point = chartPoints.get(6);
point.getArea().setForegroundColor(Color.getMaroon());

//Set the legend invisible
chart.setShowLegend(true);

//Set legends font
chart.getLegend().getFont().setName(“Arial Unicode MS”);

//Get the Chart image
ImageOrPrintOptions imgOpts = new ImageOrPrintOptions();
imgOpts.setImageFormat(ImageFormat.getPng());
chart.toImage(dir + “output “+CellsHelper.getVersion()+”.png”, imgOpts);

Hi Babar,

Works for me, thank you so much.

Great support, as always!

Cheers guys.

Hi Stephen,


It is good to know that you are up & running again. Please feel free to contact us back in case you need our further assistance with Aspose APIs.