| 1 122 123 package org.jfree.chart; 124 125 import java.awt.AlphaComposite ; 126 import java.awt.BasicStroke ; 127 import java.awt.Color ; 128 import java.awt.Composite ; 129 import java.awt.Font ; 130 import java.awt.Graphics2D ; 131 import java.awt.Image ; 132 import java.awt.Paint ; 133 import java.awt.RenderingHints ; 134 import java.awt.Shape ; 135 import java.awt.Stroke ; 136 import java.awt.geom.AffineTransform ; 137 import java.awt.geom.Point2D ; 138 import java.awt.geom.Rectangle2D ; 139 import java.awt.image.BufferedImage ; 140 import java.io.IOException ; 141 import java.io.ObjectInputStream ; 142 import java.io.ObjectOutputStream ; 143 import java.io.Serializable ; 144 import java.net.URL ; 145 import java.util.ArrayList ; 146 import java.util.Arrays ; 147 import java.util.Iterator ; 148 import java.util.List ; 149 import java.util.ResourceBundle ; 150 151 import javax.swing.ImageIcon ; 152 import javax.swing.UIManager ; 153 import javax.swing.event.EventListenerList ; 154 155 import org.jfree.JCommon; 156 import org.jfree.chart.block.BlockBorder; 157 import org.jfree.chart.block.BlockParams; 158 import org.jfree.chart.block.EntityBlockResult; 159 import org.jfree.chart.block.LengthConstraintType; 160 import org.jfree.chart.block.RectangleConstraint; 161 import org.jfree.chart.entity.EntityCollection; 162 import org.jfree.chart.event.ChartChangeEvent; 163 import org.jfree.chart.event.ChartChangeListener; 164 import org.jfree.chart.event.ChartProgressEvent; 165 import org.jfree.chart.event.ChartProgressListener; 166 import org.jfree.chart.event.LegendChangeEvent; 167 import org.jfree.chart.event.LegendChangeListener; 168 import org.jfree.chart.event.PlotChangeEvent; 169 import org.jfree.chart.event.PlotChangeListener; 170 import org.jfree.chart.event.TitleChangeEvent; 171 import org.jfree.chart.event.TitleChangeListener; 172 import org.jfree.chart.plot.CategoryPlot; 173 import org.jfree.chart.plot.Plot; 174 import org.jfree.chart.plot.PlotRenderingInfo; 175 import org.jfree.chart.plot.XYPlot; 176 import org.jfree.chart.title.LegendTitle; 177 import org.jfree.chart.title.TextTitle; 178 import org.jfree.chart.title.Title; 179 import org.jfree.data.Range; 180 import org.jfree.io.SerialUtilities; 181 import org.jfree.ui.Align; 182 import org.jfree.ui.Drawable; 183 import org.jfree.ui.HorizontalAlignment; 184 import org.jfree.ui.RectangleEdge; 185 import org.jfree.ui.RectangleInsets; 186 import org.jfree.ui.Size2D; 187 import org.jfree.ui.VerticalAlignment; 188 import org.jfree.ui.about.Contributor; 189 import org.jfree.ui.about.Licences; 190 import org.jfree.ui.about.ProjectInfo; 191 import org.jfree.util.ObjectUtilities; 192 193 216 public class JFreeChart implements Drawable, 217 TitleChangeListener, 218 LegendChangeListener, 219 PlotChangeListener, 220 Serializable , 221 Cloneable { 222 223 224 private static final long serialVersionUID = -3470703747817429120L; 225 226 227 public static final ProjectInfo INFO = new JFreeChartInfo(); 228 229 230 public static final Font DEFAULT_TITLE_FONT 231 = new Font ("SansSerif", Font.BOLD, 18); 232 233 234 public static final Paint DEFAULT_BACKGROUND_PAINT 235 = UIManager.getColor("Panel.background"); 236 237 238 public static final Image DEFAULT_BACKGROUND_IMAGE = null; 239 240 241 public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENT = Align.FIT; 242 243 244 public static final float DEFAULT_BACKGROUND_IMAGE_ALPHA = 0.5f; 245 246 247 private transient RenderingHints renderingHints; 248 249 250 private boolean borderVisible; 251 252 253 private transient Stroke borderStroke; 254 255 256 private transient Paint borderPaint; 257 258 259 private TextTitle title; 260 261 262 private List subtitles; 263 264 265 private OldLegend oldLegend; 266 267 268 private Plot plot; 269 270 271 private transient Paint backgroundPaint; 272 273 274 private transient Image backgroundImage; 276 277 private int backgroundImageAlignment = Align.FIT; 278 279 280 private float backgroundImageAlpha = 0.5f; 281 282 283 private transient EventListenerList changeListeners; 284 285 286 private transient EventListenerList progressListeners; 287 288 292 private boolean notify; 293 294 305 public JFreeChart(Plot plot) { 306 307 this( 308 null, null, plot, 311 true ); 313 314 } 315 316 328 public JFreeChart(String title, Plot plot) { 329 this(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); 330 } 331 332 349 public JFreeChart(String title, Font titleFont, Plot plot, 350 boolean createLegend) { 351 352 if (plot == null) { 353 throw new NullPointerException ("Null 'plot' argument."); 354 } 355 356 this.progressListeners = new EventListenerList (); 358 this.changeListeners = new EventListenerList (); 359 this.notify = true; 362 this.renderingHints = new RenderingHints ( 363 RenderingHints.KEY_ANTIALIASING, 364 RenderingHints.VALUE_ANTIALIAS_ON 365 ); 366 367 this.borderVisible = false; 368 this.borderStroke = new BasicStroke (1.0f); 369 this.borderPaint = Color.black; 370 371 this.plot = plot; 372 plot.addChangeListener(this); 373 374 this.subtitles = new ArrayList (); 375 376 if (createLegend) { 378 LegendTitle legend = new LegendTitle(this.plot); 379 legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0)); 380 legend.setBorder(new BlockBorder()); 381 legend.setBackgroundPaint(Color.white); 382 legend.setPosition(RectangleEdge.BOTTOM); 383 this.subtitles.add(legend); 384 } 385 386 if (title != null) { 388 if (titleFont == null) { 389 titleFont = DEFAULT_TITLE_FONT; 390 } 391 this.title = new TextTitle(title, titleFont); 392 this.title.addChangeListener(this); 393 } 394 395 this.backgroundPaint = DEFAULT_BACKGROUND_PAINT; 396 397 this.backgroundImage = DEFAULT_BACKGROUND_IMAGE; 398 this.backgroundImageAlignment = DEFAULT_BACKGROUND_IMAGE_ALIGNMENT; 399 this.backgroundImageAlpha = DEFAULT_BACKGROUND_IMAGE_ALPHA; 400 401 } 402 403 408 public RenderingHints getRenderingHints() { 409 return this.renderingHints; 410 } 411 412 420 public void setRenderingHints(RenderingHints renderingHints) { 421 if (renderingHints == null) { 422 throw new NullPointerException ("RenderingHints given are null"); 423 } 424 this.renderingHints = renderingHints; 425 fireChartChanged(); 426 } 427 428 434 public boolean isBorderVisible() { 435 return this.borderVisible; 436 } 437 438 444 public void setBorderVisible(boolean visible) { 445 this.borderVisible = visible; 446 fireChartChanged(); 447 } 448 449 454 public Stroke getBorderStroke() { 455 return this.borderStroke; 456 } 457 458 463 public void setBorderStroke(Stroke stroke) { 464 this.borderStroke = stroke; 465 fireChartChanged(); 466 } 467 468 473 public Paint getBorderPaint() { 474 return this.borderPaint; 475 } 476 477 482 public void setBorderPaint(Paint paint) { 483 this.borderPaint = paint; 484 fireChartChanged(); 485 } 486 487 495 public TextTitle getTitle() { 496 return this.title; 497 } 498 499 507 public void setTitle(TextTitle title) { 508 this.title = title; 509 fireChartChanged(); 510 } 511 512 522 public void setTitle(String text) { 523 if (text != null) { 524 if (this.title == null) { 525 setTitle(new TextTitle(text, JFreeChart.DEFAULT_TITLE_FONT)); 526 } 527 else { 528 this.title.setText(text); 529 } 530 } 531 else { 532 setTitle((TextTitle) null); 533 } 534 } 535 536 542 public LegendTitle getLegend() { 543 return getLegend(0); 544 } 545 546 553 public LegendTitle getLegend(int index) { 554 int seen = 0; 555 Iterator iterator = this.subtitles.iterator(); 556 while (iterator.hasNext()) { 557 Title subtitle = (Title) iterator.next(); 558 if (subtitle instanceof LegendTitle) { 559 if (seen == index) { 560 return (LegendTitle) subtitle; 561 } 562 else { 563 seen++; 564 } 565 } 566 } 567 return null; 568 } 569 570 574 public void removeLegend() { 575 removeSubtitle(getLegend()); 576 } 577 578 583 public List getSubtitles() { 584 return this.subtitles; 585 } 586 587 594 public void setSubtitles(List subtitles) { 595 if (subtitles == null) { 596 throw new NullPointerException ("Null 'subtitles' argument."); 597 } 598 this.subtitles = subtitles; 599 fireChartChanged(); 600 } 601 602 607 public int getSubtitleCount() { 608 return this.subtitles.size(); 609 } 610 611 618 public Title getSubtitle(int index) { 619 if ((index < 0) || (index == getSubtitleCount())) { 620 throw new IllegalArgumentException ("Index out of range."); 621 } 622 return (Title) this.subtitles.get(index); 623 } 624 625 631 public void addSubtitle(Title subtitle) { 632 if (subtitle == null) { 633 throw new IllegalArgumentException ("Null 'subtitle' argument."); 634 } 635 this.subtitles.add(subtitle); 636 subtitle.addChangeListener(this); 637 fireChartChanged(); 638 } 639 640 644 public void clearSubtitles() { 645 Iterator iterator = this.subtitles.iterator(); 646 while (iterator.hasNext()) { 647 Title t = (Title) iterator.next(); 648 t.removeChangeListener(this); 649 } 650 this.subtitles.clear(); 651 fireChartChanged(); 652 } 653 654 660 public void removeSubtitle(Title title) { 661 this.subtitles.remove(title); 662 fireChartChanged(); 663 } 664 665 670 public OldLegend getOldLegend() { 671 return this.oldLegend; 672 } 673 674 680 public void setOldLegend(OldLegend legend) { 681 682 OldLegend existing = this.oldLegend; 685 if (existing != null) { 686 existing.removeChangeListener(this); 687 existing.registerChart(null); 688 } 689 690 this.oldLegend = legend; 692 if (legend != null) { 693 legend.registerChart(this); 694 legend.addChangeListener(this); 695 } 696 697 fireChartChanged(); 699 700 } 701 702 709 public Plot getPlot() { 710 return this.plot; 711 } 712 713 721 public CategoryPlot getCategoryPlot() { 722 return (CategoryPlot) this.plot; 723 } 724 725 733 public XYPlot getXYPlot() { 734 return (XYPlot) this.plot; 735 } 736 737 743 public boolean getAntiAlias() { 744 Object o = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING); 745 if (o == null) { 746 return false; 747 } 748 return (o.equals(RenderingHints.VALUE_ANTIALIAS_ON)); 749 } 750 751 759 public void setAntiAlias(boolean flag) { 760 761 Object o = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING); 762 if (o == null) { 763 o = RenderingHints.VALUE_ANTIALIAS_DEFAULT; 764 } 765 if (!flag && RenderingHints.VALUE_ANTIALIAS_OFF.equals(o) 766 || flag && RenderingHints.VALUE_ANTIALIAS_ON.equals(o)) { 767 return; 769 } 770 if (flag) { 771 this.renderingHints.put(RenderingHints.KEY_ANTIALIASING, 772 RenderingHints.VALUE_ANTIALIAS_ON); 773 } 774 else { 775 this.renderingHints.put(RenderingHints.KEY_ANTIALIASING, 776 RenderingHints.VALUE_ANTIALIAS_OFF); 777 } 778 fireChartChanged(); 779 780 } 781 782 787 public Paint getBackgroundPaint() { 788 return this.backgroundPaint; 789 } 790 791 797 public void setBackgroundPaint(Paint paint) { 798 799 if (this.backgroundPaint != null) { 800 if (!this.backgroundPaint.equals(paint)) { 801 this.backgroundPaint = paint; 802 fireChartChanged(); 803 } 804 } 805 else { 806 if (paint != null) { 807 this.backgroundPaint = paint; 808 fireChartChanged(); 809 } 810 } 811 812 } 813 814 820 public Image getBackgroundImage() { 821 return this.backgroundImage; 822 } 823 824 830 public void setBackgroundImage(Image image) { 831 832 if (this.backgroundImage != null) { 833 if (!this.backgroundImage.equals(image)) { 834 this.backgroundImage = image; 835 fireChartChanged(); 836 } 837 } 838 else { 839 if (image != null) { 840 this.backgroundImage = image; 841 fireChartChanged(); 842 } 843 } 844 845 } 846 847 854 public int getBackgroundImageAlignment() { 855 return this.backgroundImageAlignment; 856 } 857 858 864 public void setBackgroundImageAlignment(int alignment) { 865 if (this.backgroundImageAlignment != alignment) { 866 this.backgroundImageAlignment = alignment; 867 fireChartChanged(); 868 } 869 } 870 871 876 public float getBackgroundImageAlpha() { 877 return this.backgroundImageAlpha; 878 } 879 880 886 public void setBackgroundImageAlpha(float alpha) { 887 888 if (this.backgroundImageAlpha != alpha) { 889 this.backgroundImageAlpha = alpha; 890 fireChartChanged(); 891 } 892 893 } 894 895 901 public boolean isNotify() { 902 return this.notify; 903 } 904 905 911 public void setNotify(boolean notify) { 912 this.notify = notify; 913 if (notify) { 915 notifyListeners(new ChartChangeEvent(this)); 916 } 917 } 918 919 928 public void draw(Graphics2D g2, Rectangle2D area) { 929 draw(g2, area, null, null); 930 } 931 932 940 public void draw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) { 941 draw(g2, area, null, info); 942 } 943 944 |