KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chart2d > PieChart2D


1 /**
2  * Chart2D, a java library for drawing two dimensional charts.
3  * Copyright (C) 2001 Jason J. Simas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * The author of this library may be contacted at:
19  * E-mail: jjsimas@users.sourceforge.net
20  * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
21  */

22
23
24 package net.sourceforge.chart2d;
25
26
27 import java.awt.*;
28 import java.awt.image.*;
29
30
31 /**
32  * A PieChart2D object is an enclosed are with a title, pie sectors, pie labels, and a legend.
33  * Changes through its set methods are updated upon next repaint() or getImage() calls.
34  */

35 public final class PieChart2D extends Chart2D {
36
37
38   private boolean needsUpdate;
39   private PieChartArea chart;
40   private BufferedImage image;
41   private Dimension size;
42   private Dimension imageSize;
43   private Dimension prefSize;
44   private boolean customizePrefSize;
45   private Dimension customPrefSize;
46   private PieChart2DProperties pieChart2DProps;
47   private MultiColorsProperties multiColorsProps;
48   private Dataset dataset;
49
50
51   /**
52    * Creates a PieChart2D object with its defaults.
53    */

54   public PieChart2D() {
55
56     needsUpdate = true;
57     chart = new PieChartArea();
58     size = new Dimension();
59     imageSize = new Dimension();
60     prefSize = null;
61     customizePrefSize = false;
62     customPrefSize = null;
63   }
64
65
66   /**
67    * Sets the PieChart2DProperties for this PieChart2D.
68    * @param props The PieChart2DProperties.
69    */

70   public final void setPieChart2DProperties (PieChart2DProperties props) {
71
72     needsUpdate = true;
73     props.addPieChart2D (this);
74     if (pieChart2DProps != null) pieChart2DProps.removePieChart2D (this);
75     pieChart2DProps = props;
76   }
77
78
79   /**
80    * Sets the Dataset for this PieChart2D.
81    * @param d The Dataset.
82    */

83   public final void setDataset (Dataset d) {
84
85     needsUpdate = true;
86     d.addChart2D (this);
87     if (dataset != null) dataset.removeChart2D (this);
88     dataset = d;
89   }
90
91
92   /**
93    * Sets the MultiColorsProperties for this PieChart2D.
94    * @param props The MultiColorsProperties.
95    */

96   public final void setMultiColorsProperties (MultiColorsProperties props) {
97
98     needsUpdate = true;
99     props.addObject2D (this);
100     if (multiColorsProps != null) multiColorsProps.removeObject2D (this);
101     multiColorsProps = props;
102   }
103
104
105   /**
106    * Sets the allocation of space to each component of a pie chart.
107    * There are three components: pieInfo and legend.
108    * The pieInfoW needs to be within 0f and 1f.
109    * The legendW will be 1f - pieInfoW.
110    * Both the pieInfoW and the legendH will be 1f.
111    * @param pieInfoW The ratio of pieInfo width to total.
112    */

113   public final void setLayoutRatios (float pieInfoW) {
114
115     needsUpdate = true;
116
117     chart.setPieLabelsToWidthRatio (pieInfoW);
118     chart.setPieLabelsToHeightRatio (1f);
119     chart.setLegendToWidthRatio (1f - pieInfoW);
120     chart.setLegendToHeightRatio (1f);
121
122     chart.setAutoSetLayoutRatios (true);
123   }
124
125
126   /**
127    * Sets a custom preferred size for the chart.
128    * This custom size will override the preferred size calculations that normally occurr.
129    * If null is passed, the preferred size calculations will be reinstated.
130    * @param size The custom preferred size for this chart.
131    */

132   public final void setPreferredSize (Dimension size) {
133
134     needsUpdate = true;
135     customizePrefSize = size != null;
136     customPrefSize = size;
137     prefSize = null;
138   }
139
140
141   /**
142    * Gets the PieChart2DProperties for this PieChart2D.
143    * @return The PieChart2DProperties.
144    */

145   public final PieChart2DProperties getPieChart2DProperties() {
146     return pieChart2DProps;
147   }
148
149
150   /**
151    * Gets the Dataset for this PieChart2D.
152    * @return The Dataset.
153    */

154   public final Dataset getDataset() {
155     return dataset;
156   }
157
158
159   /**
160    * Gets the MultiColorsProperties for this PieChart2D.
161    * @return The MultiColorsProperties.
162    */

163   public final MultiColorsProperties getMultiColorsProperties() {
164     return multiColorsProps;
165   }
166
167
168   /**
169    * Gets a buffered image of the chart.
170    * @return An image of this chart
171    */

172   public final BufferedImage getImage() {
173
174     if (getSize().width <= 0 || getSize().height <= 0) pack();
175     else updateImage (getSize());
176
177     if (!chart.getBackgroundExistence()) {
178
179       Graphics2D imageG2D = image.createGraphics();
180       imageG2D.setRenderingHint (
181         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
182       chart.paintComponent (imageG2D);
183       Point location = chart.getSizeLocation (chart.MIN);
184       imageSize = chart.getSize (chart.MIN);
185       image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height);
186     }
187
188     return image;
189   }
190
191
192   /**
193    * Gets the preferred size of the chart.
194    * The preferred size is within the maximum and minimum sizes of the chart.
195    * Much calculation is performed when calling this method.
196    * @return The preferred minimum size of the chart.
197    */

198   public final Dimension getPreferredSize() {
199
200     updateChart2D();
201     updatePieChartArea();
202
203     if (!customizePrefSize) {
204
205       boolean autoModel = chart.getAutoSize (chart.MAXMODEL);
206       boolean autoMin = chart.getAutoSize (chart.MIN);
207       chart.setAutoSizes (true, false);
208       chart.resetPieChartAreaModel (true);
209       chart.setAutoSetLayoutRatios (true);
210       chart.setSize (chart.MAX, getMaximumSize());
211       BufferedImage image = new BufferedImage (
212         getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
213       Graphics2D imageG2D = image.createGraphics();
214       prefSize = chart.getPrefSize (imageG2D);
215       chart.setAutoSizes (autoModel, autoMin);
216     }
217     else prefSize = customPrefSize;
218
219     int prefWidth =
220       prefSize.width < getMinimumSize().width ? getMinimumSize().width : prefSize.width;
221     int prefHeight =
222       prefSize.height < getMinimumSize().height ? getMinimumSize().height : prefSize.height;
223     prefSize.setSize (prefWidth, prefHeight);
224
225     this.size = prefSize;
226     chart.resetPieChartAreaModel (true);
227     chart.setAutoSetLayoutRatios (true);
228     chart.setSize (chart.MAX, size);
229     if (!chart.getBackgroundExistence()) {
230
231       image = new BufferedImage (
232           getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
233       Graphics2D imageG2D = image.createGraphics();
234       imageG2D.setRenderingHint (
235         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
236       chart.updatePieChartArea (imageG2D);
237     }
238     else {
239
240       image = new BufferedImage (size.width, size.height, BufferedImage.TYPE_INT_BGR);
241       Graphics2D imageG2D = image.createGraphics();
242       imageG2D.setRenderingHint (
243         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
244       chart.paintComponent (imageG2D);
245       Point location = chart.getSizeLocation (chart.MIN);
246       imageSize = chart.getSize (chart.MIN);
247       image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height);
248     }
249
250     needsUpdate = false;
251
252     return prefSize;
253   }
254
255
256   /**
257    * Causes the object to reinintialize to it's preferred size.
258    */

259   public final void pack() {
260
261     needsUpdate = true;
262     setSize (getPreferredSize());
263   }
264
265
266   /**
267    * Validates the properties of this object.
268    * If debug is true then prints a messages indicating whether each property is valid.
269    * Returns true if all the properties were valid and false otherwise.
270    * @param debug If true then will print status messages.
271    * @return If true then valid.
272    */

273   public final boolean validate (boolean debug) {
274
275     if (debug) System.out.println ("Validating PieChart2D");
276
277     boolean valid = true;
278
279     if (!validateChart2D (debug)) valid = false;
280
281     if (pieChart2DProps == null) {
282       valid = false;
283       if (debug) System.out.println ("PieChart2DProperties is null");
284     }
285     else if (!pieChart2DProps.validate (debug)) valid = false;
286
287     if (dataset == null) {
288       valid = false;
289       if (debug) System.out.println ("Dataset is null");
290     }
291     else if (!dataset.validate (debug)) valid = false;
292
293     if (multiColorsProps == null) {
294       valid = false;
295       if (debug) System.out.println ("MultiColorsProperties is null");
296     }
297     else if (!multiColorsProps.validate (debug)) valid = false;
298
299     //Restriction: If multi colors customize and num sets > 0, then custom colors > 0
300
if (valid) {
301       if (dataset.getNumSets() > 0 &&
302         multiColorsProps.getColorsCustomize() &&
303         multiColorsProps.getColorsCustom().length < 1) {
304         valid = false;
305         if (debug) System.out.println ("Not enough custom colors for MultiColorsProperties");
306       }
307     }
308
309     if (debug) {
310       if (valid) System.out.println ("PieChart2D was valid");
311       else {
312         System.out.println (
313           "Possibly unable to check for all invalidities because of early invalidity");
314         System.out.println ("PieChart2D was invalid");
315       }
316     }
317
318     return valid;
319   }
320
321
322   /**
323    * Paints the chart.
324    * This is provided for the layout manager to call.
325    * @param g The graphics context for calculations and painting.
326    */

327   public final void paintComponent (Graphics g) {
328
329     super.paintComponent (g);
330     Graphics2D g2D = (Graphics2D)g;
331
332     updateImage (getSize());
333
334     if (!chart.getBackgroundExistence()) {
335
336       g2D.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
337       chart.paintComponent (g2D);
338     }
339     else g2D.drawImage (image, 0, 0, imageSize.width, imageSize.height, this);
340   }
341
342
343   /**
344    * Gets the PieChartArea of this PieChart2D.
345    * @return The PieChartArea of this PieChart2D.
346    */

347   final TitledArea getObjectArea() {
348     return chart;
349   }
350
351
352   private boolean getNeedsUpdate() {
353
354     return (needsUpdate || size.width != getSize().width || size.height != getSize().height ||
355       getNeedsUpdateChart2D() || pieChart2DProps.getPieChart2DNeedsUpdate (this) ||
356       dataset.getChart2DNeedsUpdate (this) || multiColorsProps.getObject2DNeedsUpdate (this));
357   }
358
359
360   private void updateImage (Dimension size) {
361
362     if (prefSize == null) getPreferredSize();
363
364     if (getNeedsUpdate()) {
365
366       updateChart2D();
367       updatePieChartArea();
368
369       this.size = size;
370       chart.setSize (chart.MAX, size);
371
372       if (!chart.getBackgroundExistence()) {
373
374         image = new BufferedImage (
375           getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
376         Graphics2D imageG2D = image.createGraphics();
377         imageG2D.setRenderingHint (
378           RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
379         chart.updatePieChartArea (imageG2D);
380       }
381       else {
382
383         image = new BufferedImage (
384           size.width, size.height, BufferedImage.TYPE_INT_BGR);
385         Graphics2D imageG2D = image.createGraphics();
386         imageG2D.setRenderingHint (
387           RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
388         chart.paintComponent (imageG2D);
389         Point location = chart.getSizeLocation (chart.MIN);
390         imageSize = chart.getSize (chart.MIN);
391         image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height);
392       }
393
394       needsUpdate = false;
395     }
396   }
397
398
399   private void updatePieChartArea() {
400
401     if (pieChart2DProps.getPieChart2DNeedsUpdate (this) ||
402       dataset.getChart2DNeedsUpdate (this) || multiColorsProps.getObject2DNeedsUpdate (this)) {
403
404       pieChart2DProps.updatePieChart2D (this);
405       dataset.updateChart2D (this);
406       multiColorsProps.updateObject2D (this);
407
408       chart.setDataset (dataset.getOldPieStruct());
409       chart.setDatasetColors (multiColorsProps.getColorsArray (dataset.getNumSets()));
410       chart.setLabelsLinesExistence (pieChart2DProps.getPieLabelsLinesExistence());
411       chart.setLabelsLinesThicknessModel (pieChart2DProps.getPieLabelsLinesThicknessModel());
412       chart.setLabelsLinesColor (pieChart2DProps.getPieLabelsLinesColor());
413       chart.setLabelsLineDotsExistence (pieChart2DProps.getPieLabelsLinesDotsExistence());
414       chart.setLabelsLineDotsThicknessModel (pieChart2DProps.getPieLabelsLinesDotsThicknessModel());
415       chart.setLabelsLineDotsColor (pieChart2DProps.getPieLabelsLinesDotsColor());
416
417       PieInfoArea pieInfo = chart.getPieInfoArea();
418       pieInfo.setPieLabelsExistence(pieChart2DProps.getPieLabelsExistence());
419       pieInfo.setLabelsType (pieChart2DProps.getPieLabelsType());
420       pieInfo.setBetweenLabelsGapExistence (
421         pieChart2DProps.getPieLabelsBetweenLabelsGapExistence());
422       pieInfo.setBetweenLabelsGapThicknessModel (
423         pieChart2DProps.getPieLabelsBetweenLabelsGapThicknessModel());
424       pieInfo.setLabelsPointsGapExistence (
425         pieChart2DProps.getPieLabelsPointsGapOffsetExistence());
426       pieInfo.setLabelsPointsGapThicknessModel (
427         (int)(pieChart2DProps.getPieLabelsPointsGapOffsetModelRatio() *
428         pieChart2DProps.getChartBetweenPieLabelsAndPieGapThicknessModel()));
429       pieInfo.setFontPointModel (pieChart2DProps.getPieLabelsFontPointModel());
430       pieInfo.setFontName (pieChart2DProps.getPieLabelsFontName());
431       pieInfo.setFontColor (pieChart2DProps.getPieLabelsFontColor());
432       pieInfo.setFontStyle (pieChart2DProps.getPieLabelsFontStyle());
433
434       PieArea pie = pieInfo.getPieArea();
435       pie.setGapExistence (pieChart2DProps.getChartBetweenPieLabelsAndPieGapExistence());
436       pie.setGapThicknessModel (pieChart2DProps.getChartBetweenPieLabelsAndPieGapThicknessModel());
437       pie.setPiePrefSizeModel (pieChart2DProps.getPiePreferredSize());
438       pie.setOutlineSectors(pieChart2DProps.getPieSectorsOutlineExistence());
439       pie.setOutlineSectorsColor(pieChart2DProps.getPieSectorsOutlineColor());
440       pie.setSectorPointDepthRatio(pieChart2DProps.getPieLabelsPointsPieSectorsDepthRatio());
441       pie.setSectorGapPointRatio(
442         pieChart2DProps.getPieLabelsPointsBetweenPieAndLabelGapsDepthRatio());
443       pie.setLightSource (pieChart2DProps.getPieSectorLightSource());
444     }
445   }
446 }
Popular Tags