| 1 108 109 package org.jfree.chart.axis; 110 111 import java.awt.Font ; 112 import java.awt.FontMetrics ; 113 import java.awt.Graphics2D ; 114 import java.awt.font.FontRenderContext ; 115 import java.awt.font.LineMetrics ; 116 import java.awt.geom.Rectangle2D ; 117 import java.io.Serializable ; 118 import java.text.DateFormat ; 119 import java.text.SimpleDateFormat ; 120 import java.util.Calendar ; 121 import java.util.Date ; 122 import java.util.List ; 123 import java.util.TimeZone ; 124 125 import org.jfree.chart.event.AxisChangeEvent; 126 import org.jfree.chart.plot.Plot; 127 import org.jfree.chart.plot.PlotRenderingInfo; 128 import org.jfree.chart.plot.ValueAxisPlot; 129 import org.jfree.data.Range; 130 import org.jfree.data.time.DateRange; 131 import org.jfree.data.time.Month; 132 import org.jfree.data.time.RegularTimePeriod; 133 import org.jfree.data.time.Year; 134 import org.jfree.ui.RectangleEdge; 135 import org.jfree.ui.RectangleInsets; 136 import org.jfree.ui.TextAnchor; 137 import org.jfree.util.ObjectUtilities; 138 139 152 public class DateAxis extends ValueAxis implements Cloneable , Serializable { 153 154 155 private static final long serialVersionUID = -1013460999649007604L; 156 157 158 public static final DateRange DEFAULT_DATE_RANGE = new DateRange(); 159 160 161 public static final double 162 DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS = 2.0; 163 164 165 public static final DateTickUnit DEFAULT_DATE_TICK_UNIT 166 = new DateTickUnit(DateTickUnit.DAY, 1, new SimpleDateFormat ()); 167 168 169 public static final Date DEFAULT_ANCHOR_DATE = new Date (); 170 171 172 private DateTickUnit tickUnit; 173 174 175 private DateFormat dateFormatOverride; 176 177 181 private DateTickMarkPosition tickMarkPosition = DateTickMarkPosition.START; 182 183 187 private static class DefaultTimeline implements Timeline, Serializable { 188 189 196 public long toTimelineValue(long millisecond) { 197 return millisecond; 198 } 199 200 207 public long toTimelineValue(Date date) { 208 return date.getTime(); 209 } 210 211 219 public long toMillisecond(long value) { 220 return value; 221 } 222 223 231 public boolean containsDomainValue(long millisecond) { 232 return true; 233 } 234 235 243 public boolean containsDomainValue(Date date) { 244 return true; 245 } 246 247 256 public boolean containsDomainRange(long from, long to) { 257 return true; 258 } 259 260 269 public boolean containsDomainRange(Date from, Date to) { 270 return true; 271 } 272 273 280 public boolean equals(Object object) { 281 282 if (object == null) { 283 return false; 284 } 285 286 if (object == this) { 287 return true; 288 } 289 290 if (object instanceof DefaultTimeline) { 291 return true; 292 } 293 294 return false; 295 296 } 297 } 298 299 300 private static final Timeline DEFAULT_TIMELINE = new DefaultTimeline(); 301 302 303 private TimeZone timeZone; 304 305 306 private Timeline timeline; 307 308 311 public DateAxis() { 312 this(null); 313 } 314 315 320 public DateAxis(String label) { 321 this(label, TimeZone.getDefault()); 322 } 323 324 334 public DateAxis(String label, TimeZone zone) { 335 super(label, DateAxis.createStandardDateTickUnits(zone)); 336 setTickUnit(DateAxis.DEFAULT_DATE_TICK_UNIT, false, false); 337 setAutoRangeMinimumSize( 338 DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS 339 ); 340 setRange(DEFAULT_DATE_RANGE, false, false); 341 this.dateFormatOverride = null; 342 this.timeZone = zone; 343 this.timeline = DEFAULT_TIMELINE; 344 } 345 346 351 public Timeline getTimeline() { 352 return this.timeline; 353 } 354 355 363 public void setTimeline(Timeline timeline) { 364 if (this.timeline != timeline) { 365 this.timeline = timeline; 366 notifyListeners(new AxisChangeEvent(this)); 367 } 368 } 369 370 375 public DateTickUnit getTickUnit() { 376 return this.tickUnit; 377 } 378 379 386 public void setTickUnit(DateTickUnit unit) { 387 setTickUnit(unit, true, true); 388 } 389 390 397 public void setTickUnit(DateTickUnit unit, boolean notify, 398 boolean turnOffAutoSelection) { 399 400 this.tickUnit = unit; 401 if (turnOffAutoSelection) { 402 setAutoTickUnitSelection(false, false); 403 } 404 if (notify) { 405 notifyListeners(new AxisChangeEvent(this)); 406 } 407 408 } 409 410 416 public DateFormat getDateFormatOverride() { 417 return this.dateFormatOverride; 418 } 419 420 426 public void setDateFormatOverride(DateFormat formatter) { 427 this.dateFormatOverride = formatter; 428 notifyListeners(new AxisChangeEvent(this)); 429 } 430 431 438 public void setRange(Range range) { 439 setRange(range, true, true); 440 } 441 442 453 public void setRange(Range range, boolean turnOffAutoRange, 454 boolean notify) { 455 if (range == null) { 456 throw new IllegalArgumentException ("Null 'range' argument."); 457 } 458 if (!(range instanceof DateRange)) { 461 range = new DateRange(range); 462 } 463 super.setRange(range, turnOffAutoRange, notify); 464 } 465 466 473 public void setRange(Date lower, Date upper) { 474 if (lower.getTime() >= upper.getTime()) { 475 throw new IllegalArgumentException ("Requires 'lower' < 'upper'."); 476 } 477 setRange(new DateRange(lower, upper)); 478 } 479 480 487 public void setRange(double lower, double upper) { 488 if (lower >= upper) { 489 throw new IllegalArgumentException ("Requires 'lower' < 'upper'."); 490 } 491 setRange(new DateRange(lower, upper)); 492 } 493 494 499 public Date getMinimumDate() { 500 501 Date result = null; 502 503 Range range = getRange(); 504 if (range instanceof DateRange) { 505 DateRange r = (DateRange) range; 506 result = r.getLowerDate(); 507 } 508 else { 509 result = new Date ((long) range.getLowerBound()); 510 } 511 512 return result; 513 514 } 515 516 522 public void setMinimumDate(Date date) { 523 setRange(new DateRange(date, getMaximumDate()), true, false); 524 notifyListeners(new AxisChangeEvent(this)); 525 } 526 527 532 public Date getMaximumDate() { 533 534 Date result = null; 535 Range range = getRange(); 536 if (range instanceof DateRange) { 537 DateRange r = (DateRange) range; 538 result = r.getUpperDate(); 539 } 540 else { 541 result = new Date ((long) range.getUpperBound()); 542 } 543 return result; 544 545 } 546 547 553 public void setMaximumDate(Date maximumDate) { 554 setRange(new DateRange(getMinimumDate(), maximumDate), true, false); 555 notifyListeners(new AxisChangeEvent(this)); 556 } 557 558 563 public DateTickMarkPosition getTickMarkPosition() { 564 return this.tickMarkPosition; 565 } 566 567 573 public void setTickMarkPosition(DateTickMarkPosition position) { 574 if (position == null) { 575 throw new IllegalArgumentException ("Null 'position' argument."); 576 } 577 this.tickMarkPosition = position; 578 notifyListeners(new AxisChangeEvent(this)); 579 } 580 581 585 public void configure() { 586 if (isAutoRange()) { 587 autoAdjustRange(); 588 } 589 } 590 591 599 public boolean isHiddenValue(long millis) { 600 return (!this.timeline.containsDomainValue(new Date (millis))); 601 } 602 603 614 public double valueToJava2D(double value, Rectangle2D area, 615 RectangleEdge edge) { 616 617 value = this.timeline.toTimelineValue((long) value); 618 619 DateRange range = (DateRange) getRange(); 620 double axisMin = this.timeline.toTimelineValue(range.getLowerDate()); 621 double axisMax = this.timeline.toTimelineValue(range.getUpperDate()); 622 double result = 0.0; 623 if (RectangleEdge.isTopOrBottom(edge)) { 624 double minX = area.getX(); 625 double maxX = area.getMaxX(); 626 if (isInverted()) { 627 result = maxX + ((value - axisMin) / (axisMax - axisMin)) 628 * (minX - maxX); 629 } 630 else { 631 result = minX + ((value - axisMin) / (axisMax - axisMin)) 632 * (maxX - minX); 633 } 634 } 635 else if (RectangleEdge.isLeftOrRight(edge)) { 636 double minY = area.getMinY(); 637 double maxY = area.getMaxY(); 638 if (isInverted()) { 639 result = minY + (((value - axisMin) / (axisMax - axisMin)) 640 * (maxY - minY)); 641 } 642 else { 643 result = maxY - (((value - axisMin) / (axisMax - axisMin)) 644 * (maxY - minY)); 645 } 646 } 647 return result; 648 649 } 650 651 662 public double dateToJava2D(Date date, Rectangle2D area, 663 RectangleEdge edge) { 664 double value = date.getTime(); 665 return valueToJava2D(value, area, edge); 666 } 667 668 680 public double java2DToValue(double java2DValue, Rectangle2D area, 681 RectangleEdge edge) { 682 683 DateRange range = (DateRange) getRange(); 684 double axisMin = this.timeline.toTimelineValue(range.getLowerDate()); 685 double axisMax = this.timeline.toTimelineValue(range.getUpperDate()); 686 687 double min = 0.0; 688 double max = 0.0; 689 if (RectangleEdge.isTopOrBottom(edge)) { 690 min = area.getX(); 691 max = area.getMaxX(); 692 } 693 else if (RectangleEdge.isLeftOrRight(edge)) { 694 min = area.getMaxY(); 695 max = area.getY(); 696 } 697 698 double result; 699 if (isInverted()) { 700 result = axisMax - ((java2DValue - min) / (max - min) 701 * (axisMax - axisMin)); 702 } 703 else { 704 result = axisMin + ((java2DValue - min) / (max - min) 705 * (axisMax - axisMin)); 706 } 707 708 return this.timeline.toMillisecond((long) result); 709 } 710 711 718 public Date calculateLowestVisibleTickValue(DateTickUnit unit) { 719 return nextStandardDate(getMinimumDate(), unit); 720 } 721 722 729 public Date calculateHighestVisibleTickValue(DateTickUnit unit) { 730 return previousStandardDate(getMaximumDate(), unit); 731 } 732 733 741 protected Date previousStandardDate(Date date, DateTickUnit unit) { 742 743 int milliseconds; 744 int seconds; 745 int minutes; 746 int hours; 747 int days; 748 int months; 749 int years; 750 751 Calendar calendar = Calendar.getInstance(this.timeZone); 752 calendar.setTime(date); 753 int count = unit.getCount(); 754 int current = calendar.get(unit.getCalendarField()); 755 int value = count * (current / count); 756 757 switch (unit.getUnit()) { 758 759 case (DateTickUnit.MILLISECOND) : 760 years = calendar.get(Calendar.YEAR); 761 months = calendar.get(Calendar.MONTH); 762 days = calendar.get(Calendar.DATE); 763 hours = calendar.get(Calendar.HOUR_OF_DAY); 764 minutes = calendar.get(Calendar.MINUTE); 765 seconds = calendar.get(Calendar.SECOND); 766 calendar.set(years, months, days, hours, minutes, seconds); 767 calendar.set(Calendar.MILLISECOND, value); 768 return calendar.getTime(); 769 770 case (DateTickUnit.SECOND) : 771 years = calendar.get(Calendar.YEAR); 772 months = calendar.get(Calendar.MONTH); 773 days = calendar.get(Calendar.DATE); 774 hours = calendar.get(Calendar.HOUR_OF_DAY); 775 minutes = calendar.get(Calendar.MINUTE); 776 if (this.tickMarkPosition == DateTickMarkPosition.START) { 777 milliseconds = 0; 778 } 779 else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) { 780 milliseconds = 500; 781 } 782 else { 783 milliseconds = 999; 784 } 785 calendar.set(Calendar.MILLISECOND, milliseconds); 786 calendar.set(years, months, days, hours, minutes, value); 787 return calendar.getTime(); 788 789 case (DateTickUnit.MINUTE) : 790 years = calendar.get(Calendar.YEAR); 791 months = calendar.get(Calendar.MONTH); 792 days = calendar.get(Calendar.DATE); 793 hours = calendar.get(Calendar.HOUR_OF_DAY); 794 if (this.tickMarkPosition == DateTickMarkPosition.START) { 795 seconds = 0; 796 } 797 else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) { 798 seconds = 30; 799 } 800 else { 801 seconds = 59; 802 } 803 calendar.clear(Calendar.MILLISECOND); 804 calendar.set(years, months, days, hours, value, seconds); 805 return calendar.getTime(); 806 807 case (DateTickUnit.HOUR) : 808 years = calendar.get(Calendar.YEAR); 809 months = calendar.get(Calendar.MONTH); 810 days = calendar.get(Calendar.DATE); 811 if (this.tickMarkPosition == DateTickMarkPosition.START) { 812 minutes = 0; 813 seconds = 0; 814 } 815 else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) { 816 minutes = 30; 817 seconds = 0; 818 } 819 else { 820 minutes = 59; 821 seconds = 59; 822 } 823 calendar.clear(Calendar.MILLISECOND); 824 calendar.set(years, months, days, value, minutes, seconds); 825 return calendar.getTime(); 826 827 case (DateTickUnit.DAY) : 828 years = calendar.get(Calendar.YEAR); 829 months = calendar.get(Calendar.MONTH); 830 if (this.tickMarkPosition == DateTickMarkPosition.START) { 831 hours = 0; 832 minutes = 0; 833 seconds = 0; 834 } 835 else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) { 836 hours = 12; 837 minutes = 0; 838 seconds = 0; 839 } 840 else { 841 hours = 23; 842 minutes = 59; 843 seconds = 59; 844 } 845 calendar.clear(Calendar.MILLISECOND); 846 calendar.set(years, months, value, hours, 0, 0); 847 long result = calendar.getTime().getTime(); 850 if (result > date.getTime()) { 851 calendar.set(years, months, value - 1, hours, 0, 0); 852 } 853 return calendar.getTime(); 854 855 case (DateTickUnit.MONTH) : 856 years = calendar.get(Calendar.YEAR); 857 calendar.clear(Calendar.MILLISECOND); 858 calendar.set(years, value, 1, 0, 0, 0); 859 Month month = new Month(calendar.getTime()); 860 Date standardDate = calculateDateForPosition( 861 month, this.tickMarkPosition 862 ); 863 long millis = standardDate.getTime(); 864 if (millis > date.getTime()) { 865 month = (Month) month.previous(); 866 standardDate = calculateDateForPosition( 867 month, this.tickMarkPosition 868 ); 869 } 870 return standardDate; 871 872 case(DateTickUnit.YEAR) : 873 if (this.tickMarkPosition == DateTickMarkPosition.START) { 874 months = 0; 875 days = 1; 876 } 877 else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) { 878 months = 6; 879 days = 1; 880 } 881 else { 882 months = 11; 883 days = 31; 884 } 885 calendar.clear(Calendar.MILLISECOND); 886 calendar.set(value, months, days, 0, 0, |