Chart legend text overlapping

I have issue with Aspose.Cells. I use Aspose.Cells to generate PDF. I managed to generate graph but graph have some problems with text overlapping in chart legend. When legend was set to be positioned on the left side everything looked ok but when I tried to position legend on the bottom then text of legend started to overlap. Legend should resize itself automatically. Sometimes it does, sometimes it fails to. I could resize it myself and keep size static but then it would be ok just in some cases. I tried to find way to measure legend text and then set size dynamically using peace of code but unfortunately I wasn’t provided with enough data to do that. I am not sure if it's a bug or I do something wrong. This is peace of code I use to position the legend:

Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
legend.setAutomaticSize(true);
legend.setAutoScaleFont(true);

I also attached generated pdf file.

Hi,


Well, you may try to decrease the font size of the legend items/entries (e.g you may try: chart.getLegend().getLegendEntries().get(0).getFont()…) a bit if it works, also you may try to call/use the Chart.calculate() method if it works a bit.

If you still could not evaluate your issue, kindly create a sample JAVA console program and attach/paste the sample code (runnable) here with all the Excel files, we will check your issue soon.

Thank you.


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;

}
}

Hi,


Thanks for the sample code.

After an initial test, I can notice the issue in the rendered PDF file, the chart’s legend entries are overlapped a bit. If we render to XLSX file format by adding a line (as below) to your code, the XLSX has the chart and its legend is rendered fine, the legend entries are not overlapped.

buildGraph(chart, data, categoryData, series, “Some Title”);
wb.save(“chart.pdf”, FileFormatType.PDF);
wb.save(“chart.xlsx”, SaveFormat.XLSX);
System.out.println(“Done!”);

I have logged a ticket with an id “CELLSJAVA-40514” for your issue. We will look into it soon.

Once we have any update on it, we will let you know here.

Thank you.

Hi,

I would like to know what is a status of ticket (id: "CELLSJAVA-40514"). Can you please tell me how long it may take to resolve this issue. Thank you.

Kind Regards,

Alikas Jesatovas

Hi,


I am afraid, your issue is not resolved yet, it is currently “in process”. However, I have asked the relevant developer to update on your issue or share an eta (if possible). Once we receive an update on it, we will let you know here.

Thank you.

Hi,


Please try the new fix/version: Aspose.Cells for .NET v7.5.0.1, your issue should be fixed in it.

Thank you

Hi,

Is it fixed on Aspose.Cells for Java or just for .NET?

Thanks

Alikas

Hi,

Thanks for your posting and using Aspose.Cells for Java.

We have fixed this issue. Please download and try the latest fix: Aspose.Cells
for Java v7.5.0.1
.

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


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.