Free Support Forum - aspose.com

Rotate labels on category axis

hello!
I've successfully created a chart in excel using
the cells for java version 2.1.2.7 library...

the line of code
chart.getNSeries().setCategoryData("DataSheet!B1:F1");
correctly adds labels to the the x-axis, is there a way
to rotate these labels?

thank you!

Hi,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Please try Chart.getCategoryAxis().setRotation(45) . Please see the following sample code in this regard:

//Instantiating a Workbook object

Workbook workbook = new Workbook();

//Obtaining the reference of the first worksheet

Worksheets worksheets = workbook.getWorksheets();

Worksheet sheet = worksheets.getSheet(0);

//Adding some sample value to cells

Cells cells = sheet.getCells();

Cell cell = cells.getCell("A1");

cell.setValue(50);

cell = cells.getCell("A2");

cell. setValue (100);

cell = cells.getCell("A3");

cell.setValue(150);

cell = cells.getCell("B1");

cell.setValue(4);

cell = cells.getCell("B2");

cell.setValue(20);

cell = cells.getCell("B3");

cell.setValue(50);

Charts charts = sheet.getCharts();

//Adding a chart to the worksheet

Chart chart = charts.addChart(ChartType.PYRAMID,5,0,15,5);

//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"

NSeries series = chart.getNSeries();

series.add("A1:B3", true);

chart.getCategoryAxis().setRotation(45);

//Saving the Excel file

workbook.save("C:\\book1.xls");

Thank You & Best Regards,

hello Nausherwan...thanks for your help...

the code you provided works...but what
if we alter the population of worksheet cells
to look like this:

Cell cell = cells.getCell("A2");
cell.setValue("Series 1");
cell = cells.getCell("A3");
cell.setValue("Series 2");
cell = cells.getCell("A4");
cell.setValue("Series 3");

cell = cells.getCell("B1");
cell.setValue("B-GROUP");
cell = cells.getCell("B2");
cell. setValue (100);
cell = cells.getCell("B3");
cell.setValue(150);
cell = cells.getCell("B4");
cell.setValue(75);

cell = cells.getCell("C1");
cell.setValue("C-GROUP");
cell = cells.getCell("C2");
cell.setValue(20);
cell = cells.getCell("C3");
cell.setValue(50);
cell = cells.getCell("C4");
cell.setValue(90);

then want to have "B-GROUP", and "C-GROUP"
appear as rotated labels on the X-Axis?

also, how to make the values in column A
appear on the Y-Axis?

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for the feedback.

I have updated the code as per your feedback. Please check it and let us know if it is fine as per your requirement. In case you have any different requirement then this, please create the chart manually in MS Excel and post it here. We will check it soon.

//Instantiating a Workbook object

Workbook workbook = new Workbook();

//Obtaining the reference of the first worksheet

Worksheets worksheets = workbook.getWorksheets();

Worksheet sheet = worksheets.getSheet(0);

//Adding some sample value to cells

Cells cells = sheet.getCells();

Cell cell = cells.getCell("A2");

cell.setValue("Series 1");

cell = cells.getCell("A3");

cell.setValue("Series 2");

cell = cells.getCell("A4");

cell.setValue("Series 3");

cell = cells.getCell("B1");

cell.setValue("B-GROUP");

cell = cells.getCell("B2");

cell. setValue (100);

cell = cells.getCell("B3");

cell.setValue(150);

cell = cells.getCell("B4");

cell.setValue(75);

cell = cells.getCell("C1");

cell.setValue("C-GROUP");

cell = cells.getCell("C2");

cell.setValue(20);

cell = cells.getCell("C3");

cell.setValue(50);

cell = cells.getCell("C4");

cell.setValue(90);

Charts charts = sheet.getCharts();

//Adding a chart to the worksheet

Chart chart = charts.addChart(ChartType.PYRAMID,5,0,15,5);

//Adding NSeries (chart data source) to the chart

NSeries series = chart.getNSeries();

series.add("B1:C4", false);

series.setCategoryData("B1:C1");

for(int i=0;i<series.size();i++)

{

series.get(i).setName("=A"+(i+2));

}

chart.getCategoryAxis().setRotation(45);

//Saving the Excel file

workbook.save("D:\\book1.xls");

Thank You & Best Regards,

hello Nausherwan,
thanks for the help...
the updated code works great...

it looks so much like my code I couldn't figure
out the difference -- until I realized:
I'm creating a workbook in one method, then passing the
open workbook to other methods in other classes to perform
different operations...then eventually returning to the original
method to save the workbook...
from time to time this seems to cause erratic behavior...

using a single method to perform all operations on a workbook
seems to work perfectly...

can you say if anyone else has reported this sort of behavior?

Thank You!

Hi,

Thank you for the feedback.

Well, I am not very clear about your issue. Please share your complete sample code to show the issue and we will look into it soon.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank You & Best Regards,