| 1 123 124 package org.jfree.chart; 125 126 import java.awt.AWTEvent ; 127 import java.awt.Dimension ; 128 import java.awt.Graphics ; 129 import java.awt.Graphics2D ; 130 import java.awt.Image ; 131 import java.awt.Insets ; 132 import java.awt.Point ; 133 import java.awt.event.ActionEvent ; 134 import java.awt.event.ActionListener ; 135 import java.awt.event.MouseEvent ; 136 import java.awt.event.MouseListener ; 137 import java.awt.event.MouseMotionListener ; 138 import java.awt.geom.AffineTransform ; 139 import java.awt.geom.Line2D ; 140 import java.awt.geom.Point2D ; 141 import java.awt.geom.Rectangle2D ; 142 import java.awt.print.PageFormat ; 143 import java.awt.print.Printable ; 144 import java.awt.print.PrinterException ; 145 import java.awt.print.PrinterJob ; 146 import java.io.File ; 147 import java.io.IOException ; 148 import java.io.Serializable ; 149 import java.util.Iterator ; 150 import java.util.List ; 151 import java.util.ResourceBundle ; 152 153 import javax.swing.JFileChooser ; 154 import javax.swing.JMenu ; 155 import javax.swing.JMenuItem ; 156 import javax.swing.JOptionPane ; 157 import javax.swing.JPanel ; 158 import javax.swing.JPopupMenu ; 159 import javax.swing.ToolTipManager ; 160 161 import org.jfree.chart.entity.ChartEntity; 162 import org.jfree.chart.entity.EntityCollection; 163 import org.jfree.chart.event.ChartChangeEvent; 164 import org.jfree.chart.event.ChartChangeListener; 165 import org.jfree.chart.event.ChartProgressEvent; 166 import org.jfree.chart.event.ChartProgressListener; 167 import org.jfree.chart.plot.Plot; 168 import org.jfree.chart.plot.PlotOrientation; 169 import org.jfree.chart.plot.PlotRenderingInfo; 170 import org.jfree.chart.plot.ValueAxisPlot; 171 import org.jfree.chart.plot.Zoomable; 172 import org.jfree.chart.ui.ChartPropertyEditPanel; 173 import org.jfree.ui.ExtensionFileFilter; 174 175 182 public class ChartPanel extends JPanel 183 implements ChartChangeListener, 184 ChartProgressListener, 185 ActionListener , 186 MouseListener , 187 MouseMotionListener , 188 Printable , 189 Serializable { 190 191 192 private static final long serialVersionUID = 6046366297214274674L; 193 194 195 public static final boolean DEFAULT_BUFFER_USED = false; 196 197 198 public static final int DEFAULT_WIDTH = 680; 199 200 201 public static final int DEFAULT_HEIGHT = 420; 202 203 204 public static final int DEFAULT_MINIMUM_DRAW_WIDTH = 300; 205 206 207 public static final int DEFAULT_MINIMUM_DRAW_HEIGHT = 200; 208 209 210 public static final int DEFAULT_MAXIMUM_DRAW_WIDTH = 800; 211 212 213 public static final int DEFAULT_MAXIMUM_DRAW_HEIGHT = 600; 214 215 216 public static final int DEFAULT_ZOOM_TRIGGER_DISTANCE = 10; 217 218 219 public static final String PROPERTIES_COMMAND = "PROPERTIES"; 220 221 222 public static final String SAVE_COMMAND = "SAVE"; 223 224 225 public static final String PRINT_COMMAND = "PRINT"; 226 227 228 public static final String ZOOM_IN_BOTH_COMMAND = "ZOOM_IN_BOTH"; 229 230 231 public static final String ZOOM_IN_DOMAIN_COMMAND = "ZOOM_IN_DOMAIN"; 232 233 234 public static final String ZOOM_IN_RANGE_COMMAND = "ZOOM_IN_RANGE"; 235 236 237 public static final String ZOOM_OUT_BOTH_COMMAND = "ZOOM_OUT_BOTH"; 238 239 240 public static final String ZOOM_OUT_DOMAIN_COMMAND = "ZOOM_DOMAIN_BOTH"; 241 242 243 public static final String ZOOM_OUT_RANGE_COMMAND = "ZOOM_RANGE_BOTH"; 244 245 246 public static final String ZOOM_RESET_BOTH_COMMAND = "ZOOM_RESET_BOTH"; 247 248 249 public static final String ZOOM_RESET_DOMAIN_COMMAND = "ZOOM_RESET_DOMAIN"; 250 251 252 public static final String ZOOM_RESET_RANGE_COMMAND = "ZOOM_RESET_RANGE"; 253 254 255 private JFreeChart chart; 256 257 258 private List chartMouseListeners; 259 260 261 private boolean useBuffer; 262 263 264 private boolean refreshBuffer; 265 266 267 private Image chartBuffer; 268 269 270 private int chartBufferHeight; 271 272 273 private int chartBufferWidth; 274 275 278 private int minimumDrawWidth; 279 280 284 private int minimumDrawHeight; 285 286 290 private int maximumDrawWidth; 291 292 296 private int maximumDrawHeight; 297 298 299 private JPopupMenu popup; 300 301 302 private ChartRenderingInfo info; 303 304 305 private Point2D anchor; 306 307 308 private double scaleX; 309 310 311 private double scaleY; 312 313 314 private PlotOrientation orientation = PlotOrientation.VERTICAL; 315 316 317 private boolean domainZoomable = false; 318 319 320 private boolean rangeZoomable = false; 321 322 327 private Point zoomPoint = null; 328 329 330 private transient Rectangle2D zoomRectangle = null; 331 332 333 private boolean fillZoomRectangle = false; 334 335 336 private int zoomTriggerDistance; 337 338 339 private boolean horizontalAxisTrace = false; 340 341 342 private boolean verticalAxisTrace = false; 343 344 345 private transient Line2D verticalTraceLine; 346 347 348 private transient Line2D horizontalTraceLine; 349 350 351 private JMenuItem zoomInBothMenuItem; 352 353 354 private JMenuItem zoomInDomainMenuItem; 355 356 357 private JMenuItem zoomInRangeMenuItem; 358 359 360 private JMenuItem zoomOutBothMenuItem; 361 362 363 private JMenuItem zoomOutDomainMenuItem; 364 365 366 private JMenuItem zoomOutRangeMenuItem; 367 368 369 private JMenuItem zoomResetBothMenuItem; 370 371 372 private JMenuItem zoomResetDomainMenuItem; 373 374 375 private JMenuItem zoomResetRangeMenuItem; 376 377 378 private boolean enforceFileExtensions; 379 380 381 private boolean ownToolTipDelaysActive; 382 383 384 private int originalToolTipInitialDelay; 385 386 387 private int originalToolTipReshowDelay; 388 389 390 private int originalToolTipDismissDelay; 391 392 393 private int ownToolTipInitialDelay; 394 395 396 private int ownToolTipReshowDelay; 397 398 399 private int ownToolTipDismissDelay; 400 401 402 private double zoomInFactor = 0.5; 403 404 405 private double zoomOutFactor = 2.0; 406 407 408 protected static ResourceBundle localizationResources 409 = ResourceBundle.getBundle("org.jfree.chart.LocalizationBundle"); 410 411 416 public ChartPanel(JFreeChart chart) { 417 418 this( 419 chart, 420 DEFAULT_WIDTH, 421 DEFAULT_HEIGHT, 422 DEFAULT_MINIMUM_DRAW_WIDTH, 423 DEFAULT_MINIMUM_DRAW_HEIGHT, 424 DEFAULT_MAXIMUM_DRAW_WIDTH, 425 DEFAULT_MAXIMUM_DRAW_HEIGHT, 426 DEFAULT_BUFFER_USED, 427 true, true, true, true, true ); 433 434 } 435 436 443 public ChartPanel(JFreeChart chart, boolean useBuffer) { 444 445 this(chart, 446 DEFAULT_WIDTH, 447 DEFAULT_HEIGHT, 448 DEFAULT_MINIMUM_DRAW_WIDTH, 449 DEFAULT_MINIMUM_DRAW_HEIGHT, 450 DEFAULT_MAXIMUM_DRAW_WIDTH, 451 DEFAULT_MAXIMUM_DRAW_HEIGHT, 452 useBuffer, 453 true, true, true, true, true ); 459 460 } 461 462 477 public ChartPanel(JFreeChart chart, 478 boolean properties, 479 boolean save, 480 boolean print, 481 boolean zoom, 482 boolean tooltips) { 483 484 this(chart, 485 DEFAULT_WIDTH, 486 DEFAULT_HEIGHT, 487 DEFAULT_MINIMUM_DRAW_WIDTH, 488 DEFAULT_MINIMUM_DRAW_HEIGHT, 489 DEFAULT_MAXIMUM_DRAW_WIDTH, 490 DEFAULT_MAXIMUM_DRAW_HEIGHT, 491 DEFAULT_BUFFER_USED, 492 properties, 493 save, 494 print, 495 zoom, 496 tooltips 497 ); 498 499 } 500 501 525 public ChartPanel(JFreeChart chart, 526 int width, 527 int height, 528 int minimumDrawWidth, 529 int minimumDrawHeight, 530 int maximumDrawWidth, 531 int maximumDrawHeight, 532 boolean useBuffer, 533 boolean properties, 534 boolean save, 535 boolean print, 536 boolean zoom, 537 boolean tooltips) { 538 539 this.chart = chart; 540 this.chartMouseListeners = new java.util.ArrayList (); 541 if (chart != null) { 542 chart.addChangeListener(this); 543 Plot plot = chart.getPlot(); 544 this.domainZoomable = false; 545 this.rangeZoomable = false; 546 if (plot instanceof Zoomable) { 547 Zoomable z = (Zoomable) plot; 548 this.domainZoomable = z.isDomainZoomable(); 549 this.rangeZoomable = z.isRangeZoomable(); 550 this.orientation = z.getOrientation(); 551 } 552 } 553 this.info = new ChartRenderingInfo(); 554 setPreferredSize(new Dimension (width, height)); 555 this.useBuffer = useBuffer; 556 this.refreshBuffer = false; 557 this.minimumDrawWidth = minimumDrawWidth; 558 this.minimumDrawHeight = minimumDrawHeight; 559 this.maximumDrawWidth = maximumDrawWidth; 560 this.maximumDrawHeight = maximumDrawHeight; 561 this.zoomTriggerDistance = DEFAULT_ZOOM_TRIGGER_DISTANCE; 562 563 this.popup = null; 565 if (properties || save || print || zoom) { 566 this.popup = createPopupMenu(properties, save, print, zoom); 567 } 568 569 enableEvents(AWTEvent.MOUSE_EVENT_MASK); 570 enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); 571 setDisplayToolTips(tooltips); 572 addMouseListener(this); 573 addMouseMotionListener(this); 574 575 this.enforceFileExtensions = true; 576 577 ToolTipManager ttm = ToolTipManager.sharedInstance(); 580 this.ownToolTipInitialDelay = ttm.getInitialDelay(); 581 this.ownToolTipDismissDelay = ttm.getDismissDelay(); 582 this.ownToolTipReshowDelay = ttm.getReshowDelay(); 583 584 } 585 586 591 public JFreeChart getChart() { 592 return this.chart; 593 } 594 595 600 public void setChart(JFreeChart chart) { 601 602 if (this.chart != null) { 604 this.chart.removeChangeListener(this); 605 this.chart.removeProgressListener(this); 606 } 607 608 this.chart = chart; 610 if (chart != null) { 611 this.chart.addChangeListener(this); 612 this.chart.addProgressListener(this); 613 Plot plot = chart.getPlot(); 614 this.domainZoomable = false; 615 this.rangeZoomable = false; 616 if (plot instanceof Zoomable) { 617 Zoomable z = (Zoomable) plot; 618 this.domainZoomable = z.isDomainZoomable(); 619 this.rangeZoomable = z.isRangeZoomable(); 620 this.orientation = z.getOrientation(); 621 } 622 } 623 else { 624 this.domainZoomable = false; 625 this.rangeZoomable = false; 626 } 627 if (this.useBuffer) { 628 this.refreshBuffer = true; 629 } 630 repaint(); 631 632 } 633 634 642 public int getMinimumDrawWidth() { 643 return this.minimumDrawWidth; 644 } 645 646 655 public void setMinimumDrawWidth(int width) { 656 this.minimumDrawWidth = width; 657 } 658 659 667 public int getMaximumDrawWidth() { 668 return this.maximumDrawWidth; 669 } 670 671 680 public void setMaximumDrawWidth(int width) { 681 this.maximumDrawWidth = width; 682 } 683 684 692 public int getMinimumDrawHeight() { 693 return this.minimumDrawHeight; 694 } 695 696 705 public void setMinimumDrawHeight(int height) { 706 this.minimumDrawHeight = height; 707 } 708 709 717 public int getMaximumDrawHeight() { 718 return this.maximumDrawHeight; 719 } 720 721 730 public void setMaximumDrawHeight(int height) { 731 this.maximumDrawHeight = height; 732 } 733 734 740 public double getScaleX() { 741 return this.scaleX; 742 } 743 744 750 public double getScaleY() { 751 return this.scaleY; 752 } 753 754 759 public Point2D getAnchor() { 760 return this.anchor; 761 } 762 763 769 protected void setAnchor(Point2D anchor) { 770 this.anchor = anchor; 771 } 772 773 778 public JPopupMenu getPopupMenu() { 779 return this.popup; 780 } 781 782 787 public void setPopupMenu(JPopupMenu popup) { 788 this.popup = popup; 789 } 790 791 796 public ChartRenderingInfo getChartRenderingInfo() { 797 return this.info; 798 } 799 800 806 public void setMouseZoomable(boolean flag) { 807 setMouseZoomable(flag, true); 808 } 809 810 817 public void setMouseZoomable(boolean flag, boolean fillRectangle) { 818 setDomainZoomable(flag); 819 setRangeZoomable(flag); 820 setFillZoomRectangle(fillRectangle); 821 } 822 823 829 public boolean isDomainZoomable() { 830 return this.domainZoomable; 831 } 832 833 840 public void setDomainZoomable(boolean flag) { 841 if (flag) { 842 Plot plot = this.chart.getPlot(); 843 if (plot instanceof Zoomable) { 844 Zoomable z = (Zoomable) plot; 845 this.domainZoomable = flag && (z.isDomainZoomable()); 846 } 847 } 848 else { 849 this.domainZoomable = false; 850 } 851 } 852 853 859 public boolean isRangeZoomable() { 860 return this.rangeZoomable; 861 } 862 863 868 public void setRangeZoomable(boolean flag) { 869 if (flag) { 870 Plot plot = this.chart.getPlot(); 871 if (plot instanceof Zoomable) { 872 Zoomable z = (Zoomable) plot; 873 this.rangeZoomable = flag && (z.isRangeZoomable()); 874 } 875 } 876 else { 877 this.rangeZoomable = false; 878 } 879 } 880 881 887 public boolean getFillZoomRectangle() { 888 return this.fillZoomRectangle; 889 } 890 891 897 public void setFillZoomRectangle(boolean flag) { 898 this.fillZoomRectangle = flag; 899 } 900 901 907 public int getZoomTriggerDistance() { 908 return this.zoomTriggerDistance; 909 } 910 911 917 public void setZoomTriggerDistance(int distance) { 918 this.zoomTriggerDistance = distance; 919 } 920 921 927 public boolean getHorizontalAxisTrace() { 928 return this.horizontalAxisTrace; 929 } 930 931 |