KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > MeterLegend


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ----------------
23  * MeterLegend.java
24  * ----------------
25  * (C) Copyright 2000-2003, by Hari and Contributors.
26  *
27  * Original Author: Hari (ourhari@hotmail.com);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: MeterLegend.java,v 1.5 2003/10/30 09:53:06 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 01-Apr-2002 : Version 1, contributed by Hari (DG);
35  * 25-Jun-2002 : Updated imports and Javadoc comments (DG);
36  * 18-Sep-2002 : Updated for changes to StandardLegend (DG);
37  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  * 14-Jan-2003 : Changed outer gap to a Spacer (DG);
39  * 11-Feb-2003 : Removed constructor in line with changes to StandardLegend class (DG);
40  *
41  */

42
43 package org.jfree.chart;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.FontMetrics JavaDoc;
47 import java.awt.Graphics JavaDoc;
48 import java.awt.Graphics2D JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.awt.font.LineMetrics JavaDoc;
51 import java.awt.geom.Point2D JavaDoc;
52 import java.awt.geom.Rectangle2D JavaDoc;
53
54 import org.jfree.chart.event.LegendChangeEvent;
55 import org.jfree.chart.plot.MeterPlot;
56 import org.jfree.chart.plot.Plot;
57 import org.jfree.data.MeterDataset;
58 import org.jfree.data.ValueDataset;
59
60 /**
61  * A legend for meter plots.
62  *
63  * @author Hari
64  */

65 public class MeterLegend extends StandardLegend {
66
67     /** The legend text. */
68     private String JavaDoc legendText;
69
70     /** Show the normal range? */
71     private boolean showNormal = true;
72
73     /** Show the warning range? */
74     private boolean showWarning = true;
75
76     /** Show the critical range? */
77     private boolean showCritical = true;
78
79
80     /**
81      * Constructs a new legend.
82      *
83      * @param chart the chart.
84      * @param legendText the legend text.
85      *
86      */

87     public MeterLegend(JFreeChart chart, String JavaDoc legendText) {
88
89         super(chart);
90         this.legendText = legendText;
91
92     }
93
94     /**
95      * Returns the legend text.
96      *
97      * @return the legend text.
98      */

99     public String JavaDoc getLegendText() {
100         return this.legendText;
101     }
102
103     /**
104      * Sets the legend text.
105      *
106      * @param text the new legend text.
107      */

108     public void setLegendText(String JavaDoc text) {
109         this.legendText = text;
110         notifyListeners(new LegendChangeEvent(this));
111     }
112
113     /**
114      * Draws the legend.
115      *
116      * @param g2 the graphics device.
117      * @param available the available area.
118      *
119      * @return the remaining available drawing area.
120      */

121     public Rectangle2D JavaDoc draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc available) {
122
123         return draw(g2, available, (getAnchor() & HORIZONTAL) != 0, (getAnchor() & INVERTED) != 0);
124
125     }
126
127     /**
128      * Updates the legend information.
129      *
130      * @param plot the plot.
131      * @param data the dataset.
132      * @param type the type.
133      * @param index the index.
134      * @param legendItems the legend items.
135      * @param legendItemColors the colors.
136      *
137      * @return boolean.
138      */

