General

Components

Community

Development

TDF

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


Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.