KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > data > DataCurve


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.data;
20
21 import jcckit.util.ConfigParameters;
22
23 /**
24  * A curve is a {@link DataContainer} of {@link DataPoint DataPoints}.
25  *
26  * @author Franz-Josef Elmer
27  */

28 public class DataCurve extends DataContainer implements DataElement {
29   /** Config parameter key. */
30   public static final String JavaDoc X_KEY = "x",
31                              Y_KEY = "y",
32                              TITLE_KEY = "title";
33
34   private final String JavaDoc _title;
35   private DataContainer _container;
36
37   /** Creates an empty instance with the specified title. */
38   public DataCurve(String JavaDoc title) {
39     _title = title;
40   }
41
42   /**
43    * Creates an instance from the specified config parameters.
44    * <table border=1 cellpadding=5>
45    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
46    * <th>Description</th></tr>
47    * <tr><td><tt>title = </tt><i>empty string</i></td>
48    * <td><tt>String</tt></td><td>no</td>
49    * <td>Curve title.</td></tr>
50    * <tr><td><tt>x</tt></td><td><tt>double[]</tt></td><td>yes</td>
51    * <td>x-coordinates of the curve points.</td></tr>
52    * <tr><td><tt>y</tt></td><td><tt>double[]</tt></td><td>yes</td>
53    * <td>y-coordinates of the curve points.</td></tr>
54    * </table>
55    */

56   public DataCurve(ConfigParameters config) {
57     this(config.get(TITLE_KEY, ""));
58     double[] xPoints = config.getDoubleArray(X_KEY);
59     double[] yPoints = config.getDoubleArray(Y_KEY);
60     int n = Math.min(xPoints.length, yPoints.length);
61     for (int i = 0; i < n; i++) {
62       addElement(new DataPoint(xPoints[i], yPoints[i]));
63     }
64   }
65
66   /**
67    * Returns the {@link DataPlot} containing this curve.
68    */

69   public DataContainer getContainer() {
70     return _container;
71   }
72
73
74   /**
75    * Sets the {@link DataPlot} where this is a curve of.
76    */

77   public void setContainer(DataContainer container) {
78     _container = container;
79   }
80
81   /** Returns the title of this curve. */
82   public String JavaDoc getTitle() {
83     return _title;
84   }
85
86   /**
87    * Returns <tt>true</tt> if <tt>element</tt> is an instance of
88    * {@link DataPoint}.
89    */

90   protected boolean isValid(DataElement element) {
91     return element instanceof DataPoint;
92   }
93 }
94
Popular Tags