Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright 2011 IBM. All rights reserved. Use is subject to license terms. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at http://odftoolkit.org/docs/license.txt Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.odftoolkit.simple.chart;
 
 import java.util.Map;
 
DataSet is wrapper class for chart data. Generally it is a 2 dimensional data container, a set of chart data series. Each data series has a key, i.e. chart label. The data may be a 2 dimensional double array, or a cell range address of a sheet in Spreadsheet document. DataSet provides some convenient methods for users to manipulate specific data series in the chart data.

Since:
0.6
 
 
 public class DataSet {
 
 	private Vector<Vector<Double>> dataset// The second vector is a data
 											// series.
 	private Vector<Stringlabelset// label vector,
 	private Vector<Stringlegendset// legend vector
 	// Vector<OdfCellRangeAddressList> seriesCellRange; //cellrange of data
 	// series
 	// Vector<String> legendCellRange;
 	// String cellRangeAddress;
 	// Chart chart;
 	private boolean isLocalTable;

Creates a new DataSet without argument.
 
 	public DataSet() {
 		 = new Vector<Vector<Double>>();
 		 = new Vector<String>();
 		 = new Vector<String>();
 		 = true;
 		 = false;
 		 = false;
 	}

Creates a new DataSet.

Parameters:
labels the label strings of this DataSet.
legends the legend strings of this DataSet.
data the data of this DataSet, which stores in 2 dimensional double array.
 
 	public DataSet(String[] labelsString[] legendsdouble[][] data) {
 		 = new Vector<Vector<Double>>();
 		 = new Vector<String>();
 		 = new Vector<String>();
 		 = true;
 		 = false;
 		 = true;
 		setValues(labelslegendsdata);
 
 	}

Creates a new DataSet.

Parameters:
cellRangeAddress the cell range address, which is used as the data source.
spreadsheet the data source SpreadsheetDocument.
bFirstRowAsLabel whether this data set uses first row as chart label.
bFirstColumnAsLabel whether this data set uses first column as chart label.
rowAsDataSeries whether this data set uses row as data series.
	public DataSet(CellRangeAddressList cellRangeAddressSpreadsheetDocument spreadsheetboolean bFirstRowAsLabel,
			boolean bFirstColumnAsLabelboolean rowAsDataSeries) {
		 = new Vector<String>();
		 = new Vector<String>();
		this. = bFirstRowAsLabel;
		this. = bFirstColumnAsLabel;
		 = rowAsDataSeries;
		 = false;
		setValues(cellRangeAddressspreadsheetbFirstRowAsLabelbFirstColumnAsLabelrowAsDataSeries);
	}
	// return legend cell ranges
Gets the local table cell range.

Returns:
the local table cell range,
	public String getLocalTableCellRanges(int seriesCountint labelLengthVector<StringseriesCellRange,
			Vector<StringlegendCellAddr) {
		String localtable = "local-table";
		String tablecellrange = localtable + "." + "A1:" + (char) ('A' + seriesCount) + (1 + labelLength);
		return getCellRanges(tablecellrangetruetruefalseseriesCellRangelegendCellAddr);
	}

Gets the cell range address list.

Returns:
the cell range address list.
	}

Adds a data series at the end of current data set.

Parameters:
legend legend for the data series.
values data series values corresponding to the legend.
	public void appendDataSeries(String legenddouble[] values) {
		.add(legend);
		Vector<DoublevaluesList = new Vector<Double>();
		for (int i = 0; i < values.lengthi++) {
			valuesList.add(new Double(values[i]));
		}
		.add(valuesList);
	}

Adds a data series at the index in current data set.

Parameters:
index the index the added data series will be located.
legend legend for the data series.
values data series values corresponding to the legend.
	public void insertDataSeries(int indexString legenddouble[] values) {
		.add(indexlegend);
		Vector<DoublevaluesList = new Vector<Double>();
		for (int i = 0; i < values.lengthi++) {
			valuesList.add(new Double(values[i]));
		}
		.add(indexvaluesList);
	}

