Could you please help us code snippet to provide a combination chart containing Stacked horizontal bar in primary axis with clustered bar in secondary axis but as a hidden bar only the datalabel has to be enabled.
I am attaching the desired excel file for which we need code snippet using Aspose cells java version 25.6.
To create a combination chart in Aspose.Cells for Java that includes a stacked horizontal bar on the primary axis and a hidden clustered bar on the secondary axis (with only data labels visible), you can use the following code snippet. This will ensure that the format, height, width, and other details match your requirements.
import com.aspose.cells.*;
public class CombinationChartExample {
public static void main(String[] args) throws Exception {
// Create a workbook and get the first worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
// Sample data for the chart
worksheet.getCells().get("A1").putValue("Category");
worksheet.getCells().get("A2").putValue("A");
worksheet.getCells().get("A3").putValue("B");
worksheet.getCells().get("A4").putValue("C");
worksheet.getCells().get("B1").putValue("Stacked Data");
worksheet.getCells().get("B2").putValue(10);
worksheet.getCells().get("B3").putValue(20);
worksheet.getCells().get("B4").putValue(30);
worksheet.getCells().get("C1").putValue("Clustered Data");
worksheet.getCells().get("C2").putValue(5);
worksheet.getCells().get("C3").putValue(15);
worksheet.getCells().get("C4").putValue(25);
// Add a chart to the worksheet
int chartIndex = worksheet.getCharts().add(ChartType.COMBO, 5, 0, 15, 5);
Chart chart = worksheet.getCharts().get(chartIndex);
// Set the data range for the chart
chart.setNSeries("B2:B4", true);
chart.getNSeries().get(0).setType(ChartType.STACKED_BAR);
chart.setNSeries("C2:C4", true);
chart.getNSeries().get(1).setType(ChartType.BAR);
chart.getNSeries().get(1).setIsValueShownAsLabel(true); // Show data labels
chart.getNSeries().get(1).setPlotOnSecondaryAxis(true); // Plot on secondary axis
chart.getNSeries().get(1).setVisible(false); // Hide the bar
// Set chart title
chart.setTitle("Combination Chart Example");
// Save the workbook
workbook.save("CombinationChartExample.xlsx");
}
}
This code creates a combination chart with the specified characteristics. Ensure to adjust the data and formatting as needed to match your requirements. If you have specific formatting needs, you can further customize the chart properties accordingly.
Thank you for sharing the sample Excel file with the chart.
Please refer to the sample code below, which is written to assist you in achieving your requirements. Using the source data from your template Excel file, I have created a similar (cusom) combo chart in the first worksheet. You may modify or update the code snippet as needed to better suit your exact needs.
e.g., Sample code:
Workbook workbook = new Workbook("d:\\files\\StackedHorizontalAndClusteredBar.xlsx");
Worksheet worksheet = workbook.getWorksheets().get("1-Report");
// Add a chart to the worksheet
int chartIndex = worksheet.getCharts().add(ChartType.BAR, 5, 0, 25, 15);
Chart chart = worksheet.getCharts().get(chartIndex);
chart.setChartDataRange("='1-DataSheet'!B6:C7",false);
SeriesCollection nSeries = chart.getNSeries();
Series onSeries = nSeries.get(0);
onSeries.setName("On");
onSeries.setType(ChartType.BAR);
Series othersSeries = nSeries.get(1);
othersSeries.setName("Others");
othersSeries.setType(ChartType.BAR);
// Add a third data series
int i = nSeries.add("='1-DataSheet'!D5:D6", true);
Series netSeries = nSeries.get(2);
netSeries.setName("Net");
netSeries.setType(ChartType.BAR_STACKED);
netSeries.setPlotOnSecondAxis(true);
chart.getSecondValueAxis().setVisible(false);
//Set overlay and gap width of the first two series
Integer value = 25;
short v = value.shortValue();
onSeries.setGapWidth(v);
Integer value1 = 100;
short v1 = value1.shortValue();
onSeries.setOverlap(v1);
othersSeries.setGapWidth(v);
othersSeries.setOverlap(v1);
onSeries.getDataLabels().setShowValue(true);
othersSeries.getDataLabels().setShowValue(true);
netSeries.getArea().setFormatting(FormattingType.NONE);
netSeries.getArea().setTransparency(1.0);
nSeries.setCategoryData("'1-DataSheet'!$A$6");
// Set chart title
chart.getTitle().setText("Horizontal With labels");
chart.getCategoryAxis().getTitle().setText("Total");
chart.setShowLegend(true);
chart.getLegend().setPosition(LegendPositionType.BOTTOM);
// Save the workbook
workbook.save("d:\\files\\outchart1.xlsx");
There were few things which did not work as per the desired output. I have updated them now.
The updated code snippet is as below
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook("C:\\Desktop\\StackedHorizontalAndClusteredBar.xlsx");
int index = workbook.getWorksheets().add();
// Add a chart to the worksheet
int chartIndex = workbook.getWorksheets().get(index).getCharts().add(com.aspose.cells.ChartType.BAR, 5, 0, 25, 15);
Worksheet worksheet = workbook.getWorksheets().get(index);
Chart chart = worksheet.getCharts().get(chartIndex);
chart.getPlotArea().getArea().getFillFormat().setFillType(FillType.NONE);
chart.setChartDataRange("='1-DataSheet'!B6:C7",false);
SeriesCollection nSeries = chart.getNSeries();
Series onSeries = nSeries.get(0);
onSeries.setName("On");
onSeries.setType(com.aspose.cells.ChartType.BAR_STACKED);
Series othersSeries = nSeries.get(1);
othersSeries.setName("Others");
othersSeries.setType(com.aspose.cells.ChartType.BAR_STACKED);
// Add a third data series
int i = nSeries.add("='1-DataSheet'!D6", true);
Series netSeries = nSeries.get(2);
netSeries.setName("Net");
netSeries.setType(com.aspose.cells.ChartType.BAR);
netSeries.setPlotOnSecondAxis(true);
onSeries.setType(com.aspose.cells.ChartType.BAR_STACKED);
othersSeries.setType(com.aspose.cells.ChartType.BAR_STACKED);
chart.getSecondValueAxis().setVisible(false);
//Set overlay and gap width of the first two series
Integer value = 25;
short v = value.shortValue();
onSeries.setGapWidth(v);
Integer value1 = 100;
short v1 = value1.shortValue();
onSeries.setOverlap(v1);
othersSeries.setGapWidth(v);
othersSeries.setOverlap(v1);
//Set overlay and gap width of the third series
Integer value3 = 150;
short v3 = value3.shortValue();
netSeries.setGapWidth(v3);
Integer value4 = 0;
short v4 = value4.shortValue();
netSeries.setOverlap(v4);
onSeries.getDataLabels().setShowValue(true);
othersSeries.getDataLabels().setShowValue(true);
netSeries.getDataLabels().setShowValue(true);
netSeries.getArea().setFormatting(FormattingType.NONE);
netSeries.getArea().setTransparency(1.0);
nSeries.setCategoryData("'1-DataSheet'!$A$6");
// Set chart title
chart.getTitle().setText("Horizontal With labels");
chart.getCategoryAxis().getTitle().setText("Total");
chart.setShowLegend(true);
chart.getLegend().setPosition(LegendPositionType.BOTTOM);
// Save the workbook
workbook.save("C:Desktop\\outchart5.xlsx");
}
With this updated code, I am not able to see couple of things.
The invisible clustered bar is started from the right side in the desired chart as shown below
@Thilakbabu
Thanks for your feedback and details. We will investigate your issue further. Hopefully we can figure it out soon. Once we have an update on it, we will let you know.
Thank you for providing the details and screenshots.
I have attempted several adjustments and variations in the code to replicate the chart you provided in your Excel file. While I was able to make progress, I couldn’t achieve an exact match for the desired combo chart. Additionally, aligning the x-axis value bounds and intervals as specified impacts other chart elements and attributes.
To ensure we fully understand your requirements and deliver the chart as requested, we will conduct a thorough evaluation of the scenario/case. We have created the following new ticket(s) in our internal issue tracking system and will either provide you with the updated code or implement the fix in accordance with the terms outlined in Free Support Policies.
Issue ID(s): CELLSJAVA-46498
Once we have an update on it, we will let you know here.
@Thilakbabu
We apologize for not being able to analyze your issue in detail earlier due to other pressing priorities or tasks.
However, we have set the status of “CELLSJAVA-46498” to “In Progress” and are giving it priority, we expect get back to you within 1 day. We apologize again.
Hi @Thilakbabu
I made some modifications to the code we provided you before, which resolved the issue 1 you encountered, and this code does not have the issue 2. This is the file I generated. outchart1_250918.zip (12.9 KB)
This is my code, it will read the original file you provided, and create a new chart, the chart style is basically the same as the test chart in your file.
String path = "";
Workbook workbook = new Workbook(path + "StackedHorizontalAndClusteredBar.xlsx");
Worksheet worksheet = workbook.getWorksheets().get("1-Report");
// Add a chart to the worksheet
int chartIndex = worksheet.getCharts().add(ChartType.BAR, 1, 0, 20, 15);
Chart chart = worksheet.getCharts().get(chartIndex);
chart.setChartDataRange("='1-DataSheet'!B6:C7",false);
SeriesCollection nSeries = chart.getNSeries();
Series onSeries = nSeries.get(0);
onSeries.setName("On");
onSeries.setType(ChartType.BAR_STACKED);
Series othersSeries = nSeries.get(1);
othersSeries.setName("Others");
othersSeries.setType(ChartType.BAR_STACKED);
// Add a third data series
int i = nSeries.add("='1-DataSheet'!D6", true);
Series netSeries = nSeries.get(2);
netSeries.setName("Net");
netSeries.setType(ChartType.BAR);
netSeries.setPlotOnSecondAxis(true);
chart.getSecondValueAxis().setVisible(true);
chart.getSecondValueAxis().setTickLabelPosition(TickLabelPositionType.NONE);
//Set overlay and gap width of the first two series
Integer value = 25;
short v = value.shortValue();
onSeries.setGapWidth(v);
Integer value1 = 100;
short v1 = value1.shortValue();
onSeries.setOverlap(v1);
othersSeries.setGapWidth(v);
othersSeries.setOverlap(v1);
onSeries.getDataLabels().setShowValue(true);
onSeries.getDataLabels().setPosition(LabelPositionType.CENTER);
netSeries.getDataLabels().setShowValue(true);
netSeries.getDataLabels().setPosition(LabelPositionType.INSIDE_BASE);
netSeries.getArea().setFormatting(FormattingType.NONE);
netSeries.getArea().setTransparency(1.0);
nSeries.setCategoryData("'1-DataSheet'!$A$6");
// Set chart title
chart.getTitle().setText("Horizontal With labels");
//chart.getCategoryAxis().getTitle().setText("Total");
chart.setShowLegend(true);
chart.getLegend().setPosition(LegendPositionType.BOTTOM);
// Save the workbook
workbook.save(path + "outchart1.xlsx");
We apologize for the delay. We have not completed it and are still working on accurately generating your requested combo chart. We aim to resolve the discrepancies you highlighted and provide the exact chart in the next few days.
Once we have finalized the solution, we may share the devised code snippet with you.