KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > plot > ErrorBarFactory


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 jcckit.graphic.GraphPoint;
22 import jcckit.graphic.GraphicAttributes;
23 import jcckit.graphic.GraphicalComposite;
24 import jcckit.graphic.GraphicalElement;
25 import jcckit.graphic.Rectangle;
26 import jcckit.util.ConfigParameters;
27 import jcckit.util.Factory;
28
29 /**
30  * Symbol factory for creating symbols with error bars. It wraps
31  * a {@link SymbolFactory} for creating the symbol. The error bars
32  * are {@link Rectangle Rectangles}.
33  * <p>
34  * Curves with error bars are based on <em>two</em>
35  * {@link jcckit.data.DataCurve DataCurves}:
36  * <ol><li>The plain curve.
37  * <li>An instance which stores the errors in <i>x</i> and <i>y</i>.
38  * It is assumed that the errors are positive values defining
39  * the error symmetrically around the curve points.
40  * </ol>
41  * <p>
42  * The <tt>ErrorBarFactory</tt> needs an instance of {@link PositionHint}
43  * as initial {@link Hint} for the next curve. Its origin must be
44  * the origin of the data coordinate system in device-independent coordinates.
45  * The position of <tt>PositionHint</tt> must be undefined.
46  *
47  * @author Franz-Josef Elmer
48  */

49 public class ErrorBarFactory implements SymbolFactory {
50   /** Configuration parameter key. */
51   public static final String JavaDoc SYMBOL_FACTORY_KEY = "symbolFactory";
52
53   private final SymbolFactory _symbolFactory;
54   private final GraphicAttributes _attributes;
55   private final double _size;
56
57   /**
58    * Creates an instance from the specified configuration parameters.
59    * <table border=1 cellpadding=5>
60    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
61    * <th>Description</th></tr>
62    * <tr><td><tt>symbolFactory = null</tt></td>
63    * <td><tt>ConfigParameters</tt></td><td>no</td>
64    * <td>Definition of the wrapped {@link SymbolFactory} which generates
65    * the curve symbol without bars. By default an empty
66    * {@link GraphicalComposite} will be created.</td></tr>
67    * <tr><td><tt>size = 0</tt></td>
68    * <td><tt>double</tt></td><td>no</td>
69    * <td>Width of the error bars.</td></tr>
70    * <tr><td><tt>attributes = null</tt></td>
71    * <td><tt>ConfigParameters</tt></td><td>no</td>
72    * <td>Definition of the {@link GraphicAttributes} of the error
73    * bars.</td></tr>
74    * </table>
75    */

76   public ErrorBarFactory(ConfigParameters config) {
77     _symbolFactory = (SymbolFactory) Factory.createOrGet(
78         config.getNode(SYMBOL_FACTORY_KEY), null);
79     _size = config.getDouble(SIZE_KEY, 0);
80     _attributes = (GraphicAttributes) Factory.createOrGet(
81         config.getNode(ATTRIBUTES_KEY), null);
82   }
83
84   /**
85    * Creates the legend symbol. Calls the wrapped {@link SymbolFactory}
86    * or returns an empty instance of {@link GraphicalComposite} if undefined.
87    */

88   public GraphicalElement createLegendSymbol(GraphPoint centerPosition,
89                                              double size) {
90     return _symbolFactory == null ? new GraphicalComposite(null)
91               : _symbolFactory.createLegendSymbol(centerPosition, size);
92   }
93
94   /**
95    * Creates either the curve symbol or the error bars. Error bars are
96    * created when <tt>hintFromPreviousCurve</tt> is an instance of
97    * {@link PositionHint} and its position attribute is not <tt>null</tt>.
98    * Otherwise the curve symbol is created. The position attributes stores
99    * the curve point (in device-independent coordinates). The origin is
100    * always as set in the initial <tt>PositionHint</tt>. The hint for
101    * the next curve wrapped by the returned <tt>Symbol</tt> is always
102    * a <tt>PositionHint</tt>.
103    */

104   public Symbol createSymbol(GraphPoint point, Hint hintFromPreviousPoint,
105                              Hint hintFromPreviousCurve) {
106     GraphPoint origin = new GraphPoint(null);
107     GraphPoint position = null;
108     if (hintFromPreviousCurve instanceof PositionHint) {
109       origin = ((PositionHint) hintFromPreviousCurve).getOrigin();
110       position = ((PositionHint) hintFromPreviousCurve).getPosition();
111     }
112     if (position == null) {
113       if (_symbolFactory == null) {
114         return new Symbol(new GraphicalComposite(null), hintFromPreviousPoint,
115                           new PositionHint(origin, point));
116       } else {
117         return _symbolFactory.createSymbol(point, hintFromPreviousPoint,
118                                            new PositionHint(origin, point));
119       }
120     } else {
121       double xError = point.getX() - origin.getX();
122       double yError = point.getY() - origin.getY();
123       GraphicalComposite errorBars = new GraphicalComposite(null);
124       if (xError > 0) {
125         errorBars.addElement(new Rectangle(position, 2 * xError, _size,
126                                            _attributes));
127       }
128       if (yError > 0) {
129         errorBars.addElement(new Rectangle(position, _size, 2 * yError,
130                                            _attributes));
131       }
132       return new Symbol(errorBars, hintFromPreviousPoint,
133                         new PositionHint(origin, null));
134     }
135   }
136 }
137
Popular Tags