Updates the data series values according to the index of the data series.

Parameters:
index the index of the data series in the data set.
values data series values.
	public void updateDataSeries(int indexdouble[] values) {
		Vector<DoublevaluesList = new Vector<Double>();
		for (int i = 0; i < values.lengthi++) {
			valuesList.add(new Double(values[i]));
		}
		.set(indexvaluesList);
	}

Updates the data series values according to the legend of the data series.

Parameters:
legend the chart legend of the data series.
values data series values.
	public void updateDataSeries(String legendint beginindexdouble[] values) {
		Vector<DoublevaluesList = new Vector<Double>();
		for (int i = 0; i < values.lengthi++) {
			valuesList.add(new Double(values[i]));
		}
		.set(getIndexOfDataSeries(legendbeginindex), valuesList);
	}

Removes a data series by the index of the data series in the data set.

Parameters:
index index of the data series in the data set.
	public void removeDataSeries(int index) {
	}

Remove a data series by the legend of the data series.

Parameters:
legend the legend of the data series.
	public void removeDataSeries(String legendint beginindex) {
		int index = getIndexOfDataSeries(legendbeginindex);
	}

Gets the index of the data series by its legend.

Parameters:
legend the legend of the data series.
Returns:
index of the data series in the data set.
	public int getIndexOfDataSeries(String legendint beginindex) {
		return .indexOf(legendbeginindex);
	}

Gets the legend of the data series by its index in the data set.

Parameters:
index index of the data series in the data set.
Returns:
the legend of the data series.
	public String getLegendByIndex(int index) {
		return .get(index);
	}

Sets or change the legend of the data series by its index in the data set.

Parameters:
index index of the data series in the data set.
legend the legend of the data series.
	public void setLegendByIndex(int indexString legend) {
		.set(indexlegend);
	}

Gets the legend of the data series by its index in the data set.

Parameters:
index index of the data series in the data set.
Returns:
the label of the data series.
	public String getLabelByIndex(int index) {
		return .get(index);
	}

Sets or change the legend of the data series by its index in the data set.

Parameters:
index index of the data series in the data set.
label the label of the data series.
	public void setLabelByIndex(int indexString label) {
		.set(indexlabel);
	}

Gets the string array of labels.

Returns:
the string array of labels.
	public String[] getLabels() {
		return .toArray(new String[1]);
	}

Gets the string array of legends.

Returns:
the string array of legends.
	public String[] getLegends() {
		return .toArray(new String[1]);
	}

Gets whether this data set uses first row as chart label.

Returns:
if return true, this data set uses first row as chart label.
	public boolean isFirstRowAsLabel() {
	}

Gets whether this data set uses first column as chart label.

Returns:
if return true, this data set uses first row as chart label.
	public boolean isFirstColumnAsLabel() {
	}

Gets whether this data set uses row as data series.

Returns:
if return true, this data set uses row as data series.
	public boolean isRowAsDataSeries() {
	}

Gets whether this data set uses local table.

Returns:
if return true, this data set uses uses local table.
	public boolean isLocalTable() {
		return ;
	}

Sets this data set uses local table or not.

Parameters:
isLocalTable if the value is true, this data set uses uses local table.
	public void setLocalTable(boolean isLocalTable) {
		this. = isLocalTable;
	}

Gets the values of a data series by its index in the data set.

Parameters:
index index of the data series in the data set.
Returns:
the values of the data series.
	public double[] getDataSeriesByIndex(int index) {
		double[] doubleArray;
		Vector<Doublevalues = .elementAt(index);
		doubleArray = new double[values.size()];
		for (int i = 0; i < values.size(); i++)
			doubleArray[i] = values.get(i).doubleValue();
		return doubleArray;
	}

Gets the values of a data series by its legend, if multiple data series have duplicated legends, return the values of the first matched data series. we do not recommend the duplicated legends, but practically, some ODF editors allow users to do that.

