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
|
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/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.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* ---------------
* XYPlotTest.java
* ---------------
* (C) Copyright 2003-2014, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 26-Mar-2003 : Version 1 (DG);
* 22-Mar-2004 : Added new cloning test (DG);
* 05-Oct-2004 : Strengthened test for clone independence (DG);
* 22-Nov-2006 : Added quadrant fields to equals() and clone() tests (DG);
* 09-Jan-2007 : Mark and comment out testGetDatasetCount() (DG);
* 05-Feb-2007 : Added testAddDomainMarker() and testAddRangeMarker() (DG);
* 07-Feb-2007 : Added test1654215() (DG);
* 24-May-2007 : Added testDrawSeriesWithZeroItems() (DG);
* 07-Apr-2008 : Added testRemoveDomainMarker() and
* testRemoveRangeMarker() (DG);
* 10-May-2009 : Extended testEquals(), added testCloning3() (DG);
* 06-Jul-2009 : Added testBug2817504() (DG);
*
*/
package org.jfree.chart.plot;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotEquals;
import org.junit.Test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.TestUtilities;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.MarkerChangeListener;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.util.DefaultShadowGenerator;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.date.MonthConstants;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleInsets;
/**
* Tests for the {@link XYPlot} class.
*/
public class XYPlotTest {
/**
* Added this test in response to a bug report.
*/
public void testGetDatasetCount() {
XYPlot plot = new XYPlot();
assertEquals(0, plot.getDatasetCount());
}
/**
* Some checks for the equals() method.
*/
@Test
public void testEquals() {
XYPlot plot1 = new XYPlot();
XYPlot plot2 = new XYPlot();
assertTrue(plot1.equals(plot2));
// orientation...
plot1.setOrientation(PlotOrientation.HORIZONTAL);
assertFalse(plot1.equals(plot2));
plot2.setOrientation(PlotOrientation.HORIZONTAL);
assertTrue(plot1.equals(plot2));
// axisOffset...
plot1.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
assertFalse(plot1.equals(plot2));
plot2.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
assertTrue(plot1.equals(plot2));
// domainAxis...
plot1.setDomainAxis(new NumberAxis("Domain Axis"));
assertFalse(plot1.equals(plot2));
plot2.setDomainAxis(new NumberAxis("Domain Axis"));
assertTrue(plot1.equals(plot2));
// domainAxisLocation...
plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
assertFalse(plot1.equals(plot2));
plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
assertTrue(plot1.equals(plot2));
// secondary DomainAxes...
plot1.setDomainAxis(11, new NumberAxis("Secondary Domain Axis"));
assertFalse(plot1.equals(plot2));
plot2.setDomainAxis(11, new NumberAxis("Secondary Domain Axis"));
assertTrue(plot1.equals(plot2));
// secondary DomainAxisLocations...
plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
assertFalse(plot1.equals(plot2));
plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
assertTrue(plot1.equals(plot2));
// rangeAxis...
plot1.setRangeAxis(new NumberAxis("Range Axis"));
assertFalse(plot1.equals(plot2));
plot2.setRangeAxis(new NumberAxis("Range Axis"));
assertTrue(plot1.equals(plot2));
// rangeAxisLocation...
plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
assertFalse(plot1.equals(plot2));
plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
assertTrue(plot1.equals(plot2));
// secondary RangeAxes...
plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
assertFalse(plot1.equals(plot2));
plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
assertTrue(plot1.equals(plot2));
// secondary RangeAxisLocations...
plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
assertFalse(plot1.equals(plot2));
plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
assertTrue(plot1.equals(plot2));
// secondary DatasetDomainAxisMap...
plot1.mapDatasetToDomainAxis(11, 11);
assertFalse(plot1.equals(plot2));
plot2.mapDatasetToDomainAxis(11, 11);
assertTrue(plot1.equals(plot2));
// secondaryDatasetRangeAxisMap...
plot1.mapDatasetToRangeAxis(11, 11);
assertFalse(plot1.equals(plot2));
plot2.mapDatasetToRangeAxis(11, 11);
assertTrue(plot1.equals(plot2));
// renderer
plot1.setRenderer(new DefaultXYItemRenderer());
assertFalse(plot1.equals(plot2));
plot2.setRenderer(new DefaultXYItemRenderer());
assertTrue(plot1.equals(plot2));
// secondary renderers
plot1.setRenderer(11, new DefaultXYItemRenderer());
assertFalse(plot1.equals(plot2));
plot2.setRenderer(11, new DefaultXYItemRenderer());
assertTrue(plot1.equals(plot2));
// domainGridlinesVisible
plot1.setDomainGridlinesVisible(false);
assertFalse(plot1.equals(plot2));
plot2.setDomainGridlinesVisible(false);
assertTrue(plot1.equals(plot2));
// domainGridlineStroke
Stroke stroke = new BasicStroke(2.0f);
plot1.setDomainGridlineStroke(stroke);
assertFalse(plot1.equals(plot2));
plot2.setDomainGridlineStroke(stroke);
assertTrue(plot1.equals(plot2));
// domainGridlinePaint
plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.red));
assertFalse(plot1.equals(plot2));
plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.red));
assertTrue(plot1.equals(plot2));
// rangeGridlinesVisible
plot1.setRangeGridlinesVisible(false);
assertFalse(plot1.equals(plot2));
plot2.setRangeGridlinesVisible(false);
assertTrue(plot1.equals(plot2));
// rangeGridlineStroke
plot1.setRangeGridlineStroke(stroke);
assertFalse(plot1.equals(plot2));
plot2.setRangeGridlineStroke(stroke);
assertTrue(plot1.equals(plot2));
// rangeGridlinePaint
plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
3.0f, 4.0f, Color.red));
assertFalse(plot1.equals(plot2));
plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
3.0f, 4.0f, Color.red));
assertTrue(plot1.equals(plot2));
// rangeZeroBaselineVisible
plot1.setRangeZeroBaselineVisible(true);
assertFalse(plot1.equals(plot2));
plot2.setRangeZeroBaselineVisible(true);
assertTrue(plot1.equals(plot2));
// rangeZeroBaselineStroke
plot1.setRangeZeroBaselineStroke(stroke);
assertFalse(plot1.equals(plot2));
plot2.setRangeZeroBaselineStroke(stroke);
assertTrue(plot1.equals(plot2));
// rangeZeroBaselinePaint
plot1.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
3.0f, 4.0f, Color.red));
assertFalse(plot1.equals(plot2));
plot2.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
3.0f, 4.0f, Color.red));
assertTrue(plot1.equals(plot2));
// rangeCrosshairVisible
plot1.setRangeCrosshairVisible(true);
assertFalse(plot1.equals(plot2));
plot2.setRangeCrosshairVisible(true);
assertTrue(plot1.equals(plot2));
// rangeCrosshairValue
plot1.setRangeCrosshairValue(100.0);
assertFalse(plot1.equals(plot2));
plot2.setRangeCrosshairValue(100.0);
assertTrue(plot1.equals(plot2));
// rangeCrosshairStroke
plot1.setRangeCrosshairStroke(stroke);
assertFalse(plot1.equals(plot2));
plot2.setRangeCrosshairStroke(stroke);
assertTrue(plot1.equals(plot2));
// rangeCrosshairPaint
plot1.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.pink,
3.0f, 4.0f, Color.red));
assertFalse(plot1.equals(plot2));
plot2.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.pink,
3.0f, 4.0f, Color.red));
assertTrue(plot1.equals(plot2));
// rangeCrosshairLockedOnData
plot1.setRangeCrosshairLockedOnData(false);
assertFalse(plot1.equals(plot2));
plot2.setRangeCrosshairLockedOnData(false);
assertTrue(plot1.equals(plot2));
// range markers
plot1.addRangeMarker(new ValueMarker(4.0));
assertFalse(plot1.equals(plot2));
plot2.addRangeMarker(new ValueMarker(4.0));
assertTrue(plot1.equals(plot2));
// secondary range markers
plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
assertFalse(plot1.equals(plot2));
plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
assertTrue(plot1.equals(plot2));
plot1.addRangeMarker(1, new ValueMarker(99.0), Layer.BACKGROUND);
assertFalse(plot1.equals(plot2));
plot2.addRangeMarker(1, new ValueMarker(99.0), Layer.BACKGROUND);
assertTrue(plot1.equals(plot2));
// fixed legend items
plot1.setFixedLegendItems(new LegendItemCollection());
assertFalse(plot1.equals(plot2));
plot2.setFixedLegendItems(new LegendItemCollection());
assertTrue(plot1.equals(plot2));
// weight
plot1.setWeight(3);
assertFalse(plot1.equals(plot2));
plot2.setWeight(3);
assertTrue(plot1.equals(plot2));
// quadrant origin
plot1.setQuadrantOrigin(new Point2D.Double(12.3, 45.6));
assertFalse(plot1.equals(plot2));
plot2.setQuadrantOrigin(new Point2D.Double(12.3, 45.6));
assertTrue(plot1.equals(plot2));
// quadrant paint
plot1.setQuadrantPaint(0, new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
assertFalse(plot1.equals(plot2));
plot2.setQuadrantPaint(0, new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
assertTrue(plot1.equals(plot2));
plot1.setQuadrantPaint(1, new GradientPaint(2.0f, 3.0f, Color.red,
4.0f, 5.0f, Color.blue));
assertFalse(plot1.equals(plot2));
plot2.setQuadrantPaint(1, new GradientPaint(2.0f, 3.0f, Color.red,
4.0f, 5.0f, Color.blue));
assertTrue(plot1.equals(plot2));
plot1.setQuadrantPaint(2, new GradientPaint(3.0f, 4.0f, Color.red,
5.0f, 6.0f, Color.blue));
assertFalse(plot1.equals(plot2));
plot2.setQuadrantPaint(2, new GradientPaint(3.0f, 4.0f, Color.red,
5.0f, 6.0f, Color.blue));
assertTrue(plot1.equals(plot2));
plot1.setQuadrantPaint(3, new GradientPaint(4.0f, 5.0f, Color.red,
6.0f, 7.0f, Color.blue));
assertFalse(plot1.equals(plot2));
plot2.setQuadrantPaint(3, new GradientPaint(4.0f, 5.0f, Color.red,
6.0f, 7.0f, Color.blue));
assertTrue(plot1.equals(plot2));
plot1.setDomainTickBandPaint(Color.red);
assertFalse(plot1.equals(plot2));
plot2.setDomainTickBandPaint(Color.red);
assertTrue(plot1.equals(plot2));
plot1.setRangeTickBandPaint(Color.blue);
assertFalse(plot1.equals(plot2));
plot2.setRangeTickBandPaint(Color.blue);
assertTrue(plot1.equals(plot2));
plot1.setDomainMinorGridlinesVisible(true);
assertFalse(plot1.equals(plot2));
plot2.setDomainMinorGridlinesVisible(true);
assertTrue(plot1.equals(plot2));
plot1.setDomainMinorGridlinePaint(Color.red);
assertFalse(plot1.equals(plot2));
plot2.setDomainMinorGridlinePaint(Color.red);
assertTrue(plot1.equals(plot2));
plot1.setDomainGridlineStroke(new BasicStroke(1.1f));
assertFalse(plot1.equals(plot2));
plot2.setDomainGridlineStroke(new BasicStroke(1.1f));
assertTrue(plot1.equals(plot2));
plot1.setRangeMinorGridlinesVisible(true);
assertFalse(plot1.equals(plot2));
plot2.setRangeMinorGridlinesVisible(true);
assertTrue(plot1.equals(plot2));
plot1.setRangeMinorGridlinePaint(Color.blue);
assertFalse(plot1.equals(plot2));
plot2.setRangeMinorGridlinePaint(Color.blue);
assertTrue(plot1.equals(plot2));
plot1.setRangeMinorGridlineStroke(new BasicStroke(1.23f));
assertFalse(plot1.equals(plot2));
plot2.setRangeMinorGridlineStroke(new BasicStroke(1.23f));
assertTrue(plot1.equals(plot2));
List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
new Integer(1)});
plot1.mapDatasetToDomainAxes(0, axisIndices);
assertFalse(plot1.equals(plot2));
plot2.mapDatasetToDomainAxes(0, axisIndices);
assertTrue(plot1.equals(plot2));
plot1.mapDatasetToRangeAxes(0, axisIndices);
assertFalse(plot1.equals(plot2));
plot2.mapDatasetToRangeAxes(0, axisIndices);
assertTrue(plot1.equals(plot2));
// shadowGenerator
plot1.setShadowGenerator(new DefaultShadowGenerator(5, Color.gray,
0.6f, 4, -Math.PI / 4));
assertFalse(plot1.equals(plot2));
plot2.setShadowGenerator(new DefaultShadowGenerator(5, Color.gray,
0.6f, 4, -Math.PI / 4));
assertTrue(plot1.equals(plot2));
plot1.setShadowGenerator(null);
assertFalse(plot1.equals(plot2));
plot2.setShadowGenerator(null);
assertTrue(plot1.equals(plot2));
LegendItemCollection lic1 = new LegendItemCollection();
lic1.add(new LegendItem("XYZ", Color.red));
plot1.setFixedLegendItems(lic1);
assertFalse(plot1.equals(plot2));
LegendItemCollection lic2 = new LegendItemCollection();
lic2.add(new LegendItem("XYZ", Color.red));
plot2.setFixedLegendItems(lic2);
assertTrue(plot1.equals(plot2));
}
/**
* This test covers a flaw in the ObjectList equals() method.
*/
@Test
public void testEquals_ObjectList() {
XYPlot p1 = new XYPlot();
p1.setDomainAxis(new NumberAxis("A"));
XYPlot p2 = new XYPlot();
p2.setDomainAxis(new NumberAxis("A"));
assertEquals(p1, p2);
p2.setDomainAxis(1, new NumberAxis("B"));
assertNotEquals(p1, p2);
}
/**
* This test covers a flaw in the ObjectList equals() method.
*/
@Test
public void testEquals_ObjectList2() {
XYPlot p1 = new XYPlot();
p1.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
XYPlot p2 = new XYPlot();
p2.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
assertEquals(p1, p2);
p2.setDomainAxisLocation(1, AxisLocation.TOP_OR_LEFT);
assertNotEquals(p1, p2);
}
/**
* This test covers a flaw in the ObjectList equals() method.
*/
@Test
public void testEquals_ObjectList3() {
XYPlot p1 = new XYPlot();
p1.setRangeAxis(new NumberAxis("A"));
XYPlot p2 = new XYPlot();
p2.setRangeAxis(new NumberAxis("A"));
assertEquals(p1, p2);
p2.setRangeAxis(1, new NumberAxis("B"));
assertNotEquals(p1, p2);
}
/**
* This test covers a flaw in the ObjectList equals() method.
*/
@Test
public void testEquals_ObjectList4() {
XYPlot p1 = new XYPlot();
p1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
XYPlot p2 = new XYPlot();
p2.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
assertEquals(p1, p2);
p2.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
assertNotEquals(p1, p2);
}
/**
* This test covers a flaw in the ObjectList equals() method.
*/
@Test
public void testEquals_ObjectList5() {
XYPlot p1 = new XYPlot();
p1.setRenderer(new XYBarRenderer());
XYPlot p2 = new XYPlot();
p2.setRenderer(new XYBarRenderer());
assertEquals(p1, p2);
p2.setRenderer(1, new XYLineAndShapeRenderer());
assertNotEquals(p1, p2);
}
/**
* Confirm that basic cloning works.
*/
@Test
public void testCloning() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot();
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
}
/**
* Tests cloning for a more complex plot.
*/
@Test
public void testCloning2() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
new NumberAxis("Range Axis"), new StandardXYItemRenderer());
p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
new Integer(1)});
p1.mapDatasetToDomainAxes(0, axisIndices);
p1.mapDatasetToRangeAxes(0, axisIndices);
p1.setRenderer(1, new XYBarRenderer());
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
}
/**
* Tests cloning for a plot where the fixed legend items have been
* specified.
*/
@Test
public void testCloning3() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
new NumberAxis("Range Axis"), new StandardXYItemRenderer());
LegendItemCollection c1 = new LegendItemCollection();
p1.setFixedLegendItems(c1);
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
// verify independence of fixed legend item collection
c1.add(new LegendItem("X"));
assertFalse(p1.equals(p2));
}
/**
* Tests cloning to ensure that the cloned plot is registered as a listener
* on the cloned renderer.
*/
@Test
public void testCloning4() throws CloneNotSupportedException {
XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
new NumberAxis("Range Axis"), r1);
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
// verify that the plot is listening to the cloned renderer
XYLineAndShapeRenderer r2 = (XYLineAndShapeRenderer) p2.getRenderer();
assertTrue(r2.hasListener(p2));
}
/**
* Confirm that cloning captures the quadrantOrigin field.
*/
@Test
public void testCloning_QuadrantOrigin() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot();
Point2D p = new Point2D.Double(1.2, 3.4);
p1.setQuadrantOrigin(p);
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
assertTrue(p2.getQuadrantOrigin() != p);
}
/**
* Confirm that cloning captures the quadrantOrigin field.
*/
@Test
public void testCloning_QuadrantPaint() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot();
p1.setQuadrantPaint(3, new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
// check for independence
p1.setQuadrantPaint(1, Color.red);
assertFalse(p1.equals(p2));
p2.setQuadrantPaint(1, Color.red);
assertTrue(p1.equals(p2));
}
/**
* Renderers that belong to the plot are being cloned but they are
* retaining a reference to the original plot.
*/
@Test
public void testBug2817504() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot();
XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
p1.setRenderer(r1);
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
// check for independence
XYLineAndShapeRenderer r2 = (XYLineAndShapeRenderer) p2.getRenderer();
assertTrue(r2.getPlot() == p2);
}
/**
* Tests the independence of the clones.
*/
@Test
public void testCloneIndependence() throws CloneNotSupportedException {
XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
new NumberAxis("Range Axis"), new StandardXYItemRenderer());
p1.setDomainAxis(1, new NumberAxis("Domain Axis 2"));
p1.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
p1.setRenderer(1, new XYBarRenderer());
XYPlot p2 = (XYPlot) p1.clone();
assertTrue(p1.equals(p2));
p1.getDomainAxis().setLabel("Label");
assertFalse(p1.equals(p2));
p2.getDomainAxis().setLabel("Label");
assertTrue(p1.equals(p2));
p1.getDomainAxis(1).setLabel("S1");
assertFalse(p1.equals(p2));
p2.getDomainAxis(1).setLabel("S1");
assertTrue(p1.equals(p2));
p1.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
assertFalse(p1.equals(p2));
p2.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
assertTrue(p1.equals(p2));
p1.mapDatasetToDomainAxis(2, 1);
assertFalse(p1.equals(p2));
p2.mapDatasetToDomainAxis(2, 1);
assertTrue(p1.equals(p2));
p1.getRangeAxis().setLabel("Label");
assertFalse(p1.equals(p2));
p2.getRangeAxis().setLabel("Label");
assertTrue(p1.equals(p2));
p1.getRangeAxis(1).setLabel("S1");
assertFalse(p1.equals(p2));
p2.getRangeAxis(1).setLabel("S1");
assertTrue(p1.equals(p2));
p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
assertFalse(p1.equals(p2));
p2.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
assertTrue(p1.equals(p2));
p1.mapDatasetToRangeAxis(2, 1);
assertFalse(p1.equals(p2));
p2.mapDatasetToRangeAxis(2, 1);
assertTrue(p1.equals(p2));
p1.getRenderer().setOutlinePaint(Color.cyan);
assertFalse(p1.equals(p2));
p2.getRenderer().setOutlinePaint(Color.cyan);
assertTrue(p1.equals(p2));
p1.getRenderer(1).setOutlinePaint(Color.red);
assertFalse(p1.equals(p2));
p2.getRenderer(1).setOutlinePaint(Color.red);
assertTrue(p1.equals(p2));
}
/**
* Setting a null renderer should be allowed, but is generating a null
* pointer exception in 0.9.7.
*/
@Test
public void testSetNullRenderer() {
boolean failed = false;
try {
XYPlot plot = new XYPlot(null, new NumberAxis("X"),
new NumberAxis("Y"), null);
plot.setRenderer(null);
}
catch (Exception e) {
failed = true;
}
assertTrue(!failed);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
@Test
public void testSerialization1() {
XYDataset data = new XYSeriesCollection();
NumberAxis domainAxis = new NumberAxis("Domain");
NumberAxis rangeAxis = new NumberAxis("Range");
StandardXYItemRenderer renderer = new StandardXYItemRenderer();
XYPlot p1 = new XYPlot(data, domainAxis, rangeAxis, renderer);
XYPlot p2 = (XYPlot) TestUtilities.serialised(p1);
assertEquals(p1, p2);
}
/**
* Serialize an instance, restore it, and check for equality. This test
* uses a {@link DateAxis} and a {@link StandardXYToolTipGenerator}.
*/
@Test
public void testSerialization2() {
IntervalXYDataset data1 = createDataset1();
XYItemRenderer renderer1 = new XYBarRenderer(0.20);
renderer1.setBaseToolTipGenerator(
StandardXYToolTipGenerator.getTimeSeriesInstance());
XYPlot p1 = new XYPlot(data1, new DateAxis("Date"), null, renderer1);
XYPlot p2 = (XYPlot) TestUtilities.serialised(p1);
assertEquals(p1, p2);
}
/**
* Problem to reproduce a bug in serialization. The bug (first reported
* against the {@link org.jfree.chart.plot.CategoryPlot} class) is a null
* pointer exception that occurs when drawing a plot after deserialization.
* It is caused by four temporary storage structures (axesAtTop,
* axesAtBottom, axesAtLeft and axesAtRight - all initialized as empty
* lists in the constructor) not being initialized by the readObject()
* method following deserialization. This test has been written to
* reproduce the bug (now fixed).
*/
@Test
public void testSerialization3() {
XYSeriesCollection dataset = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
"Domain Axis", "Range Axis", dataset);
JFreeChart chart2 = (JFreeChart) TestUtilities.serialised(chart);
assertEquals(chart, chart2);
try {
chart2.createBufferedImage(300, 200);
}
catch (Exception e) {
fail("No exception should be thrown.");
}
}
/**
* A test to reproduce a bug in serialization: the domain and/or range
* markers for a plot are not being serialized.
*/
@Test
public void testSerialization4() {
XYSeriesCollection dataset = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
"Domain Axis", "Range Axis", dataset);
XYPlot plot = (XYPlot) chart.getPlot();
plot.addDomainMarker(new ValueMarker(1.0), Layer.FOREGROUND);
plot.addDomainMarker(new IntervalMarker(2.0, 3.0), Layer.BACKGROUND);
plot.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
plot.addRangeMarker(new IntervalMarker(5.0, 6.0), Layer.BACKGROUND);
JFreeChart chart2 = (JFreeChart) TestUtilities.serialised(chart);
assertEquals(chart, chart2);
try {
chart2.createBufferedImage(300, 200);
}
catch (Exception e) {
fail("No exception should be thrown.");
}
}
/**
* Tests a bug where the plot is no longer registered as a listener
* with the dataset(s) and axes after deserialization. See patch 1209475
* at SourceForge.
*/
@Test
public void testSerialization5() {
XYSeriesCollection dataset1 = new XYSeriesCollection();
NumberAxis domainAxis1 = new NumberAxis("Domain 1");
NumberAxis rangeAxis1 = new NumberAxis("Range 1");
StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
XYPlot p1 = new XYPlot(dataset1, domainAxis1, rangeAxis1, renderer1);
NumberAxis domainAxis2 = new NumberAxis("Domain 2");
NumberAxis rangeAxis2 = new NumberAxis("Range 2");
StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
XYSeriesCollection dataset2 = new XYSeriesCollection();
p1.setDataset(1, dataset2);
p1.setDomainAxis(1, domainAxis2);
p1.setRangeAxis(1, rangeAxis2);
p1.setRenderer(1, renderer2);
XYPlot p2 = (XYPlot) TestUtilities.serialised(p1);
assertEquals(p1, p2);
// now check that all datasets, renderers and axes are being listened
// too...
NumberAxis domainAxisA = (NumberAxis) p2.getDomainAxis(0);
NumberAxis rangeAxisA = (NumberAxis) p2.getRangeAxis(0);
XYSeriesCollection datasetA = (XYSeriesCollection) p2.getDataset(0);
StandardXYItemRenderer rendererA
= (StandardXYItemRenderer) p2.getRenderer(0);
NumberAxis domainAxisB = (NumberAxis) p2.getDomainAxis(1);
NumberAxis rangeAxisB = (NumberAxis) p2.getRangeAxis(1);
XYSeriesCollection datasetB = (XYSeriesCollection) p2.getDataset(1);
StandardXYItemRenderer rendererB
= (StandardXYItemRenderer) p2.getRenderer(1);
assertTrue(datasetA.hasListener(p2));
assertTrue(domainAxisA.hasListener(p2));
assertTrue(rangeAxisA.hasListener(p2));
assertTrue(rendererA.hasListener(p2));
assertTrue(datasetB.hasListener(p2));
assertTrue(domainAxisB.hasListener(p2));
assertTrue(rangeAxisB.hasListener(p2));
assertTrue(rendererB.hasListener(p2));
}
/**
* Some checks for the getRendererForDataset() method.
*/
@Test
public void testGetRendererForDataset() {
XYDataset d0 = new XYSeriesCollection();
XYDataset d1 = new XYSeriesCollection();
XYDataset d2 = new XYSeriesCollection();
XYDataset d3 = new XYSeriesCollection(); // not used by plot
XYItemRenderer r0 = new XYLineAndShapeRenderer();
XYItemRenderer r2 = new XYLineAndShapeRenderer();
XYPlot plot = new XYPlot();
plot.setDataset(0, d0);
plot.setDataset(1, d1);
plot.setDataset(2, d2);
plot.setRenderer(0, r0);
// no renderer 1
plot.setRenderer(2, r2);
assertEquals(r0, plot.getRendererForDataset(d0));
assertEquals(r0, plot.getRendererForDataset(d1));
assertEquals(r2, plot.getRendererForDataset(d2));
assertEquals(null, plot.getRendererForDataset(d3));
assertEquals(null, plot.getRendererForDataset(null));
}
/**
* Some checks for the getLegendItems() method.
*/
@Test
public void testGetLegendItems() {
// check the case where there is a secondary dataset that doesn't
// have a renderer (i.e. falls back to renderer 0)
XYDataset d0 = createDataset1();
XYDataset d1 = createDataset2();
XYItemRenderer r0 = new XYLineAndShapeRenderer();
XYPlot plot = new XYPlot();
plot.setDataset(0, d0);
plot.setDataset(1, d1);
plot.setRenderer(0, r0);
LegendItemCollection items = plot.getLegendItems();
assertEquals(2, items.getItemCount());
}
/**
* Creates a sample dataset.
*
* @return Series 1.
*/
private IntervalXYDataset createDataset1() {
// create dataset 1...
TimeSeries series1 = new TimeSeries("Series 1", Day.class);
series1.add(new Day(1, MonthConstants.MARCH, 2002), 12353.3);
series1.add(new Day(2, MonthConstants.MARCH, 2002), 13734.4);
series1.add(new Day(3, MonthConstants.MARCH, 2002), 14525.3);
series1.add(new Day(4, MonthConstants.MARCH, 2002), 13984.3);
series1.add(new Day(5, MonthConstants.MARCH, 2002), 12999.4);
series1.add(new Day(6, MonthConstants.MARCH, 2002), 14274.3);
series1.add(new Day(7, MonthConstants.MARCH, 2002), 15943.5);
series1.add(new Day(8, MonthConstants.MARCH, 2002), 14845.3);
series1.add(new Day(9, MonthConstants.MARCH, 2002), 14645.4);
series1.add(new Day(10, MonthConstants.MARCH, 2002), 16234.6);
series1.add(new Day(11, MonthConstants.MARCH, 2002), 17232.3);
series1.add(new Day(12, MonthConstants.MARCH, 2002), 14232.2);
series1.add(new Day(13, MonthConstants.MARCH, 2002), 13102.2);
series1.add(new Day(14, MonthConstants.MARCH, 2002), 14230.2);
series1.add(new Day(15, MonthConstants.MARCH, 2002), 11235.2);
TimeSeriesCollection collection = new TimeSeriesCollection(series1);
return collection;
}
/**
* Creates a sample dataset.
*
* @return A sample dataset.
*/
private XYDataset createDataset2() {
// create dataset 1...
XYSeries series = new XYSeries("Series 2");
XYSeriesCollection collection = new XYSeriesCollection(series);
return collection;
}
/**
* A test for a bug where setting the renderer doesn't register the plot
* as a RendererChangeListener.
*/
@Test
public void testSetRenderer() {
XYPlot plot = new XYPlot();
XYItemRenderer renderer = new XYLineAndShapeRenderer();
plot.setRenderer(renderer);
// now make a change to the renderer and see if it triggers a plot
// change event...
MyPlotChangeListener listener = new MyPlotChangeListener();
plot.addChangeListener(listener);
renderer.setSeriesPaint(0, Color.black);
assertTrue(listener.getEvent() != null);
}
/**
* Some checks for the removeAnnotation() method.
*/
@Test
public void testRemoveAnnotation() {
XYPlot plot = new XYPlot();
XYTextAnnotation a1 = new XYTextAnnotation("X", 1.0, 2.0);
XYTextAnnotation a2 = new XYTextAnnotation("X", 3.0, 4.0);
XYTextAnnotation a3 = new XYTextAnnotation("X", 1.0, 2.0);
plot.addAnnotation(a1);
plot.addAnnotation(a2);
plot.addAnnotation(a3);
plot.removeAnnotation(a2);
XYTextAnnotation x = (XYTextAnnotation) plot.getAnnotations().get(0);
assertEquals(x, a1);
// now remove a3, but since a3.equals(a1), this will in fact remove
// a1...
assertTrue(a1.equals(a3));
plot.removeAnnotation(a3); // actually removes a1
x = (XYTextAnnotation) plot.getAnnotations().get(0);
assertEquals(x, a3);
}
/**
* Some tests for the addDomainMarker() method(s).
*/
@Test
public void testAddDomainMarker() {
XYPlot plot = new XYPlot();
Marker m = new ValueMarker(1.0);
plot.addDomainMarker(m);
List listeners = Arrays.asList(m.getListeners(
MarkerChangeListener.class));
assertTrue(listeners.contains(plot));
plot.clearDomainMarkers();
listeners = Arrays.asList(m.getListeners(MarkerChangeListener.class));
assertFalse(listeners.contains(plot));
}
/**
* Some tests for the addRangeMarker() method(s).
*/
@Test
public void testAddRangeMarker() {
XYPlot plot = new XYPlot();
Marker m = new ValueMarker(1.0);
plot.addRangeMarker(m);
List listeners = Arrays.asList(m.getListeners(
MarkerChangeListener.class));
assertTrue(listeners.contains(plot));
plot.clearRangeMarkers();
listeners = Arrays.asList(m.getListeners(MarkerChangeListener.class));
assertFalse(listeners.contains(plot));
}
/**
* A test for bug 1654215 (where a renderer is added to the plot without
* a corresponding dataset and it throws an exception at drawing time).
*/
@Test
public void test1654215() {
DefaultXYDataset dataset = new DefaultXYDataset();
JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
dataset, PlotOrientation.VERTICAL, true, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setRenderer(1, new XYLineAndShapeRenderer());
try {
BufferedImage image = new BufferedImage(200 , 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
g2.dispose();
}
catch (Exception e) {
fail("No exception should be thrown.");
}
}
/**
* A test for drawing range grid lines when there is no primary renderer.
* In 1.0.4, this is throwing a NullPointerException.
*/
@Test
public void testDrawRangeGridlines() {
DefaultXYDataset dataset = new DefaultXYDataset();
JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
dataset, PlotOrientation.VERTICAL, true, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setRenderer(null);
try {
BufferedImage image = new BufferedImage(200 , 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
g2.dispose();
}
catch (Exception e) {
fail("No exception should be thrown.");
}
}
/**
* A test for drawing a plot where a series has zero items. With
* JFreeChart 1.0.5+cvs this was throwing an exception at one point.
*/
@Test
public void testDrawSeriesWithZeroItems() {
DefaultXYDataset dataset = new DefaultXYDataset();
dataset.addSeries("Series 1", new double[][] {{1.0, 2.0}, {3.0, 4.0}});
dataset.addSeries("Series 2", new double[][] {{}, {}});
JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
dataset, PlotOrientation.VERTICAL, true, false, false);
try {
BufferedImage image = new BufferedImage(200 , 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
g2.dispose();
}
catch (Exception e) {
fail("No exception should be thrown.");
}
}
/**
* Check that removing a marker that isn't assigned to the plot returns
* false.
*/
@Test
public void testRemoveDomainMarker() {
XYPlot plot = new XYPlot();
assertFalse(plot.removeDomainMarker(new ValueMarker(0.5)));
}
/**
* Check that removing a marker that isn't assigned to the plot returns
* false.
*/
@Test
public void testRemoveRangeMarker() {
XYPlot plot = new XYPlot();
assertFalse(plot.removeRangeMarker(new ValueMarker(0.5)));
}
/**
* Some tests for the getDomainAxisForDataset() method.
*/
@Test
public void testGetDomainAxisForDataset() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
assertEquals(xAxis, plot.getDomainAxisForDataset(0));
// should get IllegalArgumentException for negative index
boolean pass = false;
try {
plot.getDomainAxisForDataset(-1);
}
catch (IllegalArgumentException e) {
pass = true;
}
assertTrue(pass);
// if multiple axes are mapped, the first in the list should be
// returned...
NumberAxis xAxis2 = new NumberAxis("X2");
plot.setDomainAxis(1, xAxis2);
assertEquals(xAxis, plot.getDomainAxisForDataset(0));
plot.mapDatasetToDomainAxis(0, 1);
assertEquals(xAxis2, plot.getDomainAxisForDataset(0));
List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
new Integer(1)});
plot.mapDatasetToDomainAxes(0, axisIndices);
assertEquals(xAxis, plot.getDomainAxisForDataset(0));
axisIndices = Arrays.asList(new Integer[] {new Integer(1),
new Integer(2)});
plot.mapDatasetToDomainAxes(0, axisIndices);
assertEquals(xAxis2, plot.getDomainAxisForDataset(0));
}
/**
* Some tests for the getRangeAxisForDataset() method.
*/
@Test
public void testGetRangeAxisForDataset() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
assertEquals(yAxis, plot.getRangeAxisForDataset(0));
// should get IllegalArgumentException for negative index
boolean pass = false;
try {
plot.getRangeAxisForDataset(-1);
}
catch (IllegalArgumentException e) {
pass = true;
}
assertTrue(pass);
// if multiple axes are mapped, the first in the list should be
// returned...
NumberAxis yAxis2 = new NumberAxis("Y2");
plot.setRangeAxis(1, yAxis2);
assertEquals(yAxis, plot.getRangeAxisForDataset(0));
plot.mapDatasetToRangeAxis(0, 1);
assertEquals(yAxis2, plot.getRangeAxisForDataset(0));
List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
new Integer(1)});
plot.mapDatasetToRangeAxes(0, axisIndices);
assertEquals(yAxis, plot.getRangeAxisForDataset(0));
axisIndices = Arrays.asList(new Integer[] {new Integer(1),
new Integer(2)});
plot.mapDatasetToRangeAxes(0, axisIndices);
assertEquals(yAxis2, plot.getRangeAxisForDataset(0));
}
/**
* Datasets are now stored in a Map, and it should be possible to assign
* them an arbitrary key (index).
*/
@Test
public void testDatasetIndices() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
assertEquals(dataset, plot.getDataset(0));
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(new XYSeries("Series in dataset 2"));
// we should be able to give a dataset an arbitrary index
plot.setDataset(99, dataset2);
assertEquals(2, plot.getDatasetCount());
assertEquals(dataset2, plot.getDataset(99));
assertEquals(0, plot.indexOf(dataset));
assertEquals(99, plot.indexOf(dataset2));
}
@Test
public void testAxisIndices() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
assertEquals(xAxis, plot.getDomainAxis(0));
assertEquals(yAxis, plot.getRangeAxis(0));
NumberAxis xAxis2 = new NumberAxis("X2");
plot.setDomainAxis(99, xAxis2);
assertEquals(xAxis2, plot.getDomainAxis(99));
NumberAxis yAxis2 = new NumberAxis("Y2");
plot.setRangeAxis(99, yAxis2);
assertEquals(yAxis2, plot.getRangeAxis(99));
}
@Test
public void testAxisLocationIndices() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
NumberAxis xAxis2 = new NumberAxis("X2");
NumberAxis yAxis2 = new NumberAxis("Y2");
plot.setDomainAxis(99, xAxis2);
plot.setRangeAxis(99, yAxis2);
plot.setDomainAxisLocation(99, AxisLocation.BOTTOM_OR_RIGHT);
assertEquals(AxisLocation.BOTTOM_OR_RIGHT,
plot.getDomainAxisLocation(99));
plot.setRangeAxisLocation(99, AxisLocation.BOTTOM_OR_LEFT);
assertEquals(AxisLocation.BOTTOM_OR_LEFT,
plot.getRangeAxisLocation(99));
}
@Test
public void testRendererIndices() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
assertEquals(renderer, plot.getRenderer(0));
// we should be able to give a renderer an arbitrary index
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
plot.setRenderer(20, renderer2);
assertEquals(2, plot.getRendererCount());
assertEquals(renderer2, plot.getRenderer(20));
assertEquals(20, plot.getIndexOf(renderer2));
}
@Test
public void testGetRendererForDataset2() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
// add a second dataset
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(new XYSeries("Series in dataset 2"));
plot.setDataset(99, dataset2);
// by default, the renderer with index 0 is used
assertEquals(renderer, plot.getRendererForDataset(dataset2));
// add a second renderer with the same index as dataset2, now it will
// be used
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
plot.setRenderer(99, renderer);
assertEquals(renderer2, plot.getRendererForDataset(dataset2));
}
@Test
public void testMapDatasetToDomainAxis() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
NumberAxis xAxis2 = new NumberAxis("X2");
plot.setDomainAxis(11, xAxis2);
// add a second dataset
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(new XYSeries("Series in dataset 2"));
plot.setDataset(99, dataset);
assertEquals(xAxis, plot.getDomainAxisForDataset(99));
// now map the dataset to the second xAxis
plot.mapDatasetToDomainAxis(99, 11);
assertEquals(xAxis2, plot.getDomainAxisForDataset(99));
}
@Test
public void testMapDatasetToRangeAxis() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
NumberAxis yAxis2 = new NumberAxis("Y2");
plot.setRangeAxis(22, yAxis2);
// add a second dataset
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(new XYSeries("Series in dataset 2"));
plot.setDataset(99, dataset);
assertEquals(yAxis, plot.getRangeAxisForDataset(99));
// now map the dataset to the second xAxis
plot.mapDatasetToRangeAxis(99, 22);
assertEquals(yAxis2, plot.getRangeAxisForDataset(99));
}
@Test
public void testDomainMarkerIndices() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
// add a second dataset, plotted against a second x axis
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(new XYSeries("Series in dataset 2"));
plot.setDataset(99, dataset);
NumberAxis xAxis2 = new NumberAxis("X2");
plot.setDomainAxis(1, xAxis2);
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
plot.setRenderer(99, renderer2);
plot.mapDatasetToDomainAxis(99, 1);
ValueMarker xMarker1 = new ValueMarker(123);
plot.addDomainMarker(99, xMarker1, Layer.FOREGROUND);
assertTrue(plot.getDomainMarkers(99, Layer.FOREGROUND).contains(
xMarker1));
}
@Test
public void testRangeMarkerIndices() {
XYDataset dataset = new XYSeriesCollection();
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
XYItemRenderer renderer = new DefaultXYItemRenderer();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
// add a second dataset, plotted against a second axis
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(new XYSeries("Series in dataset 2"));
plot.setDataset(99, dataset);
NumberAxis yAxis2 = new NumberAxis("Y2");
plot.setRangeAxis(1, yAxis2);
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
plot.setRenderer(99, renderer2);
plot.mapDatasetToRangeAxis(99, 1);
ValueMarker yMarker1 = new ValueMarker(123);
plot.addRangeMarker(99, yMarker1, Layer.FOREGROUND);
assertTrue(plot.getRangeMarkers(99, Layer.FOREGROUND).contains(yMarker1));
}
}
|