KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > plot > SimpleCurve


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.plot;
20
21 import java.awt.Color JavaDoc;
22
23 import jcckit.graphic.ClippingShape;
24 import jcckit.graphic.GraphPoint;
25 import jcckit.graphic.GraphicalComposite;
26 import jcckit.graphic.GraphicalElement;
27 import jcckit.graphic.LineAttributes;
28 import jcckit.graphic.Polygon;
29 import jcckit.graphic.ShapeAttributes;
30 import jcckit.util.ConfigParameters;
31 import jcckit.util.Factory;
32
33 /**
34  * A simple curve is the basic implementation of the {@link Curve} interface.
35  *
36  * @author Franz-Josef Elmer
37  */

38 public class SimpleCurve implements Curve {
39   /** Configuration parameter key. */
40   public static final String JavaDoc SYMBOL_FACTORY_KEY = "symbolFactory",
41                              WITH_LINE_KEY = "withLine",
42                              SOFT_CLIPPING_KEY = "softClipping",
43                              LINE_ATTRIBUTES_KEY = "lineAttributes",
44                              INITIAL_HINT_FOR_NEXT_POINT_KEY
45                                       = "initialHintForNextPoint";
46   private final ClippingShape _clippingShape;
47   private final SymbolFactory _symbolFactory;
48   private final GraphicalComposite _symbols;
49   private final GraphicalComposite _completeCurve;
50   private final GraphicalElement _legendSymbol;
51   private final Hint _initialHintForNextPoint;
52   private final Polygon _curve;
53   private final boolean _softClipping;
54   private Hint _hintForNextPoint;
55
56   /**
57    * Creates a new curve. The parameter <tt>config</tt> contains:
58    * <table border=1 cellpadding=5>
59    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
60    * <th>Description</th></tr>
61    * <tr><td><tt>initialHintForNextPoint = null</tt></td>
62    * <td><tt>ConfigParameters</tt></td><td>no</td>
63    * <td>Definition of an initial {@link Hint} for the first curve point.
64    * </td></tr>
65    * <tr><td><tt>lineAttributes = </tt>a {@link ShapeAttributes}
66    * instances with default values and line colors based on
67    * the formula <tt>Color.getHSBColor(curveIndex/6,1,0.8)</tt></td>
68    * <td><tt>ConfigParameters</tt></td><td>no</td>
69    * <td>Configuration parameters of an instances of
70    * {@link jcckit.graphic.GraphicAttributes} for the
71    * {@link Polygon Polygons} connecting curve points.</td></tr>
72    * <tr><td><tt>symbolFactory = null</tt></td>
73    * <td><tt>ConfigParameters</tt></td><td>no</td>
74    * <td>Configuration parameters defining an instances of
75    * {@link SymbolFactory} for the {@link Symbol Symbols}
76    * decorating curve points.</td></tr>
77    * <tr><td><tt>softClipping = true</tt></td>
78    * <td><tt>boolean</tt></td><td>no</td>
79    * <td>If <tt>true</tt> no explicit clipping takes
80    * place but the symbol is not drawn if the corresponding curve
81    * point is outside the axis box.<br>
82    * If <tt>false</tt> the symbol is
83    * drawn in any case but it may be clipped by the axis box.
84    * Soft-clipping should be set to <tt>false</tt> if the
85    * symbols are not located around the curve point (like for bars).
86    * </td></tr>
87    * <tr><td><tt>withLine = true</tt></td>
88    * <td><tt>boolean</tt></td><td>no</td>
89    * <td>If <tt>true</tt> curve points are connected by a
90    * {@link jcckit.graphic.Polygon}.</td></tr>
91    * </table>
92    * @param config Configuration parameters described above.
93    * @param curveIndex Index of this curve in the collection of curves
94    * defining a {@link Plot}.
95    * @param numberOfCurves Number of curves in this collection.
96    * @param clippingShape Clipping shape. Can be <tt>null</tt>.
97    * @param legend Legend. Will be used to calculate the legend symbol.
98    * @throws IllegalArgumentException if <tt>symbolFactory == null</tt> and
99    * <tt>withLine == false</tt>.
100    *
101    */

102   public SimpleCurve(ConfigParameters config, int curveIndex,
103                      int numberOfCurves, ClippingShape clippingShape,
104                      Legend legend) {
105     _symbolFactory = (SymbolFactory) Factory.createOrGet(
106         config.getNode(SYMBOL_FACTORY_KEY), null);
107     boolean withLine = config.getBoolean(WITH_LINE_KEY, true);
108     LineAttributes lineAttributes = (LineAttributes) Factory.createOrGet(
109         config.getNode(LINE_ATTRIBUTES_KEY),
110         new ShapeAttributes(null, Color.getHSBColor((curveIndex % 6) / 6f,
111                                                     1f, 0.8f),
112                             0, null));
113     if (_symbolFactory != null || withLine) {
114       _clippingShape = clippingShape;
115       _completeCurve = new GraphicalComposite(null);
116       if (withLine) {
117         GraphicalComposite container = new GraphicalComposite(clippingShape);
118         _curve = new Polygon(lineAttributes, false);
119         container.addElement(_curve);
120        _completeCurve.addElement(container);
121       } else {
122         _curve = null;
123       }
124       _softClipping = config.getBoolean(SOFT_CLIPPING_KEY, true);
125       if (_symbolFactory != null) {
126         _symbols = new GraphicalComposite(_softClipping ? null
127                                                         : clippingShape);
128         _completeCurve.addElement(_symbols);
129       } else {
130         _symbols = null;
131       }
132     } else {
133       throw new IllegalArgumentException JavaDoc(
134           "Either a SymbolFactory must exist or withLines == true.");
135     }
136     _hintForNextPoint = _initialHintForNextPoint
137         = (Hint) Factory.createOrGet(
138             config.getNode(INITIAL_HINT_FOR_NEXT_POINT_KEY), null);
139     _legendSymbol = legend.createSymbol(curveIndex, numberOfCurves,
140                                         _symbolFactory, withLine,
141                                         lineAttributes);
142   }
143
144   /**
145    * Returns the graphical representation of a curve.
146    * @return always the same instance.
147    */

148   public GraphicalElement getView() {
149     return _completeCurve;
150   }
151
152   /** Returns the legend symbol. */
153   public GraphicalElement getLegendSymbol() {
154     return _legendSymbol;
155   }
156
157   /** Appends a new point to the curve if inside the clipping shape. */
158   public Hint addPoint(GraphPoint point, Hint hintFromPreviousCurve) {
159     if (_curve != null) {
160       _curve.addPoint(point);
161     }
162     Hint hintForNextCurve = hintFromPreviousCurve;
163     if (_symbolFactory != null) {
164       Symbol symbol = _symbolFactory.createSymbol(point, _hintForNextPoint,
165                                                   hintFromPreviousCurve);
166       if (_clippingShape == null || !_softClipping
167           || _clippingShape.isInside(point)) {
168         _symbols.addElement(symbol.getSymbol());
169       }
170       _hintForNextPoint = symbol.getHintForNextPoint();
171       hintForNextCurve = symbol.getHintForNextCurve();
172     }
173     return hintForNextCurve;
174   }
175
176   public void removeAllPoints() {
177     if (_curve != null) {
178       _curve.removeAllPoints();
179     }
180     if (_symbols != null) {
181       _symbols.removeAllElements();
182     }
183     _hintForNextPoint = _initialHintForNextPoint;
184   }
185 }
186
Popular Tags