Hi Team,
I am trying to render a Bar chart with category values reversed.
And I am trying to rotate the ticklabels by -30 degree.
On doing this, My chart is getting disappeared.
Code snippet :
Workbook wb = new Workbook();
WorksheetCollection sheets = wb.getWorksheets();
// init sheets
int i = sheets.add();
Worksheet chartSheet = sheets.get(i);
ChartCollection charts = chartSheet.getCharts();
Worksheet dataSheet = sheets.get(0);
Cells dataSheetCells = dataSheet.getCells();
// init chart
Chart chart = charts.get(charts.add(com.aspose.cells.ChartType.BAR, 0, 0, 20, 10));
SeriesCollection nSeries = chart.getNSeries();
chart.getPlotArea().getArea().setForegroundColor(com.aspose.cells.Color.getWhite());
// remove major gridlines
chart.getValueAxis().getMajorGridLines().setVisible(false);
// write data
dataSheetCells.get("A1").setValue("Division");
dataSheetCells.get("A2").setValue("TV");
dataSheetCells.get("B1").setValue("Geo");
dataSheetCells.get("B2").setValue("Total");
dataSheetCells.get("C1").setValue("$");
dataSheetCells.get("C2").setValue(8246833088.5942);
Style style = dataSheetCells.get("C2").getStyle();
style.setCustom("[>999999999]\\$#.00,,,\"B\";[>999999] \\$#.00,,\"M\"; \\$#.00");
dataSheetCells.get("C2").setStyle(style);
Series firstSeries = nSeries.get(nSeries.add("Total", true));
firstSeries.setValues("Sheet1!$C$2");
firstSeries.setName("Total");
dataSheetCells.createRange("A2", "A2").setName("TV");
nSeries.setCategoryData("TV");
chart.calculate();
chart.getCategoryAxis().getTickLabels().setRotationAngle(-30);
chart.getValueAxis().getTickLabels().setRotationAngle(-30);
chart.getValueAxis().setTickLabelPosition(TickLabelPositionType.HIGH);
//turn categories in reverse order
chart.getCategoryAxis().setPlotOrderReversed(true);
try {
wb.save("C:\\Users\\someplace\\BarRotation.xlsx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
When ticklabels rotated 30 I get the chart like below
image.png (69.8 KB)
But when I make the rotation angle as 0, I get the chart like below
image.png (67.5 KB)
Attaching the excel file →
BarRotation2.zip (10.0 KB)
Could you please check and update why the chart gets disappeared when rotating by 30?
Note - Also categories with reverse order is also needed for my requirement.
Thanks,
Thilak Babu
@Thilakbabu
Hello Thilak Babu,
Thank you for reaching out and providing detailed information about the issue you’re encountering with Aspose.Cells for Java.
Issue Description
You are experiencing a problem where the Bar chart disappears when rotating the tick labels by 30 degrees. Additionally, you need the category values to be in reverse order.
Solution
The issue you are encountering might be due to certain settings or limitations in the current version of Aspose.Cells. Here are a few steps you can take to address this issue:
-
Update Aspose.Cells Library:
Ensure you are using the latest version of Aspose.Cells. Sometimes, bugs are fixed in newer versions.
-
Adjust Chart Size:
When rotating tick labels, sometimes the chart area might not adjust properly. Try increasing the size of the chart area to ensure that all elements are visible.
-
Check for Overlapping Elements:
Ensure that no other elements on the worksheet are overlapping the chart area.
-
Customize Chart Area:
Adjust the plot area and margins of the chart to accommodate the rotated tick labels.
Sample Code Adjustment
Here is a modified version of your code with some additional settings that might help:
import com.aspose.cells.*;
public class BarChartExample {
public static void main(String[] args) {
Workbook wb = new Workbook();
WorksheetCollection sheets = wb.getWorksheets();
// init sheets
int i = sheets.add();
Worksheet chartSheet = sheets.get(i);
ChartCollection charts = chartSheet.getCharts();
Worksheet dataSheet = sheets.get(0);
Cells dataSheetCells = dataSheet.getCells();
// init chart
Chart chart = charts.get(charts.add(ChartType.BAR, 0, 0, 20, 10));
SeriesCollection nSeries = chart.getNSeries();
chart.getPlotArea().getArea().setForegroundColor(Color.getWhite());
// remove major gridlines
chart.getValueAxis().getMajorGridLines().setVisible(false);
// write data
dataSheetCells.get("A1").setValue("Division");
dataSheetCells.get("A2").setValue("TV");
dataSheetCells.get("B1").setValue("Geo");
dataSheetCells.get("B2").setValue("Total");
dataSheetCells.get("C1").setValue("$");
dataSheetCells.get("C2").setValue(8246833088.5942);
Style style = dataSheetCells.get("C2").getStyle();
style.setCustom("[>999999999]\\$#.00,,,\"B\";[>999999] \\$#.00,,\"M\"; \\$#.00");
dataSheetCells.get("C2").setStyle(style);
Series firstSeries = nSeries.get(nSeries.add("Total", true));
firstSeries.setValues("Sheet1!$C$2");
firstSeries.setName("Total");
dataSheetCells.createRange("A2", "A2").setName("TV");
nSeries.setCategoryData("TV");
chart.calculate();
chart.getCategoryAxis().getTickLabels().setRotationAngle(-30);
chart.getValueAxis().getTickLabels().setRotationAngle(-30);
chart.getValueAxis().setTickLabelPosition(TickLabelPositionType.HIGH);
// Turn categories in reverse order
chart.getCategoryAxis().setPlotOrderReversed(true);
// Adjust chart area size
chart.setTop(50);
chart.setLeft(50);
chart.setWidth(400);
chart.setHeight(250);
try {
wb.save("C:\\Users\\someplace\\BarRotation.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Additional Tips
Please try the above adjustments and let us know if the issue is resolved. If you need further assistance, feel free to ask!
Best Regards,
[Your Name]
Aspose Technical Support
@Thilakbabu
This is the expected behavior. If you change the angle of the axis to 30 in Excel, the chart will also be lost. Because the content of the chart has already been rotated out of the plane coordinates.