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