Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Create Pie Chart
The following example shows you how add pie chart to a worksheet.
import com.independentsoft.office.Unit; import com.independentsoft.office.UnitType; import com.independentsoft.office.charts.CategoryAxis; import com.independentsoft.office.charts.ChartSpace; import com.independentsoft.office.charts.Layout; import com.independentsoft.office.charts.Legend; import com.independentsoft.office.charts.LegendPosition; import com.independentsoft.office.charts.NumberCache; import com.independentsoft.office.charts.NumberReference; import com.independentsoft.office.charts.NumericPoint; import com.independentsoft.office.charts.PieChart; import com.independentsoft.office.charts.PieChartSerie; import com.independentsoft.office.charts.PlotArea; import com.independentsoft.office.charts.SeriesText; import com.independentsoft.office.charts.StringCache; import com.independentsoft.office.charts.StringPoint; import com.independentsoft.office.charts.StringReference; import com.independentsoft.office.charts.Values; import com.independentsoft.office.drawing.Extents; import com.independentsoft.office.drawing.Offset; import com.independentsoft.office.spreadsheet.Cell; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.drawing.ClientData; import com.independentsoft.office.spreadsheet.drawing.DrawingObjects; import com.independentsoft.office.spreadsheet.drawing.EndAnchorPoint; import com.independentsoft.office.spreadsheet.drawing.GraphicFrame; import com.independentsoft.office.spreadsheet.drawing.StartAnchorPoint; import com.independentsoft.office.spreadsheet.drawing.Transform2D; import com.independentsoft.office.spreadsheet.drawing.TwoCellAnchor; public class Example { public static void main(String[] args) { try { Workbook book = new Workbook(); Worksheet sheet1 = new Worksheet(); sheet1.set("A2", new Cell("1st Qtr")); sheet1.set("A3", new Cell("2nd Qtr")); sheet1.set("A4", new Cell("3rd Qtr")); sheet1.set("A5", new Cell("4th Qtr")); sheet1.set("B1", new Cell("Sales")); sheet1.set("B2", new Cell(365.68)); sheet1.set("B3", new Cell(259.98)); sheet1.set("B4", new Cell(199.80)); sheet1.set("B5", new Cell(411.90)); PieChart pieChart = new PieChart(); pieChart.setVaryColors(true); PieChartSerie serie1 = new PieChartSerie(); serie1.setIndex(0); serie1.setOrder(0); serie1.setSeriesText(new SeriesText()); serie1.getSeriesText().setStringReference(new StringReference()); serie1.getSeriesText().getStringReference().setFormula("Sheet1!$B$1"); serie1.getSeriesText().getStringReference().setStringCache(new StringCache()); StringPoint seriesTextPoint1 = new StringPoint(0, "Sales"); serie1.getSeriesText().getStringReference().getStringCache().getStringPoints().add(seriesTextPoint1); serie1.setCategoryAxis(new CategoryAxis()); serie1.getCategoryAxis().setStringReference(new StringReference()); serie1.getCategoryAxis().getStringReference().setFormula("Sheet1!$A$2:$A$5"); serie1.getCategoryAxis().getStringReference().setStringCache(new StringCache()); StringPoint categoryAxisPoint1 = new StringPoint(0, "1st Qtr"); StringPoint categoryAxisPoint2 = new StringPoint(1, "2nd Qtr"); StringPoint categoryAxisPoint3 = new StringPoint(2, "3rd Qtr"); StringPoint categoryAxisPoint4 = new StringPoint(3, "4th Qtr"); serie1.getCategoryAxis().getStringReference().getStringCache().getStringPoints().add(categoryAxisPoint1); serie1.getCategoryAxis().getStringReference().getStringCache().getStringPoints().add(categoryAxisPoint2); serie1.getCategoryAxis().getStringReference().getStringCache().getStringPoints().add(categoryAxisPoint3); serie1.getCategoryAxis().getStringReference().getStringCache().getStringPoints().add(categoryAxisPoint4); serie1.setValues(new Values()); serie1.getValues().setNumberReference(new NumberReference()); serie1.getValues().getNumberReference().setFormula("Sheet1!$B$2:$B$5"); serie1.getValues().getNumberReference().setNumberCache(new NumberCache()); serie1.getValues().getNumberReference().getNumberCache().setFormat("General"); NumericPoint valuesPoint1 = new NumericPoint(0, "365.68"); NumericPoint valuesPoint2 = new NumericPoint(1, "259.98"); NumericPoint valuesPoint3 = new NumericPoint(2, "199.80"); NumericPoint valuesPoint4 = new NumericPoint(3, "411.90"); serie1.getValues().getNumberReference().getNumberCache().getNumericPoints().add(valuesPoint1); serie1.getValues().getNumberReference().getNumberCache().getNumericPoints().add(valuesPoint2); serie1.getValues().getNumberReference().getNumberCache().getNumericPoints().add(valuesPoint3); serie1.getValues().getNumberReference().getNumberCache().getNumericPoints().add(valuesPoint4); pieChart.getSeries().add(serie1); ChartSpace chartSpace = new ChartSpace(); chartSpace.setPlotArea(new PlotArea()); chartSpace.getPlotArea().setLayout(new Layout()); chartSpace.getPlotArea().getCharts().add(pieChart); Legend legend = new Legend(); legend.setPosition(LegendPosition.RIGHT); legend.setLayout(new Layout()); chartSpace.setLegend(legend); chartSpace.setPlotVisibleOnly(true); TwoCellAnchor anchor = new TwoCellAnchor(); StartAnchorPoint start = new StartAnchorPoint(); start.setColumn(5); start.setColumnOffset(new Unit(1, UnitType.PIXEL)); start.setRow(5); start.setRowOffset(new Unit(1, UnitType.PIXEL)); EndAnchorPoint end = new EndAnchorPoint(); end.setColumn(12); end.setColumnOffset(new Unit(33, UnitType.PIXEL)); end.setRow(19); end.setRowOffset(new Unit(9, UnitType.PIXEL)); anchor.setStart(start); anchor.setEnd(end); GraphicFrame graphicFrame = new GraphicFrame(); graphicFrame.setID("1"); graphicFrame.setName("Chart 1"); graphicFrame.setGraphicObject(chartSpace); graphicFrame.setTransform2D(new Transform2D()); graphicFrame.getTransform2D().setExtents(new Extents(0, 0)); graphicFrame.getTransform2D().setOffset(new Offset(0, 0)); anchor.setElement(graphicFrame); anchor.setClientData(new ClientData()); DrawingObjects drawingObjects = new DrawingObjects(); drawingObjects.getAnchors().add(anchor); sheet1.setDrawingObjects(drawingObjects); book.getSheets().add(sheet1); book.save("c:\\test\\output.xlsx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*