Hi,
Please run the following sample and see that data labels are not reflected in image when
1. DataLabel is not present in image when
series.getDataLabels().setPercentageShown(true);
series.getDataLabels().setValueShown(false);
2. Percentage DataLabel is not present in image when
series.getDataLabels().setPercentageShown(true);
series.getDataLabels().setValueShown(true);
Only, value is shown.
Thanks
Muhammed
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import com.aspose.cells.ASeries;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartMarkerType;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.LabelPositionType;
import com.aspose.cells.Legend;
import com.aspose.cells.LegendPositionType;
import com.aspose.cells.TickLabelPositionType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.OleObjectFrame;
import com.aspose.slides.Presentation;
import com.aspose.slides.Slide;
public class PieChartPctDataLabel {
public static void main(String args[]) {
try {
String[] cellsName = new String[] { "B2","C2","D2","E2","F2","G2","H2","I2","J2",
"B3","C3","D3","E3","F3","G3","H3","I3","J3",
"B4","C4","D4","E4","F4","G4","H4","I4","J4",
};
//Array of cell data
double[] cellsValue = new double[] {7.027279E7,1744253.0,1473.0,395085.7,7.019342E7,17.0,1111239.0,0.0,5.218978E7,
9.530519E7,2848223.0,6885.9,395216.4,9.729046E7,18.5,900626.8,0.0,4.484255E7,
9.898876E7,1076122.0,1393.5,790165.4,8.850956E7,12.8,1151062.0,0.0,7.406841E7};
Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().getSheet(0);
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);
dataSheet.getCells().getCell("A2").setValue("Unit Sales");
dataSheet.getCells().getCell("A3").setValue("Value Sales");
dataSheet.getCells().getCell("A4").setValue("Volume Sales");
dataSheet.getCells().getCell("B1").setValue("BrandA Rinse Conditioners");
dataSheet.getCells().getCell("C1").setValue("BrandB Ltd Rinse Conditioners");
dataSheet.getCells().getCell("D1").setValue("BrandC Rinse Conditioners");
dataSheet.getCells().getCell("E1").setValue("BrandD Rinse Conditioners");
dataSheet.getCells().getCell("F1").setValue("BrandE Rinse Conditioners");
dataSheet.getCells().getCell("G1").setValue("BrandF Rinse Conditioners");
dataSheet.getCells().getCell("H1").setValue("BrandG Rinse Conditioners");
dataSheet.getCells().getCell("I1").setValue("BrandH Rinse Conditioners");
dataSheet.getCells().getCell("J1").setValue("BrandJ Rinse Conditioners");
//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
double cellValue = cellsValue[i];
dataSheet.getCells().getCell(cellName).setValue(cellValue);
}
//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();
//Add a chart in ChartSheet with data series from DataSheet
int chartWidth = 500;
int chartHeight = 500;
int chartHeightInRows = 0;
int chartWidthInCols = 0;
int cellWidth = dataSheet.getCells().getColumnWidthPixel(0);
int cellHeight = dataSheet.getCells().getRowHeightPixel(0);
chartWidthInCols = (int)(chartWidth / cellWidth);
chartHeightInRows = (int)(chartHeight / cellHeight);
chartWidth = chartWidthInCols * cellWidth;
chartHeight = chartHeightInRows * cellHeight;
double colsPerChart = (double)chartWidthInCols / 4;
int chartStartRow = 0;
int chartStartColumn = 0;
int chartLeft = 0;
int chartTop = 0;
Chart chart = chartSheet.getCharts().addChart(ChartType.PIE, chartStartRow, chartStartColumn, chartLeft, chartTop, chartWidth, chartHeight);
chart.getNSeries().add(dataSheetName + "!B2:J2", false);
//String categoryAreaRef = dataSheet.getName() + "!A2:A4";
String categoryAreaRef = dataSheet.getName() + "!B1:J1";
chart.getNSeries().setCategoryData(categoryAreaRef);
ASeries series = chart.getNSeries().get(0);
//series.getDataLabels().setValueShown(true);
series.getDataLabels().setPercentageShown(true);
series.getDataLabels().setValueShown(false);
series.getDataLabels().setNumberFormat("0.00%");
series.getDataLabels().setLabelPosition(LabelPositionType.CENTER);
//Get Chart as image.
ImageOptions imgOpts = new ImageOptions();
imgOpts.setImageFormat(ImageFormat.PNG);
imgOpts.setFashion(FileFormatType.EXCEL2003);
chart.toImage(new FileOutputStream("D:\\Temp\\chart.png"), imgOpts);
wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartHeightInRows - 1, 0, chartWidthInCols - 1);
//Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
wb.save(new FileOutputStream("D:\\Temp\\output.xls"));
System.out.println("successfully completed!!!" + CellsHelper.getReleaseVersion());
}
catch (Exception e) {
e.printStackTrace();
}
}
}