1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 import java.awt.image.*; 29 30 31 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 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 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 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 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 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 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 145 public final PieChart2DProperties getPieChart2DProperties() { 146 return pieChart2DProps; 147 } 148 149 150 154 public final Dataset getDataset() { 155 return dataset; 156 } 157 158 159 163 public final MultiColorsProperties getMultiColorsProperties() { 164 return multiColorsProps; 165 } 166 167 168 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 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 259 public final void pack() { 260 261 needsUpdate = true; 262 setSize (getPreferredSize()); 263 } 264 265 266 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 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 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 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 |