001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------------
028: * CombinedDomainXYPlot.java
029: * -------------------------
030: * (C) Copyright 2001-2007, by Bill Kelemen and Contributors.
031: *
032: * Original Author: Bill Kelemen;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: * Anthony Boulestreau;
035: * David Basten;
036: * Kevin Frechette (for ISTI);
037: * Nicolas Brodu;
038: * Petr Kubanek (bug 1606205);
039: *
040: * $Id: CombinedDomainXYPlot.java,v 1.9.2.6 2007/04/17 11:13:25 mungady Exp $
041: *
042: * Changes:
043: * --------
044: * 06-Dec-2001 : Version 1 (BK);
045: * 12-Dec-2001 : Removed unnecessary 'throws' clause from constructor (DG);
046: * 18-Dec-2001 : Added plotArea attribute and get/set methods (BK);
047: * 22-Dec-2001 : Fixed bug in chartChanged with multiple combinations of
048: * CombinedPlots (BK);
049: * 08-Jan-2002 : Moved to new package com.jrefinery.chart.combination (DG);
050: * 25-Feb-2002 : Updated import statements (DG);
051: * 28-Feb-2002 : Readded "this.plotArea = plotArea" that was deleted from
052: * draw() method (BK);
053: * 26-Mar-2002 : Added an empty zoom method (this method needs to be written so
054: * that combined plots will support zooming (DG);
055: * 29-Mar-2002 : Changed the method createCombinedAxis adding the creation of
056: * OverlaidSymbolicAxis and CombinedSymbolicAxis(AB);
057: * 23-Apr-2002 : Renamed CombinedPlot-->MultiXYPlot, and simplified the
058: * structure (DG);
059: * 23-May-2002 : Renamed (again) MultiXYPlot-->CombinedXYPlot (DG);
060: * 19-Jun-2002 : Added get/setGap() methods suggested by David Basten (DG);
061: * 25-Jun-2002 : Removed redundant imports (DG);
062: * 16-Jul-2002 : Draws shared axis after subplots (to fix missing gridlines),
063: * added overrides of 'setSeriesPaint()' and 'setXYItemRenderer()'
064: * that pass changes down to subplots (KF);
065: * 09-Oct-2002 : Added add(XYPlot) method (DG);
066: * 26-Mar-2003 : Implemented Serializable (DG);
067: * 16-May-2003 : Renamed CombinedXYPlot --> CombinedDomainXYPlot (DG);
068: * 04-Aug-2003 : Removed leftover code that was causing domain axis drawing
069: * problem (DG);
070: * 08-Aug-2003 : Adjusted totalWeight in remove() method (DG);
071: * 21-Aug-2003 : Implemented Cloneable (DG);
072: * 11-Sep-2003 : Fix cloning support (subplots) (NB);
073: * 15-Sep-2003 : Fixed error in cloning (DG);
074: * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
075: * 17-Sep-2003 : Updated handling of 'clicks' (DG);
076: * 12-Nov-2004 : Implemented the new Zoomable interface (DG);
077: * 25-Nov-2004 : Small update to clone() implementation (DG);
078: * 21-Feb-2005 : The getLegendItems() method now returns the fixed legend
079: * items if set (DG);
080: * 05-May-2005 : Removed unused draw() method (DG);
081: * ------------- JFREECHART 1.0.x ---------------------------------------------
082: * 23-Aug-2006 : Override setFixedRangeAxisSpace() to update subplots (DG);
083: * 06-Feb-2007 : Fixed bug 1606205, draw shared axis after subplots (DG);
084: * 23-Mar-2007 : Reverted previous patch (bug fix 1606205) (DG);
085: * 17-Apr-2007 : Added null argument checks to findSubplot() (DG);
086: *
087: */
088:
089: package org.jfree.chart.plot;
090:
091: import java.awt.Graphics2D;
092: import java.awt.geom.Point2D;
093: import java.awt.geom.Rectangle2D;
094: import java.io.Serializable;
095: import java.util.Collections;
096: import java.util.Iterator;
097: import java.util.List;
098:
099: import org.jfree.chart.LegendItemCollection;
100: import org.jfree.chart.axis.AxisSpace;
101: import org.jfree.chart.axis.AxisState;
102: import org.jfree.chart.axis.NumberAxis;
103: import org.jfree.chart.axis.ValueAxis;
104: import org.jfree.chart.event.PlotChangeEvent;
105: import org.jfree.chart.event.PlotChangeListener;
106: import org.jfree.chart.renderer.xy.XYItemRenderer;
107: import org.jfree.data.Range;
108: import org.jfree.ui.RectangleEdge;
109: import org.jfree.ui.RectangleInsets;
110: import org.jfree.util.ObjectUtilities;
111: import org.jfree.util.PublicCloneable;
112:
113: /**
114: * An extension of {@link XYPlot} that contains multiple subplots that share a
115: * common domain axis.
116: */
117: public class CombinedDomainXYPlot extends XYPlot implements Cloneable,
118: PublicCloneable, Serializable, PlotChangeListener {
119:
120: /** For serialization. */
121: private static final long serialVersionUID = -7765545541261907383L;
122:
123: /** Storage for the subplot references. */
124: private List subplots;
125:
126: /** Total weight of all charts. */
127: private int totalWeight = 0;
128:
129: /** The gap between subplots. */
130: private double gap = 5.0;
131:
132: /** Temporary storage for the subplot areas. */
133: private transient Rectangle2D[] subplotAreas;
134:
135: // TODO: the subplot areas needs to be moved out of the plot into the plot
136: // state
137:
138: /**
139: * Default constructor.
140: */
141: public CombinedDomainXYPlot() {
142: this (new NumberAxis());
143: }
144:
145: /**
146: * Creates a new combined plot that shares a domain axis among multiple
147: * subplots.
148: *
149: * @param domainAxis the shared axis.
150: */
151: public CombinedDomainXYPlot(ValueAxis domainAxis) {
152:
153: super (null, // no data in the parent plot
154: domainAxis, null, // no range axis
155: null // no rendereer
156: );
157:
158: this .subplots = new java.util.ArrayList();
159:
160: }
161:
162: /**
163: * Returns a string describing the type of plot.
164: *
165: * @return The type of plot.
166: */
167: public String getPlotType() {
168: return "Combined_Domain_XYPlot";
169: }
170:
171: /**
172: * Sets the orientation for the plot (also changes the orientation for all
173: * the subplots to match).
174: *
175: * @param orientation the orientation (<code>null</code> not allowed).
176: */
177: public void setOrientation(PlotOrientation orientation) {
178:
179: super .setOrientation(orientation);
180: Iterator iterator = this .subplots.iterator();
181: while (iterator.hasNext()) {
182: XYPlot plot = (XYPlot) iterator.next();
183: plot.setOrientation(orientation);
184: }
185:
186: }
187:
188: /**
189: * Returns the range for the specified axis. This is the combined range
190: * of all the subplots.
191: *
192: * @param axis the axis.
193: *
194: * @return The range (possibly <code>null</code>).
195: */
196: public Range getDataRange(ValueAxis axis) {
197:
198: Range result = null;
199: if (this .subplots != null) {
200: Iterator iterator = this .subplots.iterator();
201: while (iterator.hasNext()) {
202: XYPlot subplot = (XYPlot) iterator.next();
203: result = Range.combine(result, subplot
204: .getDataRange(axis));
205: }
206: }
207: return result;
208:
209: }
210:
211: /**
212: * Returns the gap between subplots, measured in Java2D units.
213: *
214: * @return The gap (in Java2D units).
215: */
216: public double getGap() {
217: return this .gap;
218: }
219:
220: /**
221: * Sets the amount of space between subplots and sends a
222: * {@link PlotChangeEvent} to all registered listeners.
223: *
224: * @param gap the gap between subplots (in Java2D units).
225: */
226: public void setGap(double gap) {
227: this .gap = gap;
228: notifyListeners(new PlotChangeEvent(this ));
229: }
230:
231: /**
232: * Adds a subplot (with a default 'weight' of 1) and sends a
233: * {@link PlotChangeEvent} to all registered listeners.
234: * <P>
235: * The domain axis for the subplot will be set to <code>null</code>. You
236: * must ensure that the subplot has a non-null range axis.
237: *
238: * @param subplot the subplot (<code>null</code> not permitted).
239: */
240: public void add(XYPlot subplot) {
241: // defer argument checking
242: add(subplot, 1);
243: }
244:
245: /**
246: * Adds a subplot with the specified weight and sends a
247: * {@link PlotChangeEvent} to all registered listeners. The weight
248: * determines how much space is allocated to the subplot relative to all
249: * the other subplots.
250: * <P>
251: * The domain axis for the subplot will be set to <code>null</code>. You
252: * must ensure that the subplot has a non-null range axis.
253: *
254: * @param subplot the subplot (<code>null</code> not permitted).
255: * @param weight the weight (must be >= 1).
256: */
257: public void add(XYPlot subplot, int weight) {
258:
259: if (subplot == null) {
260: throw new IllegalArgumentException(
261: "Null 'subplot' argument.");
262: }
263: if (weight <= 0) {
264: throw new IllegalArgumentException("Require weight >= 1.");
265: }
266:
267: // store the plot and its weight
268: subplot.setParent(this );
269: subplot.setWeight(weight);
270: subplot.setInsets(new RectangleInsets(0.0, 0.0, 0.0, 0.0),
271: false);
272: subplot.setDomainAxis(null);
273: subplot.addChangeListener(this );
274: this .subplots.add(subplot);
275:
276: // keep track of total weights
277: this .totalWeight += weight;
278:
279: ValueAxis axis = getDomainAxis();
280: if (axis != null) {
281: axis.configure();
282: }
283:
284: notifyListeners(new PlotChangeEvent(this ));
285:
286: }
287:
288: /**
289: * Removes a subplot from the combined chart and sends a
290: * {@link PlotChangeEvent} to all registered listeners.
291: *
292: * @param subplot the subplot (<code>null</code> not permitted).
293: */
294: public void remove(XYPlot subplot) {
295: if (subplot == null) {
296: throw new IllegalArgumentException(
297: " Null 'subplot' argument.");
298: }
299: int position = -1;
300: int size = this .subplots.size();
301: int i = 0;
302: while (position == -1 && i < size) {
303: if (this .subplots.get(i) == subplot) {
304: position = i;
305: }
306: i++;
307: }
308: if (position != -1) {
309: this .subplots.remove(position);
310: subplot.setParent(null);
311: subplot.removeChangeListener(this );
312: this .totalWeight -= subplot.getWeight();
313:
314: ValueAxis domain = getDomainAxis();
315: if (domain != null) {
316: domain.configure();
317: }
318: notifyListeners(new PlotChangeEvent(this ));
319: }
320: }
321:
322: /**
323: * Returns the list of subplots.
324: *
325: * @return An unmodifiable list of subplots.
326: */
327: public List getSubplots() {
328: return Collections.unmodifiableList(this .subplots);
329: }
330:
331: /**
332: * Calculates the axis space required.
333: *
334: * @param g2 the graphics device.
335: * @param plotArea the plot area.
336: *
337: * @return The space.
338: */
339: protected AxisSpace calculateAxisSpace(Graphics2D g2,
340: Rectangle2D plotArea) {
341:
342: AxisSpace space = new AxisSpace();
343: PlotOrientation orientation = getOrientation();
344:
345: // work out the space required by the domain axis...
346: AxisSpace fixed = getFixedDomainAxisSpace();
347: if (fixed != null) {
348: if (orientation == PlotOrientation.HORIZONTAL) {
349: space.setLeft(fixed.getLeft());
350: space.setRight(fixed.getRight());
351: } else if (orientation == PlotOrientation.VERTICAL) {
352: space.setTop(fixed.getTop());
353: space.setBottom(fixed.getBottom());
354: }
355: } else {
356: ValueAxis xAxis = getDomainAxis();
357: RectangleEdge xEdge = Plot.resolveDomainAxisLocation(
358: getDomainAxisLocation(), orientation);
359: if (xAxis != null) {
360: space = xAxis.reserveSpace(g2, this , plotArea, xEdge,
361: space);
362: }
363: }
364:
365: Rectangle2D adjustedPlotArea = space.shrink(plotArea, null);
366:
367: // work out the maximum height or width of the non-shared axes...
368: int n = this .subplots.size();
369: this .subplotAreas = new Rectangle2D[n];
370: double x = adjustedPlotArea.getX();
371: double y = adjustedPlotArea.getY();
372: double usableSize = 0.0;
373: if (orientation == PlotOrientation.HORIZONTAL) {
374: usableSize = adjustedPlotArea.getWidth() - this .gap
375: * (n - 1);
376: } else if (orientation == PlotOrientation.VERTICAL) {
377: usableSize = adjustedPlotArea.getHeight() - this .gap
378: * (n - 1);
379: }
380:
381: for (int i = 0; i < n; i++) {
382: XYPlot plot = (XYPlot) this .subplots.get(i);
383:
384: // calculate sub-plot area
385: if (orientation == PlotOrientation.HORIZONTAL) {
386: double w = usableSize * plot.getWeight()
387: / this .totalWeight;
388: this .subplotAreas[i] = new Rectangle2D.Double(x, y, w,
389: adjustedPlotArea.getHeight());
390: x = x + w + this .gap;
391: } else if (orientation == PlotOrientation.VERTICAL) {
392: double h = usableSize * plot.getWeight()
393: / this .totalWeight;
394: this .subplotAreas[i] = new Rectangle2D.Double(x, y,
395: adjustedPlotArea.getWidth(), h);
396: y = y + h + this .gap;
397: }
398:
399: AxisSpace subSpace = plot.calculateRangeAxisSpace(g2,
400: this .subplotAreas[i], null);
401: space.ensureAtLeast(subSpace);
402:
403: }
404:
405: return space;
406: }
407:
408: /**
409: * Draws the plot within the specified area on a graphics device.
410: *
411: * @param g2 the graphics device.
412: * @param area the plot area (in Java2D space).
413: * @param anchor an anchor point in Java2D space (<code>null</code>
414: * permitted).
415: * @param parentState the state from the parent plot, if there is one
416: * (<code>null</code> permitted).
417: * @param info collects chart drawing information (<code>null</code>
418: * permitted).
419: */
420: public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,
421: PlotState parentState, PlotRenderingInfo info) {
422:
423: // set up info collection...
424: if (info != null) {
425: info.setPlotArea(area);
426: }
427:
428: // adjust the drawing area for plot insets (if any)...
429: RectangleInsets insets = getInsets();
430: insets.trim(area);
431:
432: AxisSpace space = calculateAxisSpace(g2, area);
433: Rectangle2D dataArea = space.shrink(area, null);
434:
435: // set the width and height of non-shared axis of all sub-plots
436: setFixedRangeAxisSpaceForSubplots(space);
437:
438: // draw the shared axis
439: ValueAxis axis = getDomainAxis();
440: RectangleEdge edge = getDomainAxisEdge();
441: double cursor = RectangleEdge.coordinate(dataArea, edge);
442: AxisState axisState = axis.draw(g2, cursor, area, dataArea,
443: edge, info);
444: if (parentState == null) {
445: parentState = new PlotState();
446: }
447: parentState.getSharedAxisStates().put(axis, axisState);
448:
449: // draw all the subplots
450: for (int i = 0; i < this .subplots.size(); i++) {
451: XYPlot plot = (XYPlot) this .subplots.get(i);
452: PlotRenderingInfo subplotInfo = null;
453: if (info != null) {
454: subplotInfo = new PlotRenderingInfo(info.getOwner());
455: info.addSubplotInfo(subplotInfo);
456: }
457: plot.draw(g2, this .subplotAreas[i], anchor, parentState,
458: subplotInfo);
459: }
460:
461: if (info != null) {
462: info.setDataArea(dataArea);
463: }
464:
465: }
466:
467: /**
468: * Returns a collection of legend items for the plot.
469: *
470: * @return The legend items.
471: */
472: public LegendItemCollection getLegendItems() {
473: LegendItemCollection result = getFixedLegendItems();
474: if (result == null) {
475: result = new LegendItemCollection();
476: if (this .subplots != null) {
477: Iterator iterator = this .subplots.iterator();
478: while (iterator.hasNext()) {
479: XYPlot plot = (XYPlot) iterator.next();
480: LegendItemCollection more = plot.getLegendItems();
481: result.addAll(more);
482: }
483: }
484: }
485: return result;
486: }
487:
488: /**
489: * Multiplies the range on the range axis/axes by the specified factor.
490: *
491: * @param factor the zoom factor.
492: * @param info the plot rendering info (<code>null</code> not permitted).
493: * @param source the source point (<code>null</code> not permitted).
494: */
495: public void zoomRangeAxes(double factor, PlotRenderingInfo info,
496: Point2D source) {
497: // delegate 'info' and 'source' argument checks...
498: XYPlot subplot = findSubplot(info, source);
499: if (subplot != null) {
500: subplot.zoomRangeAxes(factor, info, source);
501: } else {
502: // if the source point doesn't fall within a subplot, we do the
503: // zoom on all subplots...
504: Iterator iterator = getSubplots().iterator();
505: while (iterator.hasNext()) {
506: subplot = (XYPlot) iterator.next();
507: subplot.zoomRangeAxes(factor, info, source);
508: }
509: }
510: }
511:
512: /**
513: * Zooms in on the range axes.
514: *
515: * @param lowerPercent the lower bound.
516: * @param upperPercent the upper bound.
517: * @param info the plot rendering info (<code>null</code> not permitted).
518: * @param source the source point (<code>null</code> not permitted).
519: */
520: public void zoomRangeAxes(double lowerPercent, double upperPercent,
521: PlotRenderingInfo info, Point2D source) {
522: // delegate 'info' and 'source' argument checks...
523: XYPlot subplot = findSubplot(info, source);
524: if (subplot != null) {
525: subplot.zoomRangeAxes(lowerPercent, upperPercent, info,
526: source);
527: } else {
528: // if the source point doesn't fall within a subplot, we do the
529: // zoom on all subplots...
530: Iterator iterator = getSubplots().iterator();
531: while (iterator.hasNext()) {
532: subplot = (XYPlot) iterator.next();
533: subplot.zoomRangeAxes(lowerPercent, upperPercent, info,
534: source);
535: }
536: }
537: }
538:
539: /**
540: * Returns the subplot (if any) that contains the (x, y) point (specified
541: * in Java2D space).
542: *
543: * @param info the chart rendering info (<code>null</code> not permitted).
544: * @param source the source point (<code>null</code> not permitted).
545: *
546: * @return A subplot (possibly <code>null</code>).
547: */
548: public XYPlot findSubplot(PlotRenderingInfo info, Point2D source) {
549: if (info == null) {
550: throw new IllegalArgumentException("Null 'info' argument.");
551: }
552: if (source == null) {
553: throw new IllegalArgumentException(
554: "Null 'source' argument.");
555: }
556: XYPlot result = null;
557: int subplotIndex = info.getSubplotIndex(source);
558: if (subplotIndex >= 0) {
559: result = (XYPlot) this .subplots.get(subplotIndex);
560: }
561: return result;
562: }
563:
564: /**
565: * Sets the item renderer FOR ALL SUBPLOTS. Registered listeners are
566: * notified that the plot has been modified.
567: * <P>
568: * Note: usually you will want to set the renderer independently for each
569: * subplot, which is NOT what this method does.
570: *
571: * @param renderer the new renderer.
572: */
573: public void setRenderer(XYItemRenderer renderer) {
574:
575: super .setRenderer(renderer); // not strictly necessary, since the
576: // renderer set for the
577: // parent plot is not used
578:
579: Iterator iterator = this .subplots.iterator();
580: while (iterator.hasNext()) {
581: XYPlot plot = (XYPlot) iterator.next();
582: plot.setRenderer(renderer);
583: }
584:
585: }
586:
587: /**
588: * Sets the fixed range axis space.
589: *
590: * @param space the space (<code>null</code> permitted).
591: */
592: public void setFixedRangeAxisSpace(AxisSpace space) {
593: super .setFixedRangeAxisSpace(space);
594: setFixedRangeAxisSpaceForSubplots(space);
595: this .notifyListeners(new PlotChangeEvent(this ));
596: }
597:
598: /**
599: * Sets the size (width or height, depending on the orientation of the
600: * plot) for the domain axis of each subplot.
601: *
602: * @param space the space.
603: */
604: protected void setFixedRangeAxisSpaceForSubplots(AxisSpace space) {
605:
606: Iterator iterator = this .subplots.iterator();
607: while (iterator.hasNext()) {
608: XYPlot plot = (XYPlot) iterator.next();
609: plot.setFixedRangeAxisSpace(space);
610: }
611:
612: }
613:
614: /**
615: * Handles a 'click' on the plot by updating the anchor values.
616: *
617: * @param x x-coordinate, where the click occured.
618: * @param y y-coordinate, where the click occured.
619: * @param info object containing information about the plot dimensions.
620: */
621: public void handleClick(int x, int y, PlotRenderingInfo info) {
622: Rectangle2D dataArea = info.getDataArea();
623: if (dataArea.contains(x, y)) {
624: for (int i = 0; i < this .subplots.size(); i++) {
625: XYPlot subplot = (XYPlot) this .subplots.get(i);
626: PlotRenderingInfo subplotInfo = info.getSubplotInfo(i);
627: subplot.handleClick(x, y, subplotInfo);
628: }
629: }
630: }
631:
632: /**
633: * Receives a {@link PlotChangeEvent} and responds by notifying all
634: * listeners.
635: *
636: * @param event the event.
637: */
638: public void plotChanged(PlotChangeEvent event) {
639: notifyListeners(event);
640: }
641:
642: /**
643: * Tests this plot for equality with another object.
644: *
645: * @param obj the other object.
646: *
647: * @return <code>true</code> or <code>false</code>.
648: */
649: public boolean equals(Object obj) {
650:
651: if (obj == null) {
652: return false;
653: }
654:
655: if (obj == this ) {
656: return true;
657: }
658:
659: if (!(obj instanceof CombinedDomainXYPlot)) {
660: return false;
661: }
662: if (!super .equals(obj)) {
663: return false;
664: }
665:
666: CombinedDomainXYPlot p = (CombinedDomainXYPlot) obj;
667: if (this .totalWeight != p.totalWeight) {
668: return false;
669: }
670: if (this .gap != p.gap) {
671: return false;
672: }
673: if (!ObjectUtilities.equal(this .subplots, p.subplots)) {
674: return false;
675: }
676:
677: return true;
678: }
679:
680: /**
681: * Returns a clone of the annotation.
682: *
683: * @return A clone.
684: *
685: * @throws CloneNotSupportedException this class will not throw this
686: * exception, but subclasses (if any) might.
687: */
688: public Object clone() throws CloneNotSupportedException {
689:
690: CombinedDomainXYPlot result = (CombinedDomainXYPlot) super
691: .clone();
692: result.subplots = (List) ObjectUtilities
693: .deepClone(this .subplots);
694: for (Iterator it = result.subplots.iterator(); it.hasNext();) {
695: Plot child = (Plot) it.next();
696: child.setParent(result);
697: }
698:
699: // after setting up all the subplots, the shared domain axis may need
700: // reconfiguring
701: ValueAxis domainAxis = result.getDomainAxis();
702: if (domainAxis != null) {
703: domainAxis.configure();
704: }
705:
706: return result;
707:
708: }
709:
710: }
|