001: /**
002: * Chart2D, a java library for drawing two dimensional charts.
003: * Copyright (C) 2001 Jason J. Simas
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * The author of this library may be contacted at:
019: * E-mail: jjsimas@users.sourceforge.net
020: * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
021: */package net.sourceforge.chart2d;
022:
023: import java.awt.*;
024: import java.awt.image.*;
025:
026: /**
027: * A class for the methods of GraphChart2D charts of the "Labels Bottom" type.
028: * A LBChart2D object is one with it's category descriptor labels along the bottom (or x) axis.
029: * For example, a vertical bar chart is a LBChart2D type chart.
030: * Changes through its set methods are updated upon next repaint() or getImage() calls.
031: */
032: public final class LBChart2D extends GraphChart2D {
033:
034: private boolean needsUpdate;
035: private LBChartArea chart;
036: private BufferedImage image;
037: private Dimension size;
038: private Dimension imageSize;
039: private Dimension prefSize;
040: private boolean customizePrefSize;
041: private Dimension customPrefSize;
042:
043: /**
044: * Creates a LBChart2D object with its defaults.
045: */
046: public LBChart2D() {
047:
048: needsUpdate = true;
049: chart = new LBChartArea();
050: size = new Dimension();
051: imageSize = new Dimension();
052: prefSize = null;
053: customizePrefSize = false;
054: customPrefSize = null;
055: }
056:
057: /**
058: * Sets a custom preferred size for the chart.
059: * This custom size will override the preferred size calculations that normally occurr.
060: * If null is passed, the preferred size calculations will be reinstated.
061: * @param size The custom preferred size for this chart.
062: */
063: public final void setPreferredSize(Dimension size) {
064:
065: needsUpdate = true;
066: customizePrefSize = size != null;
067: customPrefSize = size;
068: prefSize = null;
069: }
070:
071: /**
072: * Gets a buffered image of the chart.
073: * @return An image of this chart
074: */
075: public final BufferedImage getImage() {
076:
077: if (getSize().width <= 0 || getSize().height <= 0)
078: pack();
079: else
080: updateImage(getSize());
081:
082: if (!chart.getBackgroundExistence()) {
083:
084: Graphics2D imageG2D = image.createGraphics();
085: imageG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
086: RenderingHints.VALUE_ANTIALIAS_ON);
087: chart.paintComponent(imageG2D);
088: Point location = chart.getSizeLocation(chart.MIN);
089: imageSize = chart.getSize(chart.MIN);
090: image = image.getSubimage(location.x, location.y,
091: imageSize.width, imageSize.height);
092: }
093:
094: return image;
095: }
096:
097: /**
098: * Gets the preferred size of the chart.
099: * The preferred size is within the maximum and minimum sizes of the chart.
100: * Much calculation is performed when calling this method.
101: * @return The preferred minimum size of the chart.
102: */
103: public final Dimension getPreferredSize() {
104:
105: updateGraphChart2D();
106:
107: if (!customizePrefSize) {
108:
109: boolean autoModel = chart.getAutoSize(chart.MAXMODEL);
110: boolean autoMin = chart.getAutoSize(chart.MIN);
111: chart.setAutoSizes(true, false);
112: chart.resetLBChartAreaModel(true);
113: chart.setAutoSetLayoutRatios(true);
114: chart.setSize(chart.MAX, getMaximumSize());
115: BufferedImage image = new BufferedImage(
116: getMaximumSize().width, getMaximumSize().height,
117: BufferedImage.TYPE_INT_BGR);
118: Graphics2D imageG2D = image.createGraphics();
119: prefSize = chart.getPrefSize(imageG2D);
120: chart.setAutoSizes(autoModel, autoMin);
121: } else
122: prefSize = customPrefSize;
123:
124: int prefWidth = prefSize.width < getMinimumSize().width ? getMinimumSize().width
125: : prefSize.width;
126: int prefHeight = prefSize.height < getMinimumSize().height ? getMinimumSize().height
127: : prefSize.height;
128: prefSize.setSize(prefWidth, prefHeight);
129:
130: this .size = prefSize;
131: chart.resetLBChartAreaModel(true);
132: chart.setAutoSetLayoutRatios(true);
133: chart.setSize(chart.MAX, size);
134: if (!chart.getBackgroundExistence()) {
135:
136: image = new BufferedImage(getMaximumSize().width,
137: getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
138: Graphics2D imageG2D = image.createGraphics();
139: imageG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
140: RenderingHints.VALUE_ANTIALIAS_ON);
141: chart.updateLBChartArea(imageG2D);
142: } else {
143:
144: image = new BufferedImage(size.width, size.height,
145: BufferedImage.TYPE_INT_BGR);
146: Graphics2D imageG2D = image.createGraphics();
147: imageG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
148: RenderingHints.VALUE_ANTIALIAS_ON);
149: chart.paintComponent(imageG2D);
150: Point location = chart.getSizeLocation(chart.MIN);
151: imageSize = chart.getSize(chart.MIN);
152: image = image.getSubimage(location.x, location.y,
153: imageSize.width, imageSize.height);
154: }
155:
156: needsUpdate = false;
157:
158: return prefSize;
159: }
160:
161: /**
162: * Causes the object to reinintialize to it's preferred size.
163: */
164: public final void pack() {
165:
166: needsUpdate = true;
167: setSize(getPreferredSize());
168: }
169:
170: /**
171: * Validates the properties of this object.
172: * If debug is true then prints a messages indicating whether each property is valid.
173: * Returns true if all the properties were valid and false otherwise.
174: * @param debug If true then will print status messages.
175: * @return If true then valid.
176: */
177: public final boolean validate(boolean debug) {
178:
179: if (debug)
180: System.out.println("Validating LBChart2D");
181:
182: boolean valid = true;
183:
184: if (!validateGraphChart2D(debug))
185: valid = false;
186:
187: if (debug) {
188:
189: if (valid)
190: System.out.println("LBChart2D was valid");
191: else
192: System.out.println("LBChart2D was invalid");
193: }
194:
195: return valid;
196: }
197:
198: /**
199: * Paints the chart.
200: * This is provided for the layout manager to call.
201: * @param g The graphics context for calculations and painting.
202: */
203: public final void paintComponent(Graphics g) {
204:
205: super .paintComponent(g);
206: Graphics2D g2D = (Graphics2D) g;
207:
208: updateImage(getSize());
209:
210: if (!chart.getBackgroundExistence()) {
211:
212: g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
213: RenderingHints.VALUE_ANTIALIAS_ON);
214: chart.paintComponent(g2D);
215: } else
216: g2D.drawImage(image, 0, 0, imageSize.width,
217: imageSize.height, this );
218: }
219:
220: /**
221: * Gets the LBChartArea of this LBChart2D.
222: * @return The LBChartArea of this LBChart2D.
223: */
224: final TitledArea getObjectArea() {
225: return chart;
226: }
227:
228: /**
229: * Gets the chart type, LABELS_BOTTOM.
230: * @return The type of the chart, LABELS_BOTTOM.
231: */
232: final int getGraphChartType() {
233: return LABELS_BOTTOM;
234: }
235:
236: private boolean getNeedsUpdate() {
237:
238: return (needsUpdate || size.width != getSize().width
239: || size.height != getSize().height || getNeedsUpdateGraphChart2D());
240: }
241:
242: private void updateImage(Dimension size) {
243:
244: if (prefSize == null)
245: getPreferredSize();
246:
247: if (getNeedsUpdate()) {
248:
249: updateGraphChart2D();
250:
251: this .size = size;
252: chart.setSize(chart.MAX, size);
253:
254: if (!chart.getBackgroundExistence()) {
255:
256: image = new BufferedImage(getMaximumSize().width,
257: getMaximumSize().height,
258: BufferedImage.TYPE_INT_BGR);
259: Graphics2D imageG2D = image.createGraphics();
260: imageG2D.setRenderingHint(
261: RenderingHints.KEY_ANTIALIASING,
262: RenderingHints.VALUE_ANTIALIAS_ON);
263: chart.updateLBChartArea(imageG2D);
264: } else {
265:
266: image = new BufferedImage(size.width, size.height,
267: BufferedImage.TYPE_INT_BGR);
268: Graphics2D imageG2D = image.createGraphics();
269: imageG2D.setRenderingHint(
270: RenderingHints.KEY_ANTIALIASING,
271: RenderingHints.VALUE_ANTIALIAS_ON);
272: chart.paintComponent(imageG2D);
273: Point location = chart.getSizeLocation(chart.MIN);
274: imageSize = chart.getSize(chart.MIN);
275: image = image.getSubimage(location.x, location.y,
276: imageSize.width, imageSize.height);
277: }
278:
279: needsUpdate = false;
280: }
281: }
282: }
|