Parameters:
legend the legend of the data series
Returns:
the values of the data series
	public double[] getDataSeriesByLegend(String legendint beginindex) {
		double[] doubleArray;
		Vector<Doublevalues = .elementAt(getIndexOfDataSeries(legendbeginindex));
		doubleArray = new double[values.size()];
		for (int i = 0; i < values.size(); i++)
			doubleArray[i] = values.get(i).doubleValue();
		return doubleArray;
	}

Gets the values of a data set as a double dimension array.

Returns:
the values of the whole data set.
	public Double[][] getLocalTableData() {
		// if rowAsDataSeries, the local table data needs to turn 90 degree
		int seriescountitemcount;
		Double[][] doubleArray;
		seriescount = .size();
		itemcount = .size();
			doubleArray = new Double[seriescount][itemcount];
else {
			doubleArray = new Double[itemcount][seriescount];
		}
		for (int i = 0; i < seriescounti++) {
			Vector<Doublevalues = .elementAt(i);
			for (int j = 0; j < itemcountj++) {
					doubleArray[i][j] = values.get(j);
				else
					doubleArray[j][i] = values.get(j);
			}
		}
		return doubleArray;
	}

Gets the first row labels of the local table.

Returns:
the first row labels of the local table.
			return .toArray(new String[1]);
		else
			return .toArray(new String[1]);
	}

Gets the first column labels of the local table.

Returns:
the first column labels of the local table.
			return .toArray(new String[1]);
		else
			return .toArray(new String[1]);
	}

Gets the data item count of the data series given by the index.

Parameters:
index index of the data series in the data set
Returns:
the data item count of the data series
	public int getLengthOfDataSeries(int index) {
		Vector<Doublevalues = .elementAt(index);
		int length = values.size();
		for (int i = values.size() - 1; i >= 0; i--) {
			if (values.elementAt(i) == null)
				length--;
		}
		return length;
	}

Gets the data item count of the data series given by the label.

Parameters:
legend the legend of the data series
Returns:
the data item count of the data series
	public int getLengthOfDataSeries(String legendint beginindex) {
		int index = getIndexOfDataSeries(legendbeginindex);
		return getLengthOfDataSeries(index);
	}

Gets the maximal data item count

Returns:
the maximal data item count
	public int getMaxLengthOfDataSeries() {
		int max = 0;
		for (int i = 0; i < .size(); i++) {
			int length = getLengthOfDataSeries(i);
			if (max < length)
				max = length;
		}
		return max;
	}

Gets the count of data series in the data set.

Returns:
the count of data series
	public int getDataSeriesCount() {
		return .size();
	}

Sets or updates data for the data set with 2 dimensional double array, the first dimension represents the index of data series, the second dimension represents the index of data item in each data series.

Parameters:
labels the chart labels, which is corresponding to the first dimension of data array.
data a 2 dimensional double array.
	public void setValues(String[] labelsString[] legendsdouble[][] data) {
		this. = true;
		this. = true;
		this. = true;
		this. = false;
		int seriescount = (legends.length <= data.length) ? legends.length : data.length;
		int itemcount = (labels.length <= data[0].length) ? labels.length : data[0].length;
		for (int i = 0; i < seriescounti++) {
			if (i < legends.length)
				.add(legends[i]);
			else
				.add("" + (i + 1));
			Vector<Doubleseries = new Vector<Double>();
			for (int j = 0; j < itemcountj++) {
				if ((i < data.length) && (j < data[0].length))
					series.add(new Double(data[i][j]));
				else
					series.add(null);
			}
			.add(series);
		}
		for (int i = 0; i < itemcounti++) {
			if (i < labels.length)
				.add(labels[i]);
			else
		}
	}

Sets data for the data set with cell range address of sheet in spreadsheet document or internal chart table in other chart containers.

