Source for 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
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------------
  28:  * ExtendedCategoryAxis.java
  29:  * -------------------------
  30:  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: ExtendedCategoryAxis.java,v 1.4.2.1 2005/10/25 20:37:34 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 07-Nov-2003 : Version 1 (DG);
  40:  * 07-Jan-2004 : Updated the createLabel() method (DG);
  41:  * 29-Jan-2004 : Added paint attribute (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.chart.axis;
  46: 
  47: import java.awt.Color;
  48: import java.awt.Font;
  49: import java.awt.Graphics2D;
  50: import java.awt.Paint;
  51: import java.util.HashMap;
  52: import java.util.Map;
  53: 
  54: import org.jfree.text.TextBlock;
  55: import org.jfree.text.TextFragment;
  56: import org.jfree.text.TextLine;
  57: import org.jfree.ui.RectangleEdge;
  58: 
  59: /**
  60:  * An extended version of the {@link CategoryAxis} class that supports 
  61:  * sublabels on the axis.
  62:  */
  63: public class ExtendedCategoryAxis extends CategoryAxis {
  64: 
  65:     /** Storage for the sublabels. */
  66:     private Map sublabels;
  67:     
  68:     /** The sublabel font. */
  69:     private Font sublabelFont;
  70:     
  71:     /** The sublabel paint. */
  72:     private Paint sublabelPaint;
  73:     
  74:     /**
  75:      * Creates a new axis.
  76:      * 
  77:      * @param label  the axis label.
  78:      */
  79:     public ExtendedCategoryAxis(String label) {
  80:         super(label);
  81:         this.sublabels = new HashMap();
  82:         this.sublabelFont = new Font("SansSerif", Font.PLAIN, 10);
  83:         this.sublabelPaint = Color.black;
  84:     }
  85:     
  86:     /**
  87:      * Returns the font for the sublabels.
  88:      * 
  89:      * @return The font.
  90:      */
  91:     public Font getSubLabelFont() {
  92:         return this.sublabelFont;
  93:     }
  94:     
  95:     /**
  96:      * Sets the font for the sublabels.
  97:      * 
  98:      * @param font  the font.
  99:      */
 100:     public void setSubLabelFont(Font font) {
 101:         this.sublabelFont = font;
 102:     }
 103:     
 104:     /**
 105:      * Returns the paint for the sublabels.
 106:      * 
 107:      * @return The paint.
 108:      */
 109:     public Paint getSubLabelPaint() {
 110:         return this.sublabelPaint;
 111:     }
 112:     
 113:     /**
 114:      * Sets the paint for the sublabels.
 115:      * 
 116:      * @param paint  the paint.
 117:      */
 118:     public void setSubLabelPaint(Paint paint) {
 119:         this.sublabelPaint = paint;
 120:     }
 121:     
 122:     /**
 123:      * Adds a sublabel for a category.
 124:      * 
 125:      * @param category  the category.
 126:      * @param label  the label.
 127:      */
 128:     public void addSubLabel(Comparable category, String label) {
 129:         this.sublabels.put(category, label);
 130:     }
 131:     
 132:     /**
 133:      * Overrides the default behaviour by adding the sublabel to the text 
 134:      * block that is used for the category label.
 135:      * 
 136:      * @param category  the category.
 137:      * @param width  the width (not used yet).
 138:      * @param edge  the location of the axis.
 139:      * @param g2  the graphics device.
 140:      * 
 141:      * @return A label.
 142:      */
 143:     protected TextBlock createLabel(Comparable category, float width, 
 144:                                     RectangleEdge edge, Graphics2D g2) {
 145:         TextBlock label = super.createLabel(category, width, edge, g2);   
 146:         String s = (String) this.sublabels.get(category);
 147:         if (s != null) {
 148:             if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
 149:                 TextLine line = new TextLine(
 150:                     s, this.sublabelFont, this.sublabelPaint
 151:                 );
 152:                 label.addLine(line);
 153:             }
 154:             else if (edge == RectangleEdge.LEFT 
 155:                     || edge == RectangleEdge.RIGHT) {
 156:                 TextLine line = label.getLastLine();
 157:                 if (line != null) {
 158:                     line.addFragment(
 159:                         new TextFragment(
 160:                             "  " + s, this.sublabelFont, this.sublabelPaint
 161:                         )
 162:                     );
 163:                 }
 164:             }
 165:         }
 166:         return label; 
 167:     }
 168:     
 169: }