package com.positive.charts.util;
import com.positive.charts.data.DefaultKeyedValues;
import com.positive.charts.data.KeyedValues;
import com.positive.charts.data.Values2D;
import com.positive.charts.data.util.DatasetUtilities;
/**
* Utility methods for use with some of the data classes (but not the datasets,
* see {@link DatasetUtilities}).
*/
public abstract class DataUtilities {
/**
* Returns the total of the values in one column of the supplied data table.
*
* @param data
* the table of values (<code>null</code> not permitted).
* @param column
* the column index (zero-based).
*
* @return The total of the values in the specified column.
*/
public static double calculateColumnTotal(final Values2D data,
final int column) {
double total = 0.0;
final int rowCount = data.getRowCount();
for (int r = 0; r < rowCount; r++) {
final Number n = data.getValue(r, column);
if (n != null) {
total += n.doubleValue();
}
}
return total;
}
/**
* Returns the total of the values in one row of the supplied data table.
*
* @param data
* the table of values (<code>null</code> not permitted).
* @param row
* the row index (zero-based).
*
* @return The total of the values in the specified row.
*/
public static double calculateRowTotal(final Values2D data, final int row) {
double total = 0.0;
final int columnCount = data.getColumnCount();
for (int c = 0; c < columnCount; c++) {
final Number n = data.getValue(row, c);
if (n != null) {
total += n.doubleValue();
}
}
return total;
}
/**
* Constructs an array of <code>Number</code> objects from an array of
* <code>double</code> primitives.
*
* @param data
* the data (<code>null</code> not permitted).
*
* @return An array of <code>Double</code>.
*/
public static Number[] createNumberArray(final double[] data) {
if (data == null) {
throw new IllegalArgumentException("Null 'data' argument.");
}
final Number[] result = new Number[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = new Double(data[i]);
}
return result;
}
/**
* Constructs an array of arrays of <code>Number</code> objects from a
* corresponding structure containing <code>double</code> primitives.
*
* @param data
* the data (<code>null</code> not permitted).
*
* @return An array of <code>Double</code>.
*/
public static Number[][] createNumberArray2D(final double[][] data) {
if (data == null) {
throw new IllegalArgumentException("Null 'data' argument.");
}
final int l1 = data.length;
final Number[][] result = new Number[l1][];
for (int i = 0; i < l1; i++) {
result[i] = createNumberArray(data[i]);
}
return result;
}
/**
* Returns a {@link KeyedValues} instance that contains the cumulative
* percentage values for the data in another {@link KeyedValues} instance.
* <p>
* The percentages are values between 0.0 and 1.0 (where 1.0 = 100%).
*
* @param data
* the data (<code>null</code> not permitted).
*
* @return The cumulative percentages.
*/
public static KeyedValues getCumulativePercentages(final KeyedValues data) {
if (data == null) {
throw new IllegalArgumentException("Null 'data' argument.");
}
final DefaultKeyedValues result = new DefaultKeyedValues();
double total = 0.0;
for (int i = 0; i < data.getItemCount(); i++) {
final Number v = data.getValue(i);
if (v != null) {
total = total + v.doubleValue();
}
}
double runningTotal = 0.0;
for (int i = 0; i < data.getItemCount(); i++) {
final Number v = data.getValue(i);
if (v != null) {
runningTotal = runningTotal + v.doubleValue();
}
result.addValue(data.getKey(i), new Double(runningTotal / total));
}
return result;
}
}