📄 dateaxis.java
字号:
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);
calendar.setTime(date);
int count = unit.getCount();
int current = calendar.get(unit.getCalendarField());
int value = count * (current / count);
switch (unit.getUnit()) {
case (DateTickUnit.MILLISECOND) :
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);
return calendar.getTime();
case (DateTickUnit.SECOND) :
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);
return calendar.getTime();
case (DateTickUnit.MINUTE) :
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);
return calendar.getTime();
case (DateTickUnit.HOUR) :
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);
return calendar.getTime();
case (DateTickUnit.DAY) :
years = calendar.get(Calendar.YEAR);
months = calendar.get(Calendar.MONTH);
if (this.tickMarkPosition == DateTickMarkPosition.START) {
hours = 0;
minutes = 0;
seconds = 0;
}
else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
hours = 12;
minutes = 0;
seconds = 0;
}
else {
hours = 23;
minutes = 59;
seconds = 59;
}
calendar.clear(Calendar.MILLISECOND);
calendar.set(years, months, value, hours, 0, 0);
long result = calendar.getTime().getTime();
if (result > date.getTime()) {
calendar.set(years, months, value - 1, hours, 0, 0);
}
return calendar.getTime();
case (DateTickUnit.MONTH) :
years = calendar.get(Calendar.YEAR);
calendar.clear(Calendar.MILLISECOND);
calendar.set(years, value, 1, 0, 0, 0);
Month month = new Month(calendar.getTime());
Date standardDate = calculateDateForPosition(month, this.tickMarkPosition);
long millis = standardDate.getTime();
if (millis > date.getTime()) {
month = (Month) month.previous();
standardDate = calculateDateForPosition(month, this.tickMarkPosition);
}
return standardDate;
case(DateTickUnit.YEAR) :
if (this.tickMarkPosition == DateTickMarkPosition.START) {
months = 0;
}
else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
months = 6;
}
else {
months = 12;
}
calendar.clear(Calendar.MILLISECOND);
calendar.set(value, months, 1, 0, 0, 0);
return calendar.getTime();
default: return null;
}
}
/**
* Returns a {@link java.util.Date} corresponding to the specified position within a
* {@link RegularTimePeriod}.
*
* @param period the period.
* @param position the position (<code>null</code> not permitted).
*
* @return A date.
*/
private Date calculateDateForPosition(RegularTimePeriod period, DateTickMarkPosition position) {
if (position == null) {
throw new IllegalArgumentException("Null 'position' argument.");
}
Date result = null;
if (position == DateTickMarkPosition.START) {
result = new Date(period.getFirstMillisecond());
}
else if (position == DateTickMarkPosition.MIDDLE) {
result = new Date(period.getMiddleMillisecond());
}
else if (position == DateTickMarkPosition.END) {
result = new Date(period.getLastMillisecond());
}
return result;
}
/**
* Returns the first "standard" date (based on the specified field and units).
*
* @param date the reference date.
* @param unit the date tick unit.
*
* @return the next "standard" date.
*/
protected Date nextStandardDate(Date date, DateTickUnit unit) {
Date previous = previousStandardDate(date, unit);
Calendar calendar = Calendar.getInstance();
calendar.setTime(previous);
calendar.add(unit.getCalendarField(), unit.getCount());
return calendar.getTime();
}
/**
* Returns a collection of standard date tick units that uses the default time zone.
* This collection will be used by default, but you are free to create your own collection if
* you want to (see the setStandardTickUnits() method inherited from the ValueAxis class).
*
* @return A collection of standard date tick units.
*/
public static TickUnitSource createStandardDateTickUnits() {
return createStandardDateTickUnits(TimeZone.getDefault());
}
/**
* Returns a collection of standard date tick units. This collection will be used by default,
* but you are free to create your own collection if you want to (see the
* setStandardTickUnits() method inherited from the {@link ValueAxis} class).
*
* @param zone the time zone (<code>null</code> not permitted).
*
* @return A collection of standard date tick units.
*/
public static TickUnitSource createStandardDateTickUnits(TimeZone zone) {
if (zone == null) {
throw new IllegalArgumentException("Null 'zone' argument.");
}
TickUnits units = new TickUnits();
// date formatters
DateFormat f1 = new SimpleDateFormat("HH:mm:ss.SSS");
DateFormat f2 = new SimpleDateFormat("HH:mm:ss");
DateFormat f3 = new SimpleDateFormat("HH:mm");
DateFormat f4 = new SimpleDateFormat("d-MMM, HH:mm");
DateFormat f5 = new SimpleDateFormat("d-MMM");
DateFormat f6 = new SimpleDateFormat("MMM-yyyy");
DateFormat f7 = new SimpleDateFormat("yyyy");
f1.setTimeZone(zone);
f2.setTimeZone(zone);
f3.setTimeZone(zone);
f4.setTimeZone(zone);
f5.setTimeZone(zone);
f6.setTimeZone(zone);
f7.setTimeZone(zone);
// milliseconds
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 1, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 5, DateTickUnit.MILLISECOND, 1, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 10, DateTickUnit.MILLISECOND, 1, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 25, DateTickUnit.MILLISECOND, 5, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 50, DateTickUnit.MILLISECOND, 10, f1));
units.add(
new DateTickUnit(DateTickUnit.MILLISECOND, 100, DateTickUnit.MILLISECOND, 10, f1)
);
units.add(
new DateTickUnit(DateTickUnit.MILLISECOND, 250, DateTickUnit.MILLISECOND, 10, f1)
);
units.add(
new DateTickUnit(DateTickUnit.MILLISECOND, 500, DateTickUnit.MILLISECOND, 50, f1)
);
// seconds
units.add(new DateTickUnit(DateTickUnit.SECOND, 1, DateTickUnit.MILLISECOND, 50, f2));
units.add(new DateTickUnit(DateTickUnit.SECOND, 5, DateTickUnit.SECOND, 1, f2));
units.add(new DateTickUnit(DateTickUnit.SECOND, 10, DateTickUnit.SECOND, 1, f2));
units.add(new DateTickUnit(DateTickUnit.SECOND, 30, DateTickUnit.SECOND, 5, f2));
// minutes
units.add(new DateTickUnit(DateTickUnit.MINUTE, 1, DateTickUnit.SECOND, 5, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 2, DateTickUnit.SECOND, 10, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 5, DateTickUnit.MINUTE, 1, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 10, DateTickUnit.MINUTE, 1, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 15, DateTickUnit.MINUTE, 5, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 20, DateTickUnit.MINUTE, 5, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 30, DateTickUnit.MINUTE, 5, f3));
// hours
units.add(new DateTickUnit(DateTickUnit.HOUR, 1, DateTickUnit.MINUTE, 5, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 2, DateTickUnit.MINUTE, 10, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 4, DateTickUnit.MINUTE, 30, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 6, DateTickUnit.HOUR, 1, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 12, DateTickUnit.HOUR, 1, f4));
// days
units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1, f5));
units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1, f5));
units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1, f5));
units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1, f5));
// months
units.add(new DateTickUnit(DateTickUnit.MONTH, 1, DateTickUnit.DAY, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 2, DateTickUnit.DAY, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 3, DateTickUnit.MONTH, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 4, DateTickUnit.MONTH, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 6, DateTickUnit.MONTH, 1, f6));
// years
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -