General

Components

Community

Development

PPMC

ASF

Documents > Cookbook >Charts



Overview
Since 0.6, Simple ODF provides methods to manipulate charts in text document, spreadsheet document and presentation document. You can create, update and delete charts with these methods.

Create charts
We all know, a chart is associated with a table. In order to create a chart, you must determine the data set of this chart. The data set can be a cell range of a table, for example:

        CellRangeAddressList cellRange = CellRangeAddressList.valueOf("A.A1:A.B3");
DataSet dataSet = new DataSet(cellRange, spreadsheetDoc, true, true, false);

Or a two dimensional array, for example:

        int row = 2, column = 3;
double[][] data = new double[column][row];
String[] labels = new String[row];
String[] legends = new String[column];
DataSet dataset = new DataSet(labels, legends, data);

You should also use rectangle to define the position and the size of this chart. For example:

        Rectangle rect = new Rectangle();
rect.x = 2000;
rect.y = 2700;
rect.width = 15000;
rect.height = 8000;
rect.y = 110000;

Then you can create a chart:

        spreadsheetDoc.createChart("Page Visit", dataSet,rect);

There are some shortcut methods to create charts, for example, below codes show how to create a chart in a text document:

        Chart chart = textDoc.createChart(
"Page Visit", spreadsheetDoc,
cellRange, true, true, false, rect);

If you want to create a chart in a spreadsheet document, you need to specify a cell to be the anchor of this chart, for example:

        spreadsheetDoc.createChart("Page Visit", spreadsheetDoc, cellRange,
true, true, false, rect, spreadsheetDoc.getTableByName("C")
.getCellByPosition("D10"));

If you want to create a chart in a presentation document, you can use the existing layout of a slide, which means, you don't need to specify a rectangle. The layouts that could contain a chart include: TITLE_PLUS_CHART, TITLE_PLUS_2_CHART, TITLE_LEFT_CHART_RIGHT_OUTLINE, TITLE_PLUS_3_OBJECT, and TITLE_PLUS_4_OBJECT. For example:

        Slide slide = presentationDoc.newSlide(2, "Slide3",
SlideLayout.TITLE_PLUS_2_CHART);
chart = slide.createChart("Count of Visits", spreadsheetDoc,
cellRange, true, true, false, null);


Update charts
You can update charts properties, for example, the title, axis title, chart type, whether to apply 3D effect, whether to use legend with API. For example:

        chart.setChartTitle("New title");
chart.setAxisTitle("Hour", "Number");
chart.setChartType(ChartType.AREA);
chart.setApply3DEffect(true);
chart.setUseLegend(true);

You can update the data set too. For example:

        chart.setChartData(new DataSet(CellRangeAddressList.valueOf("A.A1:A.C4"), spreadsheetDoc, true, true, true));


Get and delete charts
You can get charts by title e.g.

        chart = textDoc.getChartByTitle("New title").get(0);

You can also get a chart by its unique ID. The unique ID of a chart in Simple ODF API is the path of the chart document (relative to the ODF document package). The unique ID can be gotten with method:

        String chartid = chart.getChartID();
chart = textDoc.getChartById(chartid);

You can also get the count of charts in this document.

        int count = textDoc.getChartCount();

You can delete a chart by ID or by title, e.g.

        textDoc.deleteChartById(chartid);
textDoc.deleteChartByTitle("New title");



Powered by the Apache CMS.

Apache "ODF Toolkit" is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2011 The Apache Software Foundation Licensed under the Apache License, Version 2.0. Contact Us
Apache and the Apache feather logos are trademarks of The Apache Software Foundation.
Other names appearing on the site may be trademarks of their respective owners.