-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
DateAxis.java
1930 lines (1778 loc) · 71.4 KB
/
DateAxis.java
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
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2017, 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.]
*
* -------------
* DateAxis.java
* -------------
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert;
* Contributor(s): Jonathan Nash;
* David Li;
* Michael Rauch;
* Bill Kelemen;
* Pawel Pabis;
* Chris Boek;
* Peter Kolb (patches 1934255 and 2603321);
* Andrew Mickish (patch 1870189);
* Fawad Halim (bug 2201869);
*
* Changes (from 23-Jun-2001)
* --------------------------
* 23-Jun-2001 : Modified to work with null data source (DG);
* 18-Sep-2001 : Updated header (DG);
* 27-Nov-2001 : Changed constructors from public to protected, updated Javadoc
* comments (DG);
* 16-Jan-2002 : Added an optional crosshair, based on the implementation by
* Jonathan Nash (DG);
* 26-Feb-2002 : Updated import statements (DG);
* 22-Apr-2002 : Added a setRange() method (DG);
* 25-Jun-2002 : Removed redundant local variable (DG);
* 25-Jul-2002 : Changed order of parameters in ValueAxis constructor (DG);
* 21-Aug-2002 : The setTickUnit() method now turns off auto-tick unit
* selection (fix for bug id 528885) (DG);
* 05-Sep-2002 : Updated the constructors to reflect changes in the Axis
* class (DG);
* 18-Sep-2002 : Fixed errors reported by Checkstyle (DG);
* 25-Sep-2002 : Added new setRange() methods, and deprecated
* setAxisRange() (DG);
* 04-Oct-2002 : Changed auto tick selection to parallel number axis
* classes (DG);
* 24-Oct-2002 : Added a date format override (DG);
* 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
* 14-Jan-2003 : Changed autoRangeMinimumSize from Number --> double, moved
* crosshair settings to the plot (DG);
* 15-Jan-2003 : Removed anchor date (DG);
* 20-Jan-2003 : Removed unnecessary constructors (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 02-May-2003 : Added additional units to createStandardDateTickUnits()
* method, as suggested by mhilpert in bug report 723187 (DG);
* 13-May-2003 : Merged HorizontalDateAxis and VerticalDateAxis (DG);
* 24-May-2003 : Added support for underlying timeline for
* SegmentedTimeline (BK);
* 16-Jul-2003 : Applied patch from Pawel Pabis to fix overlapping dates (DG);
* 22-Jul-2003 : Applied patch from Pawel Pabis for monthly ticks (DG);
* 25-Jul-2003 : Fixed bug 777561 and 777586 (DG);
* 13-Aug-2003 : Implemented Cloneable and added equals() method (DG);
* 02-Sep-2003 : Fixes for bug report 790506 (DG);
* 04-Sep-2003 : Fixed tick label alignment when axis appears at the top (DG);
* 10-Sep-2003 : Fixes for segmented timeline (DG);
* 17-Sep-2003 : Fixed a layout bug when multiple domain axes are used (DG);
* 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
* 07-Nov-2003 : Modified to use new tick classes (DG);
* 12-Nov-2003 : Modified tick labelling to use roll unit from DateTickUnit
* when a calculated tick value is hidden (which can occur in
* segmented date axes) (DG);
* 24-Nov-2003 : Fixed some problems with the auto tick unit selection, and
* fixed bug 846277 (labels missing for inverted axis) (DG);
* 30-Dec-2003 : Fixed bug in refreshTicksHorizontal() when start of time unit
* (ex. 1st of month) was hidden, causing infinite loop (BK);
* 13-Jan-2004 : Fixed bug in previousStandardDate() method (fix by Richard
* Wardle) (DG);
* 21-Jan-2004 : Renamed translateJava2DToValue --> java2DToValue, and
* translateValueToJava2D --> valueToJava2D (DG);
* 12-Mar-2004 : Fixed bug where date format override is ignored for vertical
* axis (DG);
* 16-Mar-2004 : Added plotState to draw() method (DG);
* 07-Apr-2004 : Changed string width calculation (DG);
* 21-Apr-2004 : Fixed bug in estimateMaximumTickLabelWidth() method (bug id
* 939148) (DG);
* 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
* release (DG);
* 13-Jan-2005 : Fixed bug (see
* http://www.jfree.org/forum/viewtopic.php?t=11330) (DG);
* 21-Apr-2005 : Replaced Insets with RectangleInsets, removed redundant
* argument from selectAutoTickUnit() (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 10-Feb-2006 : Added some API doc comments in respect of bug 821046 (DG);
* 19-Apr-2006 : Fixed bug 1472942 in equals() method (DG);
* 25-Sep-2006 : Fixed bug 1564977 missing tick labels (DG);
* 15-Jan-2007 : Added get/setTimeZone() suggested by 'skunk' (DG);
* 18-Jan-2007 : Fixed bug 1638678, time zone for calendar in
* previousStandardDate() (DG);
* 04-Apr-2007 : Use time zone in date calculations (CB);
* 19-Apr-2007 : Fix exceptions in setMinimum/MaximumDate() (DG);
* 03-May-2007 : Fixed minor bugs in previousStandardDate(), with new JUnit
* tests (DG);
* 21-Nov-2007 : Fixed warnings from FindBugs (DG);
* 01-Sep-2008 : Use new methods from DateRange, added fix for bug
* 2078057 (DG);
* 18-Sep-2008 : Added locale to go with timezone (DG);
* 25-Sep-2008 : Added minor tick support, see patch 1934255 by Peter Kolb (DG);
* 25-Nov-2008 : Added bug fix 2201869 by Fawad Halim (DG);
* 21-Jan-2009 : Check tickUnit for minor tick count (DG);
* 19-Mar-2009 : Added entity support - see patch 2603321 by Peter Kolb (DG);
* 08-Feb-2012 : Bugfix for endless-loop, bug 3484403 by rbrabe (MH);
* 25-Jul-2013 : Update event notification to use fireChangeEvent() (DG);
* 01-Aug-2013 : Added attributedLabel override to support superscripts,
* subscripts and more (DG);
* 12-Sep-2013 : Prevent exception when zooming in below 1 millisecond (DG);
* 23-Nov-2013 : Deprecated DEFAULT_DATE_TICK_UNIT to fix bug #977 (DG);
* 10-Mar-2014 : Add get/setLocale() methods (DG);
* 29-Aug-2016 : Fix for previousStandardDate - bug #25 (DG);
*
*/
package org.jfree.chart.axis;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.ValueAxisPlot;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.util.ObjectUtils;
import org.jfree.chart.util.Args;
import org.jfree.data.Range;
import org.jfree.data.time.DateRange;
import org.jfree.data.time.Month;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.Year;
/**
* The base class for axes that display dates. You will find it easier to
* understand how this axis works if you bear in mind that it really
* displays/measures integer (or long) data, where the integers are
* milliseconds since midnight, 1-Jan-1970. When displaying tick labels, the
* millisecond values are converted back to dates using a {@code DateFormat}
* instance.
* <P>
* You can also create a {@link org.jfree.chart.axis.Timeline} and supply in
* the constructor to create an axis that only contains certain domain values.
* For example, this allows you to create a date axis that only contains
* working days.
*/
public class DateAxis extends ValueAxis implements Cloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = -1013460999649007604L;
/** The default axis range. */
public static final DateRange DEFAULT_DATE_RANGE = new DateRange();
/** The default minimum auto range size. */
public static final double
DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS = 2.0;
/** The default anchor date. */
public static final Date DEFAULT_ANCHOR_DATE = new Date();
/** The current tick unit. */
private DateTickUnit tickUnit;
/** The override date format. */
private DateFormat dateFormatOverride;
/**
* Tick marks can be displayed at the start or the middle of the time
* period.
*/
private DateTickMarkPosition tickMarkPosition = DateTickMarkPosition.START;
/**
* A timeline that includes all milliseconds (as defined by
* {@code java.util.Date}) in the real time line.
*/
private static class DefaultTimeline implements Timeline, Serializable {
/**
* Converts a millisecond into a timeline value.
*
* @param millisecond the millisecond.
*
* @return The timeline value.
*/
@Override
public long toTimelineValue(long millisecond) {
return millisecond;
}
/**
* Converts a date into a timeline value.
*
* @param date the domain value.
*
* @return The timeline value.
*/
@Override
public long toTimelineValue(Date date) {
return date.getTime();
}
/**
* Converts a timeline value into a millisecond (as encoded by
* {@code java.util.Date}).
*
* @param value the value.
*
* @return The millisecond.
*/
@Override
public long toMillisecond(long value) {
return value;
}
/**
* Returns {@code true} if the timeline includes the specified
* domain value.
*
* @param millisecond the millisecond.
*
* @return {@code true}.
*/
@Override
public boolean containsDomainValue(long millisecond) {
return true;
}
/**
* Returns {@code true} if the timeline includes the specified
* domain value.
*
* @param date the date.
*
* @return {@code true}.
*/
@Override
public boolean containsDomainValue(Date date) {
return true;
}
/**
* Returns {@code true} if the timeline includes the specified
* domain value range.
*
* @param from the start value.
* @param to the end value.
*
* @return {@code true}.
*/
@Override
public boolean containsDomainRange(long from, long to) {
return true;
}
/**
* Returns {@code true} if the timeline includes the specified
* domain value range.
*
* @param from the start date.
* @param to the end date.
*
* @return {@code true}.
*/
@Override
public boolean containsDomainRange(Date from, Date to) {
return true;
}
/**
* Tests an object for equality with this instance.
*
* @param object the object.
*
* @return A boolean.
*/
@Override
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object == this) {
return true;
}
if (object instanceof DefaultTimeline) {
return true;
}
return false;
}
}
/** A static default timeline shared by all standard DateAxis */
private static final Timeline DEFAULT_TIMELINE = new DefaultTimeline();
/** The time zone for the axis. */
private TimeZone timeZone;
/**
* The locale for the axis ({@code null} is not permitted).
*
* @since 1.0.11
*/
private Locale locale;
/** Our underlying timeline. */
private Timeline timeline;
/**
* Creates a date axis with no label.
*/
public DateAxis() {
this(null);
}
/**
* Creates a date axis with the specified label.
*
* @param label the axis label ({@code null} permitted).
*/
public DateAxis(String label) {
this(label, TimeZone.getDefault(), Locale.getDefault());
}
/**
* Creates a date axis.
*
* @param label the axis label ({@code null} permitted).
* @param zone the time zone.
* @param locale the locale ({@code null} not permitted).
*
* @since 1.0.11
*/
public DateAxis(String label, TimeZone zone, Locale locale) {
super(label, DateAxis.createStandardDateTickUnits(zone, locale));
this.tickUnit = new DateTickUnit(DateTickUnitType.DAY, 1,
new SimpleDateFormat());
setAutoRangeMinimumSize(
DEFAULT_AUTO_RANGE_MINIMUM_SIZE_IN_MILLISECONDS);
setRange(DEFAULT_DATE_RANGE, false, false);
this.dateFormatOverride = null;
this.timeZone = zone;
this.locale = locale;
this.timeline = DEFAULT_TIMELINE;
}
/**
* Returns the time zone for the axis.
*
* @return The time zone (never {@code null}).
*
* @since 1.0.4
*
* @see #setTimeZone(TimeZone)
*/
public TimeZone getTimeZone() {
return this.timeZone;
}
/**
* Sets the time zone for the axis and sends an {@link AxisChangeEvent} to
* all registered listeners.
*
* @param zone the time zone ({@code null} not permitted).
*
* @since 1.0.4
*
* @see #getTimeZone()
*/
public void setTimeZone(TimeZone zone) {
Args.nullNotPermitted(zone, "zone");
this.timeZone = zone;
setStandardTickUnits(createStandardDateTickUnits(zone, this.locale));
fireChangeEvent();
}
/**
* Returns the locale for this axis.
*
* @return The locale (never {@code null}).
*
* @since 1.0.18
*/
public Locale getLocale() {
return this.locale;
}
/**
* Sets the locale for the axis and sends a change event to all registered
* listeners.
*
* @param locale the new locale ({@code null} not permitted).
*/
public void setLocale(Locale locale) {
Args.nullNotPermitted(locale, "locale");
this.locale = locale;
setStandardTickUnits(createStandardDateTickUnits(this.timeZone,
this.locale));
fireChangeEvent();
}
/**
* Returns the underlying timeline used by this axis.
*
* @return The timeline.
*/
public Timeline getTimeline() {
return this.timeline;
}
/**
* Sets the underlying timeline to use for this axis. If the timeline is
* changed, an {@link AxisChangeEvent} is sent to all registered listeners.
*
* @param timeline the timeline.
*/
public void setTimeline(Timeline timeline) {
if (this.timeline != timeline) {
this.timeline = timeline;
fireChangeEvent();
}
}
/**
* Returns the tick unit for the axis.
* <p>
* Note: if the {@code autoTickUnitSelection} flag is
* {@code true} the tick unit may be changed while the axis is being
* drawn, so in that case the return value from this method may be
* irrelevant if the method is called before the axis has been drawn.
*
* @return The tick unit (possibly {@code null}).
*
* @see #setTickUnit(DateTickUnit)
* @see ValueAxis#isAutoTickUnitSelection()
*/
public DateTickUnit getTickUnit() {
return this.tickUnit;
}
/**
* Sets the tick unit for the axis. The auto-tick-unit-selection flag is
* set to {@code false}, and registered listeners are notified that
* the axis has been changed.
*
* @param unit the tick unit.
*
* @see #getTickUnit()
* @see #setTickUnit(DateTickUnit, boolean, boolean)
*/
public void setTickUnit(DateTickUnit unit) {
setTickUnit(unit, true, true);
}
/**
* Sets the tick unit attribute and, if requested, sends an
* {@link AxisChangeEvent} to all registered listeners.
*
* @param unit the new tick unit.
* @param notify notify registered listeners?
* @param turnOffAutoSelection turn off auto selection?
*
* @see #getTickUnit()
*/
public void setTickUnit(DateTickUnit unit, boolean notify,
boolean turnOffAutoSelection) {
this.tickUnit = unit;
if (turnOffAutoSelection) {
setAutoTickUnitSelection(false, false);
}
if (notify) {
fireChangeEvent();
}
}
/**
* Returns the date format override. If this is non-null, then it will be
* used to format the dates on the axis.
*
* @return The formatter (possibly {@code null}).
*/
public DateFormat getDateFormatOverride() {
return this.dateFormatOverride;
}
/**
* Sets the date format override and sends an {@link AxisChangeEvent} to
* all registered listeners. If this is non-null, then it will be
* used to format the dates on the axis.
*
* @param formatter the date formatter ({@code null} permitted).
*/
public void setDateFormatOverride(DateFormat formatter) {
this.dateFormatOverride = formatter;
fireChangeEvent();
}
/**
* Sets the upper and lower bounds for the axis and sends an
* {@link AxisChangeEvent} to all registered listeners. As a side-effect,
* the auto-range flag is set to false.
*
* @param range the new range ({@code null} not permitted).
*/
@Override
public void setRange(Range range) {
setRange(range, true, true);
}
/**
* Sets the range for the axis, if requested, sends an
* {@link AxisChangeEvent} to all registered listeners. As a side-effect,
* the auto-range flag is set to {@code false} (optional).
*
* @param range the range ({@code null} not permitted).
* @param turnOffAutoRange a flag that controls whether or not the auto
* range is turned off.
* @param notify a flag that controls whether or not listeners are
* notified.
*/
@Override
public void setRange(Range range, boolean turnOffAutoRange,
boolean notify) {
Args.nullNotPermitted(range, "range");
// usually the range will be a DateRange, but if it isn't do a
// conversion...
if (!(range instanceof DateRange)) {
range = new DateRange(range);
}
super.setRange(range, turnOffAutoRange, notify);
}
/**
* Sets the axis range and sends an {@link AxisChangeEvent} to all
* registered listeners.
*
* @param lower the lower bound for the axis.
* @param upper the upper bound for the axis.
*/
public void setRange(Date lower, Date upper) {
if (lower.getTime() >= upper.getTime()) {
throw new IllegalArgumentException("Requires 'lower' < 'upper'.");
}
setRange(new DateRange(lower, upper));
}
/**
* Sets the axis range and sends an {@link AxisChangeEvent} to all
* registered listeners.
*
* @param lower the lower bound for the axis.
* @param upper the upper bound for the axis.
*/
@Override
public void setRange(double lower, double upper) {
if (lower >= upper) {
throw new IllegalArgumentException("Requires 'lower' < 'upper'.");
}
setRange(new DateRange(lower, upper));
}
/**
* Returns the earliest date visible on the axis.
*
* @return The date.
*
* @see #setMinimumDate(Date)
* @see #getMaximumDate()
*/
public Date getMinimumDate() {
Date result;
Range range = getRange();
if (range instanceof DateRange) {
DateRange r = (DateRange) range;
result = r.getLowerDate();
}
else {
result = new Date((long) range.getLowerBound());
}
return result;
}
/**
* Sets the minimum date visible on the axis and sends an
* {@link AxisChangeEvent} to all registered listeners. If
* {@code date} is on or after the current maximum date for
* the axis, the maximum date will be shifted to preserve the current
* length of the axis.
*
* @param date the date ({@code null} not permitted).
*
* @see #getMinimumDate()
* @see #setMaximumDate(Date)
*/
public void setMinimumDate(Date date) {
Args.nullNotPermitted(date, "date");
// check the new minimum date relative to the current maximum date
Date maxDate = getMaximumDate();
long maxMillis = maxDate.getTime();
long newMinMillis = date.getTime();
if (maxMillis <= newMinMillis) {
Date oldMin = getMinimumDate();
long length = maxMillis - oldMin.getTime();
maxDate = new Date(newMinMillis + length);
}
setRange(new DateRange(date, maxDate), true, false);
fireChangeEvent();
}
/**
* Returns the latest date visible on the axis.
*
* @return The date.
*
* @see #setMaximumDate(Date)
* @see #getMinimumDate()
*/
public Date getMaximumDate() {
Date result;
Range range = getRange();
if (range instanceof DateRange) {
DateRange r = (DateRange) range;
result = r.getUpperDate();
}
else {
result = new Date((long) range.getUpperBound());
}
return result;
}
/**
* Sets the maximum date visible on the axis and sends an
* {@link AxisChangeEvent} to all registered listeners. If
* {@code maximumDate} is on or before the current minimum date for
* the axis, the minimum date will be shifted to preserve the current
* length of the axis.
*
* @param maximumDate the date ({@code null} not permitted).
*
* @see #getMinimumDate()
* @see #setMinimumDate(Date)
*/
public void setMaximumDate(Date maximumDate) {
Args.nullNotPermitted(maximumDate, "maximumDate");
// check the new maximum date relative to the current minimum date
Date minDate = getMinimumDate();
long minMillis = minDate.getTime();
long newMaxMillis = maximumDate.getTime();
if (minMillis >= newMaxMillis) {
Date oldMax = getMaximumDate();
long length = oldMax.getTime() - minMillis;
minDate = new Date(newMaxMillis - length);
}
setRange(new DateRange(minDate, maximumDate), true, false);
fireChangeEvent();
}
/**
* Returns the tick mark position (start, middle or end of the time period).
*
* @return The position (never {@code null}).
*/
public DateTickMarkPosition getTickMarkPosition() {
return this.tickMarkPosition;
}
/**
* Sets the tick mark position (start, middle or end of the time period)
* and sends an {@link AxisChangeEvent} to all registered listeners.
*
* @param position the position ({@code null} not permitted).
*/
public void setTickMarkPosition(DateTickMarkPosition position) {
Args.nullNotPermitted(position, "position");
this.tickMarkPosition = position;
fireChangeEvent();
}
/**
* Configures the axis to work with the specified plot. If the axis has
* auto-scaling, then sets the maximum and minimum values.
*/
@Override
public void configure() {
if (isAutoRange()) {
autoAdjustRange();
}
}
/**
* Returns {@code true} if the axis hides this value, and
* {@code false} otherwise.
*
* @param millis the data value.
*
* @return A value.
*/
public boolean isHiddenValue(long millis) {
return (!this.timeline.containsDomainValue(new Date(millis)));
}
/**
* Translates the data value to the display coordinates (Java 2D User Space)
* of the chart.
*
* @param value the date to be plotted.
* @param area the rectangle (in Java2D space) where the data is to be
* plotted.
* @param edge the axis location.
*
* @return The coordinate corresponding to the supplied data value.
*/
@Override
public double valueToJava2D(double value, Rectangle2D area,
RectangleEdge edge) {
value = this.timeline.toTimelineValue((long) value);
DateRange range = (DateRange) getRange();
double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());
double result = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
double minX = area.getX();
double maxX = area.getMaxX();
if (isInverted()) {
result = maxX + ((value - axisMin) / (axisMax - axisMin))
* (minX - maxX);
}
else {
result = minX + ((value - axisMin) / (axisMax - axisMin))
* (maxX - minX);
}
}
else if (RectangleEdge.isLeftOrRight(edge)) {
double minY = area.getMinY();
double maxY = area.getMaxY();
if (isInverted()) {
result = minY + (((value - axisMin) / (axisMax - axisMin))
* (maxY - minY));
}
else {
result = maxY - (((value - axisMin) / (axisMax - axisMin))
* (maxY - minY));
}
}
return result;
}
/**
* Translates a date to Java2D coordinates, based on the range displayed by
* this axis for the specified data area.
*
* @param date the date.
* @param area the rectangle (in Java2D space) where the data is to be
* plotted.
* @param edge the axis location.
*
* @return The coordinate corresponding to the supplied date.
*/
public double dateToJava2D(Date date, Rectangle2D area,
RectangleEdge edge) {
double value = date.getTime();
return valueToJava2D(value, area, edge);
}
/**
* Translates a Java2D coordinate into the corresponding data value. To
* perform this translation, you need to know the area used for plotting
* data, and which edge the axis is located on.
*
* @param java2DValue the coordinate in Java2D space.
* @param area the rectangle (in Java2D space) where the data is to be
* plotted.
* @param edge the axis location.
*
* @return A data value.
*/
@Override
public double java2DToValue(double java2DValue, Rectangle2D area,
RectangleEdge edge) {
DateRange range = (DateRange) getRange();
double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());
double min = 0.0;
double max = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
min = area.getX();
max = area.getMaxX();
}
else if (RectangleEdge.isLeftOrRight(edge)) {
min = area.getMaxY();
max = area.getY();
}
double result;
if (isInverted()) {
result = axisMax - ((java2DValue - min) / (max - min)
* (axisMax - axisMin));
}
else {
result = axisMin + ((java2DValue - min) / (max - min)
* (axisMax - axisMin));
}
return this.timeline.toMillisecond((long) result);
}
/**
* Calculates the value of the lowest visible tick on the axis.
*
* @param unit date unit to use.
*
* @return The value of the lowest visible tick on the axis.
*/
public Date calculateLowestVisibleTickValue(DateTickUnit unit) {
return nextStandardDate(getMinimumDate(), unit);
}
/**
* Calculates the value of the highest visible tick on the axis.
*
* @param unit date unit to use.
*
* @return The value of the highest visible tick on the axis.
*/
public Date calculateHighestVisibleTickValue(DateTickUnit unit) {
return previousStandardDate(getMaximumDate(), unit);
}
/**
* Returns the previous "standard" date, for a given date and tick unit.
*
* @param date the reference date.
* @param unit the tick unit.
*
* @return The previous "standard" date.
*/
protected Date previousStandardDate(Date date, DateTickUnit unit) {
int milliseconds;
int seconds;
int minutes;
int hours;
int days;
int months;
int years;
Calendar calendar = Calendar.getInstance(this.timeZone, this.locale);
calendar.setTime(date);
int count = unit.getMultiple();
int current = calendar.get(unit.getCalendarField());
int value = count * (current / count);
if (DateTickUnitType.MILLISECOND.equals(unit.getUnitType())) {
years = calendar.get(Calendar.YEAR);
months = calendar.get(Calendar.MONTH);
days = calendar.get(Calendar.DATE);
hours = calendar.get(Calendar.HOUR_OF_DAY);
minutes = calendar.get(Calendar.MINUTE);
seconds = calendar.get(Calendar.SECOND);
calendar.set(years, months, days, hours, minutes, seconds);
calendar.set(Calendar.MILLISECOND, value);
Date mm = calendar.getTime();
if (mm.getTime() >= date.getTime()) {
calendar.set(Calendar.MILLISECOND, value - count);
mm = calendar.getTime();
}
return mm;
}
else if (DateTickUnitType.SECOND.equals(unit.getUnitType())) {
years = calendar.get(Calendar.YEAR);
months = calendar.get(Calendar.MONTH);
days = calendar.get(Calendar.DATE);
hours = calendar.get(Calendar.HOUR_OF_DAY);
minutes = calendar.get(Calendar.MINUTE);
if (this.tickMarkPosition == DateTickMarkPosition.START) {
milliseconds = 0;
}
else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
milliseconds = 500;
}
else {
milliseconds = 999;
}
calendar.set(Calendar.MILLISECOND, milliseconds);
calendar.set(years, months, days, hours, minutes, value);
Date dd = calendar.getTime();
if (dd.getTime() >= date.getTime()) {
calendar.set(Calendar.SECOND, value - count);
dd = calendar.getTime();
}
return dd;
}
else if (DateTickUnitType.MINUTE.equals(unit.getUnitType())) {
years = calendar.get(Calendar.YEAR);
months = calendar.get(Calendar.MONTH);
days = calendar.get(Calendar.DATE);
hours = calendar.get(Calendar.HOUR_OF_DAY);
if (this.tickMarkPosition == DateTickMarkPosition.START) {
seconds = 0;
}
else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
seconds = 30;
}
else {
seconds = 59;
}
calendar.clear(Calendar.MILLISECOND);
calendar.set(years, months, days, hours, value, seconds);
Date d0 = calendar.getTime();
if (d0.getTime() >= date.getTime()) {
calendar.set(Calendar.MINUTE, value - count);
d0 = calendar.getTime();
}
return d0;
}
else if (DateTickUnitType.HOUR.equals(unit.getUnitType())) {
years = calendar.get(Calendar.YEAR);
months = calendar.get(Calendar.MONTH);
days = calendar.get(Calendar.DATE);
if (this.tickMarkPosition == DateTickMarkPosition.START) {
minutes = 0;
seconds = 0;
}
else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
minutes = 30;
seconds = 0;
}
else {
minutes = 59;
seconds = 59;
}
calendar.clear(Calendar.MILLISECOND);
calendar.set(years, months, days, value, minutes, seconds);
Date d1 = calendar.getTime();
if (d1.getTime() >= date.getTime()) {
calendar.set(Calendar.HOUR_OF_DAY, value - count);
d1 = calendar.getTime();
}
return d1;
}
else if (DateTickUnitType.DAY.equals(unit.getUnitType())) {
years = calendar.get(Calendar.YEAR);
months = calendar.get(Calendar.MONTH);
if (this.tickMarkPosition == DateTickMarkPosition.START) {
hours = 0;
}
else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
hours = 12;
}
else {
hours = 23;
}
calendar.clear(Calendar.MILLISECOND);
calendar.set(years, months, value, hours, 0, 0);
// long result = calendar.getTimeInMillis();
// won't work with JDK 1.3
Date d2 = calendar.getTime();