139     private boolean updateInformation(MeterPlot plot, ValueDataset data, int type, int index,
140                                       LegendItem[] legendItems, Paint JavaDoc[] legendItemColors) {
141
142         boolean ret = false;
143         String JavaDoc label = null;
144 // double minValue = 0.0;
145
// double maxValue = 0.0;
146
Paint JavaDoc paint = null;
147
148         switch(type) {
149             case MeterDataset.NORMAL_DATA:
150 // minValue = plot.getNormalRange().getLowerBound();
151
// maxValue = plot.getNormalRange().getUpperBound();
152
paint = plot.getNormalPaint();
153                 label = MeterPlot.NORMAL_TEXT;
154                 break;
155             case MeterDataset.WARNING_DATA:
156 // minValue = plot.getWarningRange().getLowerBound();
157
// maxValue = plot.getWarningRange().getUpperBound();
158
paint = plot.getWarningPaint();
159                 label = MeterPlot.WARNING_TEXT;
160                 break;
161             case MeterDataset.CRITICAL_DATA:
162 // minValue = plot.getCriticalRange().getLowerBound();
163
// maxValue = plot.getCriticalRange().getUpperBound();
164
paint = plot.getCriticalPaint();
165                 label = MeterPlot.CRITICAL_TEXT;
166                 break;
167             case MeterDataset.FULL_DATA:
168 // minValue = plot.getRange().getLowerBound();
169
// maxValue = plot.getRange().getUpperBound();
170
paint = MeterPlot.DEFAULT_BACKGROUND_PAINT;
171                 label = "Meter Graph";
172                 break;
173             default:
174                 return false;
175         }
176
177 // if (minValue != null && maxValue != null) {
178
// if (data.getBorderType() == type) {
179
label += " Range: ";
180 // + data.getMinimumValue().toString() + " to "
181
// + minValue.toString()
182
// + " and "
183
// + maxValue.toString() + " to "
184
// + data.getMaximumValue().toString();
185
// }
186
// else {
187
// label += " Range: " + minValue.toString() + " to " + maxValue.toString();
188
// }
189
legendItems[index] = new LegendItem(label, label, null, null, null, null);
190             legendItemColors[index] = paint;
191             ret = true;
192         //}
193
return ret;
194     }
195
196     /**
197      * Draws the legend.
198      *
199      * @param g2 the graphics device.
200      * @param available the available drawing area.
201      * @param horizontal if <code>true</code> draw a horizontal legend.
202      * @param inverted ???
203      *
204      * @return the remaining available drawing area.
205      *
206      */

