Hi,
Thank you for replay.
I did try to call method Chart.calculate(), but that didn’t make any different.
I tried to set font size but that didn’t help either. I think that font size and legend size should get resized automatically because that by default Legend’s AutoScaleFont and AutomaticSize is set to true.
I don’t use any excel templates. I populate excel sheet and then I converting it to PDF. (workbook.save(outputStream, FileFormatType.PDF);).
I attached very simplified java code which will reproduce this graphic issue.
import com.aspose.cells.*;
public class Test {
private static boolean doneLicense;
public static void main(String[] args) throws Exception {
setLicense();
Workbook wb = new Workbook();
Worksheet sheet = wb.getWorksheets().get(0);
int index = sheet.getCharts().add(ChartType.COLUMN, 0, 0, 20, 20);
sheet.getPageSetup().setPaperSize(PaperSizeType.PAPER_A_4);
sheet.getPageSetup().setOrientation(PageOrientationType.LANDSCAPE);
sheet.getPageSetup().setFitToPagesTall(1);
sheet.getPageSetup().setFitToPagesWide(1);
Chart chart = sheet.getCharts().get(index);
String[][] data = { { "88", "44", "12" }, { "23", "77", "77" }, { "55", "78", "80" }, };
String[] categoryData = { "one", "two", "three" };
String[] series = { "Some text here", "Somer long text here 38838" , "Some another very long name 0032841399"};
buildGraph(chart, data, categoryData, series, "Some Title");
wb.save("chart.pdf", FileFormatType.PDF);
System.out.println("Done!");
}
public static void buildGraph(Chart chart, String[][] data, String[] categoryData, String[] series, String title) throws Exception {
// format graph
SeriesCollection nSeries = chart.getNSeries();
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
legend.setAutomaticSize(true);
legend.setAutoScaleFont(true);
legend.getFont().setSize(6);
chart.getTitle().setText(title);
chart.getTitle().setShadow(true);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.LOW);
chart.getCategoryAxis().getTickLabels().setRotationAngle(90);
// add data and collect categories information
for (int r = 0; r < data.length; r++) {
String[] rowData = data[r];
int index = nSeries.add(getSeriesValuesStringVersion(rowData), true);
nSeries.get(index).setName(series[r]);
}
nSeries.setCategoryData(getSeriesValuesStringVersion(categoryData));
chart.calculate();
}
public static String getSeriesValuesStringVersion(String[] data) {
StringBuilder builder = new StringBuilder("{");
boolean firstElement = true;
for (String d : data) {
if (firstElement) {
builder.append(d);
firstElement = false;
} else {
builder.append(", ");
builder.append(d);
}
}
builder.append("}");
return builder.toString();
}
/**
* Loads the Aspose license.
*
* @throws Exception If we can't load the license.
*/
private static void setLicense() throws Exception {
if (doneLicense) {
return; // Only need to do it once.
}
// Get the Aspose license file, and load it into Aspose.
License license = new License();
license.setLicense("Aspose.Java.lic");
doneLicense = true;
}
}import com.aspose.cells.*;
public class Test {
private static boolean doneLicense;
public static void main(String[] args) throws Exception {
setLicense();
Workbook wb = new Workbook();
Worksheet sheet = wb.getWorksheets().get(0);
int index = sheet.getCharts().add(ChartType.COLUMN, 0, 0, 20, 20);
sheet.getPageSetup().setPaperSize(PaperSizeType.PAPER_A_4);
sheet.getPageSetup().setOrientation(PageOrientationType.LANDSCAPE);
sheet.getPageSetup().setFitToPagesTall(1);
sheet.getPageSetup().setFitToPagesWide(1);
Chart chart = sheet.getCharts().get(index);
String[][] data = { { "88", "44", "12" }, { "23", "77", "77" }, { "55", "78", "80" }, };
String[] categoryData = { "one", "two", "three" };
String[] series = { "Some text here", "Somer long text here 38838" , "Some another very long name 0032841399"};
buildGraph(chart, data, categoryData, series, "Some Title");
wb.save("chart.pdf", FileFormatType.PDF);
System.out.println("Done!");
}
public static void buildGraph(Chart chart, String[][] data, String[] categoryData, String[] series, String title) throws Exception {
// format graph
SeriesCollection nSeries = chart.getNSeries();
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
legend.setAutomaticSize(true);
legend.setAutoScaleFont(true);
legend.getFont().setSize(6);
chart.getTitle().setText(title);
chart.getTitle().setShadow(true);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.LOW);
chart.getCategoryAxis().getTickLabels().setRotationAngle(90);
// add data and collect categories information
for (int r = 0; r < data.length; r++) {
String[] rowData = data[r];
int index = nSeries.add(getSeriesValuesStringVersion(rowData), true);
nSeries.get(index).setName(series[r]);
}
nSeries.setCategoryData(getSeriesValuesStringVersion(categoryData));
chart.calculate();
}
public static String getSeriesValuesStringVersion(String[] data) {
StringBuilder builder = new StringBuilder("{");
boolean firstElement = true;
for (String d : data) {
if (firstElement) {
builder.append(d);
firstElement = false;
} else {
builder.append(", ");
builder.append(d);
}
}
builder.append("}");
return builder.toString();
}
/**
* Loads the Aspose license.
*
* @throws Exception If we can't load the license.
*/
private static void setLicense() throws Exception {
if (doneLicense) {
return; // Only need to do it once.
}
// Get the Aspose license file, and load it into Aspose.
License license = new License();
license.setLicense("Aspose.Java.lic");
doneLicense = true;
}
}