Hi Team,
I have a requirement to position the legend based on the chart size.
I have a sample bar chart and the snipper is as follows.
// init workbook
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_STACKED, 0, 0, 20, 10));
SeriesCollection nSeries = chart.getNSeries();
// write data
dataSheetCells.get(0,1).setValue("Jan 22");
dataSheetCells.get(0,2).setValue("Feb 22");
dataSheetCells.get(1,0).setValue("Costco");
dataSheetCells.get(1,1).setValue(5543);
dataSheetCells.get(1,2).setValue(6543);
dataSheetCells.get(2,0).setValue("Walmart");
dataSheetCells.get(2,1).setValue(3456);
dataSheetCells.get(2,2).setValue(5643);
dataSheetCells.createRange(1,0,2,1).setName("Retailer");
dataSheetCells.createRange(1,1,2,1).setName("Jan");
dataSheetCells.createRange(1,2,2,1).setName("Feb");
Series janSeries = nSeries.get(nSeries.add("Jan", true));
janSeries.setName("Jan sales");
Series febSeries = nSeries.get(nSeries.add("Feb", true));
febSeries.setName("Feb Sales");
nSeries.setCategoryData("Retailer");
try {
wb.save("C:\\Users\\StackedBarTestLegend.xlsx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This code generates the chart as below
image.png (33.8 KB)
But now I have to make the legend to occupy 40% of the chart. And this has to be dynamic based on the chart’s size, width and height.
So that legend and chart should look like below
image.png (29.7 KB)
Could you please help me with the code snippet which helps me adjust the legend size to 40% of the chart size?
Attaching the required excel here →
StackedBarTestLegend.zip (10.0 KB)
Thanks in advance.
- Thilak Babu