207     protected Rectangle2D JavaDoc draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc available,
208                                boolean horizontal, boolean inverted) {
209
210         int legendCount = 0;
211         Plot plot = getChart().getPlot();
212         if (!(plot instanceof MeterPlot)) {
213             throw new IllegalArgumentException JavaDoc("Plot must be MeterPlot");
214         }
215         MeterPlot meterPlot = (MeterPlot) plot;
216         ValueDataset data = meterPlot.getDataset();
217
218         legendCount = 1; // Name of the Chart.
219
legendCount++; // Display Full Range
220
if (showCritical) {
221             legendCount++;
222         }
223         if (showWarning) {
224             legendCount++;
225         }
226         if (showNormal) {
227             legendCount++;
228         }
229
230         LegendItem[] legendItems = new LegendItem[legendCount];
231         Color JavaDoc[] legendItemColors = new Color JavaDoc[legendCount];
232
233         int currentItem = 0;
234         String JavaDoc label = this.legendText
235             + (data.getValue() != null ? (" Current Value: " + data.getValue().toString()) : "");
236         legendItems[currentItem] = new LegendItem(label, label, null, null, null, null);
237         legendItemColors[currentItem] = null; // no color
238
currentItem++;
239         if (updateInformation(meterPlot, data, MeterDataset.FULL_DATA,
240             currentItem, legendItems, legendItemColors)) {
241             currentItem++;
242         }
243         if (showCritical && updateInformation(meterPlot, data,
244             MeterDataset.CRITICAL_DATA, currentItem, legendItems, legendItemColors)) {
245             currentItem++;
246         }
247         if (showWarning && updateInformation(meterPlot, data,
248             MeterDataset.WARNING_DATA, currentItem, legendItems, legendItemColors)) {
249             currentItem++;
250         }
251         if (showNormal && updateInformation(meterPlot, data,
252             MeterDataset.NORMAL_DATA, currentItem, legendItems, legendItemColors)) {
253             currentItem++;
254         }
255
256         if (legendItems != null) {
257
258             Rectangle2D JavaDoc legendArea = new Rectangle2D.Double JavaDoc();
259             double availableWidth = available.getWidth();
260             double availableHeight = available.getHeight();
261
262             // the translation point for the origin of the drawing system
263
Point2D JavaDoc translation = new Point2D.Double JavaDoc();
264
265             // Create buffer for individual rectangles within the legend
266
DrawableLegendItem[] items = new DrawableLegendItem[legendItems.length];
267             g2.setFont(getItemFont());
268
269             // Compute individual rectangles in the legend, translation point
270
// as well as the bounding box for the legend.
271
if (horizontal) {
272                 double xstart = available.getX() + getOuterGap().getLeftSpace(availableWidth);
273                 double xlimit = available.getMaxX()
274                                 + getOuterGap().getRightSpace(availableWidth) - 1;
275                 double maxRowWidth = 0;
276                 double xoffset = 0;
277                 double rowHeight = 0;
278                 double totalHeight = 0;
279                 boolean startingNewRow = true;
280
281                 for (int i = 0; i < legendItems.length; i++) {
282                     items[i] = createLegendItem(g2, legendItems[i], xoffset, totalHeight);
283                     if ((!startingNewRow)
284                         && (items[i].getX() + items[i].getWidth() + xstart > xlimit)) {
285                         maxRowWidth = Math.max(maxRowWidth, xoffset);
286                         xoffset = 0;
287                         totalHeight += rowHeight;
288                         i--;
289                         startingNewRow = true;
290                     }
291                     else {
292                         rowHeight = Math.max(rowHeight, items[i].getHeight());
293                         xoffset += items[i].getWidth();
294                         startingNewRow = false;
295                     }
296                 }
297
298                 maxRowWidth = Math.max(maxRowWidth, xoffset);
299                 totalHeight += rowHeight;
300
301                 // Create the bounding box
302
legendArea = new Rectangle2D.Double JavaDoc(0, 0, maxRowWidth, totalHeight);
303
304                 // The yloc point is the variable part of the translation point
305
// for horizontal legends. xloc is constant.
306
double yloc = (inverted)
307                     ? available.getMaxY() - totalHeight
308                                           - getOuterGap().getBottomSpace(availableHeight)
309                     : available.getY() + getOuterGap().getTopSpace(availableHeight);
310                 double xloc = available.getX() + available.getWidth() / 2 - maxRowWidth / 2;
311
312                 // Create the translation point
313
translation = new Point2D.Double JavaDoc(xloc, yloc);
314             }
315             else { // vertical...
316
double totalHeight = 0;
317                 double maxWidth = 0;
318                 g2.setFont(getItemFont());
319                 for (int i = 0; i < items.length; i++) {
320                     items[i] = createLegendItem(g2, legendItems[i], 0, totalHeight);
321                     totalHeight += items[i].getHeight();
322                     maxWidth = Math.max(maxWidth, items[i].getWidth());
323                 }
324
325                 // Create the bounding box
326
legendArea = new Rectangle2D.Float JavaDoc(0, 0, (float) maxWidth, (float) totalHeight);
327
328                 // The xloc point is the variable part of the translation point
329
// for vertical legends. yloc is constant.
330
double xloc = (inverted)
331                     ? available.getMaxX() - maxWidth - getOuterGap().getRightSpace(availableWidth)
332                     : available.getX() + getOuterGap().getLeftSpace(availableWidth);
333                 double yloc = available.getY() + (available.getHeight() / 2) - (totalHeight / 2);
334
335                 // Create the translation point
336
translation = new Point2D.Double JavaDoc(xloc, yloc);
337             }
338
339             // Move the origin of the drawing to the appropriate location
340
g2.translate(translation.getX(), translation.getY());
341
342             // Draw the legend's bounding box
343
g2.setPaint(getBackgroundPaint());
344             g2.fill(legendArea);
345             g2.setPaint(getOutlinePaint());
346             g2.setStroke(getOutlineStroke());
347             g2.draw(legendArea);
348
349             // Draw individual series elements
350
for (int i = 0; i < items.length; i++) {
351                 Color JavaDoc color = legendItemColors[i];
352                 if (color != null) {
353                     g2.setPaint(color);
354                     g2.fill(items[i].getMarker());
355                 }
356                 g2.setPaint(getItemPaint());
357                 g2.drawString(items[i].getItem().getLabel(),
358                               (float) items[i].getLabelPosition().getX(),
359                               (float) items[i].getLabelPosition().getY());
360             }
361
362             // translate the origin back to what it was prior to drawing the
363
// legend
364
g2.translate(-translation.getX(), -translation.getY());
365
366             if (horizontal) {
367                 // The remaining drawing area bounding box will have the same
368
// x origin, width and height independent of the anchor's
369
// location. The variable is the y coordinate. If the anchor is
370
// SOUTH, the y coordinate is simply the original y coordinate
371
// of the available area. If it is NORTH, we adjust original y
372
// by the total height of the legend and the initial gap.
373
double yy = available.getY();
374                 double yloc = (inverted) ? yy
375                                          : yy + legendArea.getHeight()
376                                               + getOuterGap().getBottomSpace(availableHeight);
377
378                 // return the remaining available drawing area
379
return new Rectangle2D.Double JavaDoc(available.getX(), yloc, availableWidth,
380                     availableHeight - legendArea.getHeight()
381                                     - getOuterGap().getTopSpace(availableHeight)
382                                     - getOuterGap().getBottomSpace(availableHeight));
383             }
384             else {
385                 // The remaining drawing area bounding box will have the same
386
// y origin, width and height independent of the anchor's
387
// location. The variable is the x coordinate. If the anchor is
388
// EAST, the x coordinate is simply the original x coordinate
389
// of the available area. If it is WEST, we adjust original x
390
// by the total width of the legend and the initial gap.
391
double xloc = (inverted) ? available.getX()
392                                          : available.getX()
393                                            + legendArea.getWidth()
394                                            + getOuterGap().getLeftSpace(availableWidth)
395                                            + getOuterGap().getRightSpace(availableWidth);
396
397
398                 // return the remaining available drawing area
399
return new Rectangle2D.Double JavaDoc(xloc, available.getY(),
400                     availableWidth - legendArea.getWidth()
401                                    - getOuterGap().getLeftSpace(availableWidth)
402                                    - getOuterGap().getRightSpace(availableWidth),
403                     availableHeight);
404             }
405         }
406         else {
407             return available;
408         }
409     }
410
411     /**
412      * Creates a legend item
413      *
414      * @param graphics the graphics device.
415      * @param item the legend item.
416      * @param x the x coordinate.
417      * @param y the y coordinate.
418      *
419      * @return the legend item.
420      */

