We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Draw stylish Charts in Excel

Currently Aspose provides of a bunch of different chartTypes, I have tried using most of them to draw my pie chart and a bar graph. However not getting a feel.

I would like to know if its possible to draw a custom image chart with custom colors. Or is there any other option where in I can use some stylish Charts.

Hi,

If you want to create native Excel chart with customized colors, pattern fills or gradient fills, you may do it using Aspose.Cells for Java API, see the topics in the section/sub-sections for your reference:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/creating-charts.html

I will also write an example here for your reference, in the example, I have set some gradient fills/styles for different slices of a PIE Chart.

Sample code:
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 = 300;
int chartHeight = 300;

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;


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);

String chartTitle = dataSheet.getCells().getCell(“A2”).getValue().toString();
chart.getTitle().setText(chartTitle);

for (int j = 0; j < chart.getNSeries().size(); j++) {
ASeries series = chart.getNSeries().get(j);
ChartPoints chartPoints = series.getChartPoints();
for (int k = 0; k < chartPoints.size(); k++) {
ChartPoint chartPoint = chartPoints.getChartPoint(k);
GradientFill gf = new GradientFill();
gf.setGradientColorType(GradientColorType.TWO_COLORS);
gf.setColor1(wb.getPalette().getColor(k));
gf.setColor2(wb.getPalette().getColor(k +1 * 2));
gf.setDirection(GradientDirectionType.FROM_CENTER);

chartPoint.getArea().setFill(gf);
}
}


chart.setLegendShown(false);

wb.save(new FileOutputStream(“D:\files\pie_tst_chart.xls”));

And, if you need to take the image of the chart, see the document:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/converting-chart-to-image.html

And, if you need to create non-Excel charts yet sophisticated stylish charts and you need to take the image for the chart, you may use Aspose.Report for Java product, see some demos e.g:
http://www.aspose.com/demos/java-components/aspose.report/chart/java/pie-chart.jsf
Here is an article:
http://www.aspose.com/documentation/java-components/aspose.report-for-java/how-to-create-a-pie-chart.html
If you need further help or info regarding Aspose.Report for Java, please do post a query in their forum, they will help you soon.

Thank you.