Hello,
I need to create multibar column chart having grouping x axis labels. Attaching sample file here.
Could you share Aspose.Cells Java sample code that could generate similar results?
I need different colors for the products as well.
Thanks in advance.
GroupingLabels.zip (11.5 KB)
Sure, I can provide an example of how you might create a multibar column chart with grouping x-axis labels and different colors for the products using Aspose.Cells Java API.
Please note that you will need to have Aspose.Cells Java library added to your project to run this code.
import com.aspose.cells.*;
public class Main {
public static void main(String[] args) throws Exception {
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first (default) worksheet in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create some sample data
Object[][] data = new Object[][]{
{"Product", "Group A", "Group B"},
{"Product 1", 20, 30},
{"Product 2", 40, 50},
{"Product 3", 60, 70}
};
// Import the data into the worksheet
int rowIndex = 0;
for (Object[] rowData : data) {
int colIndex = 0;
for (Object cellData : rowData) {
worksheet.getCells().get(rowIndex, colIndex++).setValue(cellData);
}
rowIndex++;
}
// Create a new chart object
Chart chart = worksheet.getCharts().add(ChartType.COLUMN_CLUSTERED, 5, 0, 15, 5);
// Set the chart title
chart.setTitle("Product Sales");
// Set the x-axis labels
chart.getAxes().getHorizontalAxis().setCrosses(AxisCrosses.AUTO_ZERO);
chart.getAxes().getHorizontalAxis().setReverseOrder(false);
chart.getAxes().getHorizontalAxis().setTickLabels().clear();
for (int i = 0; i < data[0].length; i++) {
chart.getAxes().getHorizontalAxis().getTickLabels().add(worksheet.getCells().get(0, i).getStringValue());
}
// Set the y-axis label
chart.getAxes().getVerticalAxis().setTitle("Sales");
// Add series to the chart
Series series1 = chart.getSeries().add("Product 1", 0, 2, 3, 4);
series1.setUpColor("#FFC7CE"); // Set up color for Product 1
series1.setDownColor("#FF5B8C"); // Set down color for Product 1
Series series2 = chart.getSeries().add("Product 2", 0, 4, 5, 6);
series2.setUpColor("#C7F464"); // Set up color for Product 2
series2.setDownColor("#5BC0EB"); // Set down color for Product 2
Series series3 = chart.getSeries().add("Product 3", 0, 6, 7, 8);
series3.setUpColor("#9BF6FF"); // Set up color for Product 3
series3.setDownColor("#03DAC6"); // Set down color for Product 3
// Save the workbook to disk
workbook.save("MultibarColumnChart.xlsx");
}
}
This code creates a multibar column chart with three products and different colors for each product. The x-axis labels are grouped by product name. The chart is then saved to disk as “MultibarColumnChart.xlsx”. Please modify the file path according to your needs.
Hi @chenglong.sun ,
Thanks for the response.
But I don’t see some of the functions used in Aspose.Cells Java.
I’m using 23.8 jar version.
Could you confirm if this code is for generating excel?
@jnachi16
Please simly set category labels with Series.setCategoryData() method:
Please try the following codes with
GroupingLabels.zip (12.4 KB)
public static void main(String[] args) throws Throwable {
Workbook book = new Workbook(dir +"GroupingLabels.xlsx");
ChartCollection charts = book.getWorksheets().get(1).getCharts();
int index = charts.add(ChartType.COLUMN, 2,5,19,12);
Chart chart = charts.get(0);
chart.getNSeries().add("=D6:D23",true);
chart.getNSeries().get(0).setName("=D5");
chart.getNSeries().setCategoryData("=A6:D23");
book.save(dir +"dest.xlsx");
}