This worked in aspose 7.4.0.3 (yes, really), and no longer works as of 19.8.0.
We started with code to create an Excel document and a chart, and then created an image based on that chart. The chart contains labels. As of 7.4.0.3, this worked. (See images.)
Excel:
image.png (6.3 KB)
PNG:
image.png (17.6 KB)
Now the same using new(er) code (19.8.0):
Excel:
image.png (6.7 KB)
PNG:
image.png (17.4 KB)
Is there a way to make this work using newest aspose? (Note: all code is identical between these two examples; only difference is the aspose jar.)
Our code (which expects the license file to be at File licenseFile = new File(“aspose-licenses/Aspose-Cells-License-New.xml”);):
package com.athenium;
import com.aspose.cells.*;
import java.io.*;
public class AsposeTestHarness {
public static final String CHART_SHEET_NAME = "chart";
public static final String DATA_SHEET_NAME = "data";
public static void main(String[] args) {
initNewLicense();
// init workbook
Workbook wb = new Workbook();
WorksheetCollection sheets = wb.getWorksheets();
sheets.clear();
// init sheets
Worksheet chartSheet = sheets.add(AsposeTestHarness.CHART_SHEET_NAME);
ChartCollection charts = chartSheet.getCharts();
Worksheet dataSheet = wb.getWorksheets().add(DATA_SHEET_NAME);
Cells dataSheetCells = dataSheet.getCells();
// init chart
Chart chart = charts.get(charts.add(ChartType.BAR_100_PERCENT_STACKED, 0, 0, 20, 10));
SeriesCollection nSeries = chart.getNSeries();
// write data
dataSheetCells.get(0,1).setValue("Incorrect %");
dataSheetCells.get(0,2).setValue("Correct %");
dataSheetCells.get(1,0).setValue("Question 1");
dataSheetCells.get(1,1).setValue(0.12);
dataSheetCells.get(1,2).setValue(0.88);
dataSheetCells.get(2,0).setValue("Question 2");
dataSheetCells.get(2,1).setValue(0.21);
dataSheetCells.get(2,2).setValue(0.79);
dataSheetCells.createRange(1,0,2,1).setName("Categories");
dataSheetCells.createRange(1,1,2,1).setName("IncorrectPct");
dataSheetCells.createRange(1,2,2,1).setName("CorrectPct");
Series incorrectSeries = nSeries.get(nSeries.add("IncorrectPct", true));
incorrectSeries.setName("Incorrect %");
Series correctSeries = nSeries.get(nSeries.add("CorrectPct", true));
correctSeries.setName("Correct %");
nSeries.setCategoryData("Categories");
// format labels
DataLabels incorrectLabels = incorrectSeries.getDataLabels();
DataLabels correctLabels = correctSeries.getDataLabels();
incorrectLabels.setShowLegendKey(false);
correctLabels.setShowLegendKey(false);
incorrectLabels.setAutoText(true);
correctLabels.setAutoText(true);
incorrectSeries.setHasLeaderLines(true);
correctSeries.setHasLeaderLines(true);
incorrectLabels.setShowSeriesName(false);
correctLabels.setShowSeriesName(false);
incorrectLabels.setShowCategoryName(false);
correctLabels.setShowCategoryName(false);
incorrectLabels.setShowValue(true);
correctLabels.setShowValue(true);
incorrectLabels.setShowPercentage(true);
correctLabels.setShowPercentage(true);
incorrectLabels.setShowBubbleSize(true);
correctLabels.setShowBubbleSize(true);
incorrectLabels.setAutoScaleFont(false);
correctLabels.setNumberFormat("0.00");
incorrectLabels.setBackgroundMode(BackgroundMode.OPAQUE);
correctLabels.setBackgroundMode(BackgroundMode.TRANSPARENT);
// write excel file
writeFile(wb);
// write image file
ImageOrPrintOptions ops = new ImageOrPrintOptions();
ops.setChartImageType(ImageFormat.getImageFormatFromSuffixName("png"));
try {
FileOutputStream fos = new FileOutputStream(new File("out/img.png"));
chart.toImage(fos, ops);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void initNewLicense() {
try {
File licenseFile = new File("aspose-licenses/Aspose-Cells-License-New.xml");
InputStream licenseFileStream = new FileInputStream(licenseFile);
License asposeCellsLicense = new License();
asposeCellsLicense.setLicense(licenseFileStream);
if (License.isLicenseSet()) {
System.out.println("License OK");
} else {
System.out.println("License NOT set!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void writeFile(Workbook wb) {
try {
wb.save("out/workbook.xls", SaveFormat.EXCEL_97_TO_2003);
} catch (Exception e) {
e.printStackTrace();
}
}
}