KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > plot > BarFactory


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.GraphicalElement;
24 import jcckit.graphic.Rectangle;
25 import jcckit.util.ConfigParameters;
26
27 /**
28  * A factory of bars. The bars are {@link Rectangle Rectangles}.
29  * Depending on the configuration parameters the bars can be
30  * horizontal or vertical. Bars of several curves can be side by side or
31  * stacked. The bar length is determined by the x or y value of the
32  * curve point in device-independent coordinates. If the value is negative
33  * the bar goes into the negative direction. For stacked bars the values
34  * should always be positive.
35  * <p>
36  * When used inside a {@link SimpleCurve} soft clipping should always be
37  * switched off (see
38  * {@link SimpleCurve#SimpleCurve(ConfigParameters, int, int, ClippingShape, Legend)}).
39  *
40  * @author Franz-Josef Elmer
41  */

42 public class BarFactory extends AbstractSymbolFactory {
43   /** Configuration parameter key. */
44   public static final String JavaDoc STACKED_KEY = "stacked",
45                              HORIZONTAL_BARS_KEY = "horizontalBars";
46
47   private final boolean _stacked;
48   private final boolean _horizontalBars;
49
50   /**
51    * Creates an instance from the specified configuration parameters.
52    * <table border=1 cellpadding=5>
53    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
54    * <th>Description</th></tr>
55    * <tr><td><tt>horizontalBars = false</tt></td>
56    * <td><tt>boolean</tt></td><td>no</td>
57    * <td>If <tt>true</tt> horizontal bars will be drawn. Otherwise
58    * vertical bars are drawn.</td></tr>
59    * <tr><td><tt>stacked = false</tt></td>
60    * <td><tt>boolean</tt></td><td>no</td>
61    * <td>If <tt>true</tt> the bars of several curves will be
62    * stacked.</td></tr>
63    * </table>
64    * In addition the configuration parameters of the
65    * <a HREF="AbstractSymbolFactory.html#AbstractSymbolFactory(jcckit.util.ConfigParameters)">
66    * constructor</a> of the superclass {@link AbstractSymbolFactory} apply.
67    */

68   public BarFactory(ConfigParameters config) {
69     super(config);
70     _horizontalBars = config.getBoolean(HORIZONTAL_BARS_KEY, false);
71     _stacked = config.getBoolean(STACKED_KEY, false);
72   }
73
74   /**
75    * Creates a bar at the specified point.
76    * If <tt>hintFromPreviousCurve</tt>
77    * is not an instance of {@link PositionHint} the values of
78    * origin and position will be (0,0).
79    * @param hintFromPreviousCurve Hint from previous curve. Will be used
80    * to calculate symbol shape and hint for the next curve.
81    */

82   protected Symbol createSymbol(GraphPoint point, GraphicAttributes attributes,
83                              Hint hintForNextPoint,
84                              Hint hintFromPreviousCurve) {
85     GraphPoint origin = new GraphPoint(null);
86     GraphPoint position = origin;
87     if (hintFromPreviousCurve instanceof PositionHint) {
88       origin = ((PositionHint) hintFromPreviousCurve).getOrigin();
89       position = ((PositionHint) hintFromPreviousCurve).getPosition();
90     }
91     double px = position.getX();
92     double py = position.getY();
93     double x = point.getX() - origin.getX();
94     double y = point.getY() - origin.getY();
95     if (_horizontalBars) {
96       y = _size;
97       position = new GraphPoint(px + 0.5 * x, point.getY() + py);
98       px += _stacked ? x : 0;
99       py += _stacked ? 0 : _size;
100     } else {
101       x = _size;
102       position = new GraphPoint(point.getX() + px, py + 0.5 * y);
103       px += _stacked ? 0 : _size;
104       py += _stacked ? y : 0;
105     }
106     Hint hintForNextCurve = new PositionHint(origin, new GraphPoint(px, py));
107     return new Symbol(new Rectangle(position, Math.abs(x), Math.abs(y),
108                                     attributes),
109                       hintForNextPoint, hintForNextCurve);
110   }
111
112   /**
113    * Creates a symbol for the legend at the specified position.
114    * @param centerPosition Center position of the symbol.
115    * @param size The size of the symbol.
116    */

117   public GraphicalElement createLegendSymbol(GraphPoint centerPosition,
118                                              double size) {
119     return new Rectangle(centerPosition, size, size, _attributes);
120   }
121   
122   /**
123    * Returns <tt>null</tt> because this method isn't needed but has to be
124    * implemented.
125    */

126   protected GraphicalElement createPlainSymbol(
127       GraphPoint centerPosition, double size, GraphicAttributes attributes) {
128     return null;
129   }
130 }
131
Popular Tags