Parameters:
cellRangeAddress cell range address of sheet or table.
spreadsheet the spreadsheet document instance, the cell address is relative to the spreadsheet document.
isRowLabel true if select the first row as labels, false if select the first column as labels.
	public void setValues(CellRangeAddressList cellRangeAddressSpreadsheetDocument spreadsheet,
			boolean bFirstRowAsLabelboolean bFirstColumnAsLabelboolean rowAsDataSeries) {
		this. = false;
		this. = bFirstColumnAsLabel;
		this. = bFirstRowAsLabel;
		this. = rowAsDataSeries;
		this. = cellRangeAddress;
		// analysis cell range
		String cellrange = ((CellRangeAddresscellRangeAddress.getCellRangesAddressList().get(0)).toString();
		// init variables
		StringTokenizer st = new StringTokenizer(cellrange".:$ ");
		if (st.countTokens() < 3)
			return;
		String sheettable = st.nextToken();
		String begincell = st.nextToken();
		String endcell = st.nextToken();
		if (st.hasMoreTokens())
			endcell = st.nextToken();
		char beginColumn = begincell.charAt(0);
		char endColumn = endcell.charAt(0);
		int beginRow = Integer.parseInt(begincell.substring(1));
		int endRow = Integer.parseInt(endcell.substring(1));
		Vector<StringrowLabels = new Vector<String>();
		Vector<StringcolumnLabels = new Vector<String>();
		try {
			int i = 0, rowindexcolumnindex;
			char ch;
			OdfFileDom sheetContent;
			NodeList tableListrowListcellList;
			Vector<Doubleseries;
			double value;
			// get table
			sheetContent = spreadsheet.getContentDom();
			tableList = sheetContent.getElementsByTagNameNS(OdfNamespace.newNamespace(.)
					.toString(), "table");
			while (!sheettable.equals(((TableTableElementtableList.item(i)).getTableNameAttribute()))
				i++;
			table = (TableTableElementtableList.item(i);
					"table-row");
			// get data, begin for(beginRow,endRow)
			for (i = beginRowi <= endRowi++) {
				rowindex = bFirstRowAsLabel ? (i - beginRow - 1) : (i - beginRow);
				td = (TableTableRowElementrowList.item(i - 1);
						"table-cell");
				for (int index = 0, cellNum = 0; (cellNum < cellList.getLength()) && (index <= (endColumn - 'A')); cellNum++) {
					TableTableCellElement item = (TableTableCellElementcellList.item(cellNum);
					int repeatedCount = item.getTableNumberColumnsRepeatedAttribute()
					int tmpIndex = index + repeatedCount;
					if (tmpIndex >= (beginColumn - 'A')) {
						if ((beginColumn - 'A') > index) {
							index = beginColumn - 'A';
						}
						for (int ii = indexii < tmpIndexii++) {
							cellIndexMap.put(iiitem);
						}
					}
					index = tmpIndex;
				}
				// begin for(beginColumn,endColumn)
				for (ch = beginColumnch <= endColumnch++) {
					columnindex = bFirstColumnAsLabel ? (ch - beginColumn - 1) : (ch - beginColumn);
					cell = cellIndexMap.get(ch - 'A');
					if (bFirstRowAsLabel && (i == beginRow)) // label row
					{
						if (!bFirstColumnAsLabel || (ch != beginColumn)) {
							if (cell.getFirstChild() != null)
								rowLabels.add(cell.getFirstChild().getTextContent());
							else
								rowLabels.add("Column " + ch);
						}
else if (bFirstColumnAsLabel && (ch == beginColumn)) {
						if (!bFirstRowAsLabel || (i != beginRow)) {
							if (cell.getFirstChild() != null)
								columnLabels.add(cell.getFirstChild().getTextContent());
							else
								columnLabels.add("Row " + i);
						}
else {
						// set default rowLabel
						if ((i == beginRow) && (!bFirstColumnAsLabel || (ch != beginColumn))) {
							// first row is not label.
							rowLabels.add("Column " + ch);
						}
						// set default column label
						if ((ch == beginColumn) && (!bFirstRowAsLabel || (i != beginRow))) // first
							// column is not label.
							columnLabels.add("Row " + i);
						if (rowAsDataSeries) {
							if (rowindex < .size())
								series = .get(rowindex);
							else {
								series = new Vector<Double>();
								.add(series);
							}
							try {
								series.add(new Double(value));
catch (Exception e) {
								series.add(null);
							}
else {
							if (columnindex < .size())
								series = .get(columnindex);
							else {
								series = new Vector<Double>();
								.add(series);
							}
							try {
								series.add(new Double(value));
catch (Exception e) {
								series.add(null);
							}
						}
					}
				}// end begin for(beginColumn,endColumn)
			}// end begin for(beginRow,endRow)
			if (rowAsDataSeries) {
				 = rowLabels;
				 = columnLabels;
else {
				 = columnLabels;
				 = rowLabels;
			}
catch (Exception e) {
		}
	}
	// return label cell ranges
	String getCellRanges(String tablecellrangeboolean bFirstRowAsLabelboolean bFirstColumnAsLabel,
			boolean rowAsDataSeriesVector<StringseriesCellRangeVector<StringlegendCellAddr) {
		// prepare variables
		String labelCellRange = null;
		if (seriesCellRange == null)
			seriesCellRange = new Vector<String>();
		else
			seriesCellRange.removeAllElements();
		if (legendCellAddr == null)
			legendCellAddr = new Vector<String>();
		else
			legendCellAddr.removeAllElements();
		// seperate column and row from cell range
		StringTokenizer st = new StringTokenizer(tablecellrange".:$ ");
		if (st.countTokens() < 3)
			return null;
		String sheettable = st.nextToken();
		String begincell = st.nextToken();
		String endcell = st.nextToken();
		if (st.hasMoreTokens())
			endcell = st.nextToken();
		char beginColumn = begincell.charAt(0);
		char endColumn = endcell.charAt(0);
		int beginRow = Integer.parseInt(begincell.substring(1));
		int endRow = Integer.parseInt(endcell.substring(1));
		// if (bFirstColumnAsLabel) beginColumn=(char)(beginColumn1);
		// if (bFirstRowAsLabel)beginRow=beginRow1;
		if (rowAsDataSeries) {
			int starti = beginRow + 1;
			if (!bFirstRowAsLabel) {
				labelCellRange = null;
				starti = beginRow;
else if (bFirstColumnAsLabel)
				labelCellRange = createCellRange(sheettablebeginRow, (char) (beginColumn + 1), beginRowendColumn);
			else
				labelCellRange = createCellRange(sheettablebeginRowbeginColumnbeginRowendColumn);
			for (int i = startii < endRow + 1; i++) {
				if (bFirstColumnAsLabel)
					seriesCellRange.add(createCellRange(sheettablei, (char) (beginColumn + 1), iendColumn));
				else
					seriesCellRange.add(createCellRange(sheettableibeginColumniendColumn));
				if (bFirstColumnAsLabel)
					legendCellAddr.add(sheettable + "." + beginColumn + i);
				else
					legendCellAddr.add(null);
			}
else {
			char startch = (char) (beginColumn + 1);
			if (!bFirstColumnAsLabel) {
				labelCellRange = null;
				startch = beginColumn;
else if (bFirstRowAsLabel)
				labelCellRange = createCellRange(sheettablebeginRow + 1, beginColumnendRowbeginColumn);
			else
				labelCellRange = createCellRange(sheettablebeginRowbeginColumnendRowbeginColumn);
			for (char ch = startchch <= endColumnch++) {
				if (bFirstRowAsLabel)
					seriesCellRange.add(createCellRange(sheettablebeginRow + 1, chendRowch));
				else
					seriesCellRange.add(createCellRange(sheettablebeginRowchendRowch));
				if (bFirstRowAsLabel)
					legendCellAddr.add(sheettable + "." + ch + beginRow);
				else
					legendCellAddr.add(null);
			}
		}
		return labelCellRange;
	}
	//get cell range string
	private String createCellRange(String tableint beginRowchar beginColumnint endRowchar endColumn) {
		return table + "." + beginColumn + beginRow + ":" + table + "." + endColumn + endRow;
	}
New to GrepCode? Check out our FAQ X