001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: Legend.java
021: Created on 26. Juni 2001, 22:49
022: */
023:
024: package de.progra.charting;
025:
026: import java.awt.*;
027: import java.awt.geom.*;
028: import java.awt.font.*;
029: import java.awt.image.*;
030: import de.progra.charting.render.*;
031:
032: /** This class implements a Chart's Legend. The Strings and the colors
033: * can be set manually or eventually through some kind of data model.
034: */
035: public class Legend extends AbstractRenderer {
036:
037: protected int inner_margin = 5;
038: protected int color_text_spacing = 10;
039: protected Font font = new Font("Helvetica", Font.PLAIN, 14);
040: protected Rectangle colorbox = new Rectangle(25, 15);
041:
042: protected RowColorModel rcm;
043:
044: /** Creates a default Legend. */
045: public Legend() {
046: }
047:
048: /** Creates a Legend with the given Strings and Colors.
049: * @param rcm the RowColorModel containing the row titles and their colors.
050: */
051: public Legend(RowColorModel rcm) {
052: setRowColorModel(rcm);
053: }
054:
055: /** Defines the RowColorModel of the DataModel.
056: * @param rcm the RowColorModel
057: */
058: public void setRowColorModel(RowColorModel rcm) {
059: this .rcm = rcm;
060: }
061:
062: /** Returns the RowColorModel of the DataModel.
063: * @return the RowColorModel
064: */
065: public RowColorModel getRowColorModel() {
066: return rcm;
067: }
068:
069: /** Sets the size of the color boxes.
070: * @param r the Rectangle defining the colored box rendered left to every Legend entry.
071: */
072: public void setColorBox(Rectangle r) {
073: colorbox = r;
074: }
075:
076: /** Returns the size of the color boxes.
077: * @return the Rectangle defining the colored box left to every Legend entry
078: */
079: public Rectangle getColorBox() {
080: return colorbox;
081: }
082:
083: /** Sets the Font that is used to render the Legend.
084: * @param f the font object
085: */
086: public void setFont(Font f) {
087: font = f;
088: }
089:
090: /** Returns this Legend's Font.
091: * @return the font currently in use
092: */
093: public Font getFont() {
094: return font;
095: }
096:
097: /** Returns the preferred size needed for the renderer.
098: * @return a non-null Dimension object
099: */
100: public Dimension getPreferredSize() {
101: RowColorModel rcm = getRowColorModel();
102:
103: int maxTitleWidth = Integer.MIN_VALUE;
104: int titleHeight = Integer.MIN_VALUE;
105:
106: titleHeight = (int) getFont().getMaxCharBounds(
107: new FontRenderContext(null, true, false)).getHeight();
108:
109: for (int i = 0; i < rcm.getRowCount(); i++) {
110: TextLayout layout = new TextLayout(rcm.getRow(i),
111: getFont(), new FontRenderContext(null, true, false));
112:
113: maxTitleWidth = (int) Math.max((double) maxTitleWidth,
114: layout.getBounds().getWidth());
115: }
116:
117: return new Dimension(
118: (int) (2 * inner_margin + color_text_spacing
119: + getColorBox().getWidth() + maxTitleWidth),
120: Math.max(titleHeight, (int) getColorBox().getHeight())
121: * rcm.getRowCount() + (rcm.getRowCount() + 1)
122: * inner_margin);
123: }
124:
125: /** This method is called by the paint method to do the actual painting.
126: * The painting is supposed to start at point (0,0) and the size is
127: * always the same as the preferred size. The paint method performs
128: * the possible scaling.
129: * @param g the <CODE>Graphics2D</CODE> object to paint in
130: */
131: public void paintDefault(Graphics2D g) {
132: RowColorModel rcm = getRowColorModel();
133:
134: int height = Integer.MIN_VALUE;
135:
136: int fontheight = (int) getFont().getMaxCharBounds(
137: g.getFontRenderContext()).getHeight();
138: height = (int) Math.max(fontheight, getColorBox().getHeight());
139:
140: int startx = inner_margin;
141: int starty = inner_margin;
142:
143: Rectangle colorBox = getColorBox();
144:
145: /* Rendering the Text and the ColorBoxes. */
146: for (int i = 0; i < rcm.getRowCount(); i++) {
147: colorBox.setLocation(startx, starty);
148: g.setColor(rcm.getColor(i));
149:
150: g.fill(colorBox);
151:
152: g.setColor(Color.black);
153:
154: TextLayout layout = new TextLayout(rcm.getRow(i),
155: getFont(), new FontRenderContext(null, true, false));
156:
157: layout.draw(g, startx + (int) colorBox.getWidth()
158: + color_text_spacing, starty
159: + (int) colorBox.getHeight());
160:
161: starty = starty + height + inner_margin;
162: }
163: }
164: }
|