Hi,
For your issues:
a. The color of the bars was not right. I tried to adjust RGB value but it didn't work, it always came back as black.
Well, since MS Excel standard color palette has only 56 colors by default and the color your are trying to set is not present on the palette, so you need to update and add your desired color to the palette first at some indexed position, see your following piece of code with an addition of a line for the method labeled "AddExcelChart()". I have tested and it works fine.
private static int AddExcelChart(Workbook wb, int p_chartType, String p_dataSheet,int p_dataRows, int p_dataColumns,
int chartRows, int chartCols) throws Exception {
//Add a chart sheet
System.out.println("AddExcelChart -start");
Worksheet chartSheet = wb.getWorksheets().addSheet(SheetType.CHART);
//Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("Chartsheet");
int chartSheetIdx = chartSheet.getIndex();
String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" };
//Add a chart in ChartSheet with data series from DataSheet
Charts charts = chartSheet.getCharts();
Chart chart = charts.addChart(ChartType.BAR_STACKED, 0, chartRows, 0, chartCols);
chart.getChartArea().getBorder().setVisible(false);
chart.getPlotArea().getBorder().setVisible(false);
chart.getChartArea().getArea().setForegroundColor(Color.WHITE);
chart.getPlotArea().getArea().setForegroundColor(Color.WHITE);
chart.getPlotArea().setWidth(1400);
//chart area
chart.getChartArea().getArea().setForegroundColor(Color.WHITE);
chart.getChartArea().getBorder().setVisible(false);
chart.getChartArea().setAutoScaleFont(false);
//chart.getChartShape().setWidth(1320);
//chart.getChartShape().setHeight(1300);
chart.getPlotArea().getArea().setForegroundColor(Color.WHITE);
chart.getPlotArea().getBorder().setVisible(false);
//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"
NSeries serieses = chart.getNSeries();
Color color = new Color();
color.setRed(0);
color.setGreen(104);
color.setBlue(208);
//Add the color to the MS Excel standard color palette first.
wb.getPalette().setColor(55, color);
for (int i = 0; i < p_dataColumns; i++) {
System.out.println("AddExcelChart - RANGE=" + p_dataSheet + "!$" + alphabet[i] + "$" + 2 + ":$" + alphabet[i+1] + "$" + (p_dataRows+1));
serieses.add(p_dataSheet + "!$" + alphabet[i] + "$" + 2 + ":$" + alphabet[i+1] + "$" + (p_dataRows+1) ,true);
// serieses.get(i).setName("Documents");
serieses.get(i).getDataLabels().setValueShown(true);
serieses.get(i).getDataLabels().setLabelPosition(LabelPositionType.INSIDE_BASE);
serieses.get(i).getDataLabels().setSeriesShown(true);
serieses.get(i).getArea().setForegroundColor(color);
serieses.get(i).getBorder().setColor(color);
}
..................
}
For further reference, see the document: http://www.aspose.com/documentation/java-components/aspose.cells-for-java/colors-and-palette.html
b. The values on the bars always get displayed in the middle of the bar. I need it at the end of the bar (just outside)..
Well for this type of bar chart, there are only three data label positions: Center, Inside End and Inside Base (
You may confirm it in MS Excel manually by opening the file into MS Excel and check the Format Label Position in Alignment tab). So you can choose any one for your requirements.
Thank you.
Amjad Sahi
Support Developer,
Aspose Sialkot Team
Contact Us