1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ------------------
  27. * DataUtilities.java
  28. * ------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: DataUtilities.java,v 1.3 2005/03/04 11:48:23 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 05-Mar-2003 : Version 1 (DG);
  39. * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
  40. * from the DatasetUtilities class (DG);
  41. *
  42. */
  43. package org.jfree.data.general;
  44. import org.jfree.data.DefaultKeyedValues;
  45. import org.jfree.data.KeyedValues;
  46. /**
  47. * Utility methods for use with some of the data classes (but not the datasets,
  48. * see {@link DatasetUtilities}).
  49. */
  50. public abstract class DataUtilities {
  51. /**
  52. * Constructs an array of <code>Number</code> objects from an array of
  53. * <code>double</code> primitives.
  54. *
  55. * @param data the data (<code>null</code> not permitted).
  56. *
  57. * @return An array of <code>Double</code>.
  58. */
  59. public static Number[] createNumberArray(double[] data) {
  60. if (data == null) {
  61. throw new IllegalArgumentException("Null 'data' argument.");
  62. }
  63. Number[] result = new Number[data.length];
  64. for (int i = 0; i < data.length; i++) {
  65. result[i] = new Double(data[i]);
  66. }
  67. return result;
  68. }
  69. /**
  70. * Constructs an array of arrays of <code>Number</code> objects from a
  71. * corresponding structure containing <code>double</code> primitives.
  72. *
  73. * @param data the data (<code>null</code> not permitted).
  74. *
  75. * @return An array of <code>Double</code>.
  76. */
  77. public static Number[][] createNumberArray2D(double[][] data) {
  78. if (data == null) {
  79. throw new IllegalArgumentException("Null 'data' argument.");
  80. }
  81. int l1 = data.length;
  82. Number[][] result = new Number[l1][];
  83. for (int i = 0; i < l1; i++) {
  84. result[i] = createNumberArray(data[i]);
  85. }
  86. return result;
  87. }
  88. /**
  89. * Returns a {@link KeyedValues} instance that contains the cumulative
  90. * percentage values for the data in another {@link KeyedValues} instance.
  91. * <p>
  92. * The percentages are values between 0.0 and 1.0 (where 1.0 = 100%).
  93. *
  94. * @param data the data (<code>null</code> not permitted).
  95. *
  96. * @return The cumulative percentages.
  97. */
  98. public static KeyedValues getCumulativePercentages(KeyedValues data) {
  99. if (data == null) {
  100. throw new IllegalArgumentException("Null 'data' argument.");
  101. }
  102. DefaultKeyedValues result = new DefaultKeyedValues();
  103. double total = 0.0;
  104. for (int i = 0; i < data.getItemCount(); i++) {
  105. Number v = data.getValue(i);
  106. if (v != null) {
  107. total = total + v.doubleValue();
  108. }
  109. }
  110. double runningTotal = 0.0;
  111. for (int i = 0; i < data.getItemCount(); i++) {
  112. Number v = data.getValue(i);
  113. if (v != null) {
  114. runningTotal = runningTotal + v.doubleValue();
  115. }
  116. result.addValue(data.getKey(i), new Double(runningTotal / total));
  117. }
  118. return result;
  119. }
  120. }