Sample to draw 100% stacked bar chart using cells java

Hi Team,

Could you please provide a sample code to draw a 100% stacked bar chart using aspose cells java ?

Thanks
Thilak Babu

@Thilakbabu,

See the following sample code for your needs for your reference:
e.g.
Sample code:

        // 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(ChartType.BAR_100_PERCENT_STACKED, 0, 0, 20, 10));
        SeriesCollection nSeries = chart.getNSeries();

        // write data
        dataSheetCells.get(0,1).setValue("Incorrect %");
        dataSheetCells.get(0,2).setValue("Correct %");
        dataSheetCells.get(1,0).setValue("Question 1");
        dataSheetCells.get(1,1).setValue(0.12);
        dataSheetCells.get(1,2).setValue(0.88);
        dataSheetCells.get(2,0).setValue("Question 2");
        dataSheetCells.get(2,1).setValue(0.21);
        dataSheetCells.get(2,2).setValue(0.79);
        dataSheetCells.createRange(1,0,2,1).setName("Categories");
        dataSheetCells.createRange(1,1,2,1).setName("IncorrectPct");
        dataSheetCells.createRange(1,2,2,1).setName("CorrectPct");
        Series incorrectSeries = nSeries.get(nSeries.add("IncorrectPct", true));
        incorrectSeries.setName("Incorrect %");
        Series correctSeries = nSeries.get(nSeries.add("CorrectPct", true));
        correctSeries.setName("Correct %");
        nSeries.setCategoryData("Categories");

        // format labels
        DataLabels incorrectLabels = incorrectSeries.getDataLabels();
        DataLabels correctLabels = correctSeries.getDataLabels();

        incorrectLabels.setShowPercentage(true);
        correctLabels.setShowPercentage(true);

        wb.save("f:\\files\\out1.xlsx");

Hope, this helps a bit.

Hi @Amjad_Sahi,

Thanks for your reply.

The data you populated to draw the stacked bar looks like below

Incorrect % Correct %
Question 1 0.12 0.88
Question 2 0.21 0.79

Is it possible to draw the same chart with the data populated like below ?

category %
Question1 Incorrect % 0.12
Question1 Correct % 0.88
Question2 Incorrect % 0.21
Question2 Correct % 0.79

Please help me on this. Thanks in advance.

Thanks,
Thilak Babu

@Thilakbabu,

Please create your chart in MS Excel manually based on your desired dataset/data, save the file and provide us. We will check and help you on how to create the chart via Aspose.Cells API.

PS. please zip the Excel file prior attaching here.