KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > ExtendedCategoryAxis


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  * ExtendedCategoryAxis.java
28  * -------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: ExtendedCategoryAxis.java,v 1.4 2005/03/29 12:54:56 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 07-Nov-2003 : Version 1 (DG);
39  * 07-Jan-2004 : Updated the createLabel() method (DG);
40  * 29-Jan-2004 : Added paint attribute (DG);
41  *
42  */

43
44 package org.jfree.chart.axis;
45
46 import java.awt.Color JavaDoc;
47 import java.awt.Font JavaDoc;
48 import java.awt.Graphics2D JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.Map JavaDoc;
52
53 import org.jfree.text.TextBlock;
54 import org.jfree.text.TextFragment;
55 import org.jfree.text.TextLine;
56 import org.jfree.ui.RectangleEdge;
57
58 /**
59  * An extended version of the {@link CategoryAxis} class that supports
60  * sublabels on the axis.
61  */

62 public class ExtendedCategoryAxis extends CategoryAxis {
63
64     /** Storage for the sublabels. */
65     private Map JavaDoc sublabels;
66     
67     /** The sublabel font. */
68     private Font JavaDoc sublabelFont;
69     
70     /** The sublabel paint. */
71     private Paint JavaDoc sublabelPaint;
72     
73     /**
74      * Creates a new axis.
75      *
76      * @param label the axis label.
77      */

78     public ExtendedCategoryAxis(String JavaDoc label) {
79         super(label);
80         this.sublabels = new HashMap JavaDoc();
81         this.sublabelFont = new Font JavaDoc("SansSerif", Font.PLAIN, 10);
82         this.sublabelPaint = Color.black;
83     }
84     
85     /**
86      * Returns the font for the sublabels.
87      *
88      * @return The font.
89      */

90     public Font JavaDoc getSubLabelFont() {
91         return this.sublabelFont;
92     }
93     
94     /**
95      * Sets the font for the sublabels.
96      *
97      * @param font the font.
98      */

99     public void setSubLabelFont(Font JavaDoc font) {
100         this.sublabelFont = font;
101     }
102     
103     /**
104      * Returns the paint for the sublabels.
105      *
106      * @return The paint.
107      */

108     public Paint JavaDoc getSubLabelPaint() {
109         return this.sublabelPaint;
110     }
111     
112     /**
113      * Sets the paint for the sublabels.
114      *
115      * @param paint the paint.
116      */

117     public void setSubLabelPaint(Paint JavaDoc paint) {
118         this.sublabelPaint = paint;
119     }
120     
121     /**
122      * Adds a sublabel for a category.
123      *
124      * @param category the category.
125      * @param label the label.
126      */

127     public void addSubLabel(Comparable JavaDoc category, String JavaDoc label) {
128         this.sublabels.put(category, label);
129     }
130     
131     /**
132      * Overrides the default behaviour by adding the sublabel to the text
133      * block that is used for the category label.
134      *
135      * @param category the category.
136      * @param width the width (not used yet).
137      * @param edge the location of the axis.
138      * @param g2 the graphics device.
139      *
140      * @return A label.
141      */

142     protected TextBlock createLabel(Comparable JavaDoc category, float width,
143                                     RectangleEdge edge, Graphics2D JavaDoc g2) {
144         TextBlock label = super.createLabel(category, width, edge, g2);
145         String JavaDoc s = (String JavaDoc) this.sublabels.get(category);
146         if (s != null) {
147             if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
148                 TextLine line = new TextLine(
149                     s, this.sublabelFont, this.sublabelPaint
150                 );
151                 label.addLine(line);
152             }
153             else if (edge == RectangleEdge.LEFT
154                     || edge == RectangleEdge.RIGHT) {
155                 TextLine line = label.getLastLine();
156                 if (line != null) {
157                     line.addFragment(
158                         new TextFragment(
159                             " " + s, this.sublabelFont, this.sublabelPaint
160                         )
161                     );
162                 }
163             }
164         }
165         return label;
166     }
167     
168 }
169
Popular Tags