421     private DrawableLegendItem createLegendItem(Graphics JavaDoc graphics,
422                                                 LegendItem item, double x, double y) {
423
424         int innerGap = 2;
425         FontMetrics JavaDoc fm = graphics.getFontMetrics();
426         LineMetrics JavaDoc lm = fm.getLineMetrics(item.getLabel(), graphics);
427         float textHeight = lm.getHeight();
428
429         DrawableLegendItem drawable = new DrawableLegendItem(item);
430
431         float xloc = (float) (x + innerGap + 1.15f * textHeight);
432         float yloc = (float) (y + innerGap + (textHeight - lm.getLeading() - lm.getDescent()));
433
434         drawable.setLabelPosition(new Point2D.Float JavaDoc(xloc, yloc));
435
436         float boxDim = textHeight * 0.70f;
437         xloc = (float) (x + innerGap + 0.15f * textHeight);
438         yloc = (float) (y + innerGap + 0.15f * textHeight);
439
440         drawable.setMarker(new Rectangle2D.Float JavaDoc(xloc, yloc, boxDim, boxDim));
441
442         float width = (float) (drawable.getLabelPosition().getX() - x
443                                + fm.getStringBounds(item.getLabel(), graphics).getWidth()
444                                + 0.5 * textHeight);
445
446         float height = 2 * innerGap + textHeight;
447         drawable.setBounds(x, y, width, height);
448         return drawable;
449
450     }
451
452 }
453
Popular Tags