📄 dateaxis.java
字号:
}
setRange(range, true, true);
notifyListeners(new AxisChangeEvent(this));
}
/**
* Sets the axis range. An {@link AxisChangeEvent} is sent 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) {
// check arguments...
if (lower.getTime() >= upper.getTime()) {
throw new IllegalArgumentException("DateAxis.setRange(...): lower not before upper.");
}
// make the change...
setRange(new DateRange(lower, upper));
}
/**
* Sets the axis range. An {@link AxisChangeEvent} is sent to all registered listeners.
*
* @param lower the lower bound for the axis.
* @param upper the upper bound for the axis.
*/
public void setRange(double lower, double upper) {
// check arguments...
if (lower >= upper) {
throw new IllegalArgumentException("DateAxis.setRange(...): lower >= upper.");
}
// make the change...
setRange(new DateRange(lower, upper));
}
/**
* Returns the earliest date visible on the axis.
*
* @return the earliest date visible on the axis.
*/
public Date getMinimumDate() {
Date result = null;
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. An {@link AxisChangeEvent} is sent to all
* registered listeners.
*
* @param minimumDate the new minimum date.
*/
public void setMinimumDate(Date minimumDate) {
setRange(new DateRange(minimumDate, getMaximumDate()), true, false);
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the latest date visible on the axis.
*
* @return the latest date visible on the axis.
*/
public Date getMaximumDate() {
Date result = null;
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. An {@link AxisChangeEvent} is sent to all
* registered listeners.
*
* @param maximumDate the new maximum date.
*/
public void setMaximumDate(Date maximumDate) {
setRange(new DateRange(getMinimumDate(), maximumDate), true, false);
notifyListeners(new AxisChangeEvent(this));
}
/**
* Returns the tick mark position (start, middle or end of the time period).
*
* @return The position.
*/
public DateTickMarkPosition getTickMarkPosition() {
return this.tickMarkPosition;
}
/**
* Sets the tick mark position (start, middle or end of the time period). An
* {@link AxisChangeEvent} is sent to all registered listeners.
*
* @param position the new position.
*/
public void setTickMarkPosition(DateTickMarkPosition position) {
this.tickMarkPosition = position;
notifyListeners(new AxisChangeEvent(this));
}
/**
* Configures the axis to work with the specified plot. If the axis has
* auto-scaling, then sets the maximum and minimum values.
*/
public void configure() {
if (isAutoRange()) {
autoAdjustRange();
}
}
/**
* Translates the data value to the display coordinates (Java 2D User Space)
* of the chart.
*
* @param value the date to be plotted.
* @param dataArea 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.
*/
public double translateValueToJava2D(double value, Rectangle2D dataArea,
RectangleEdge edge) {
value = timeline.toTimelineValue((long) value);
DateRange range = (DateRange) getRange();
double axisMin = timeline.toTimelineValue(range.getLowerDate());
double axisMax = timeline.toTimelineValue(range.getUpperDate());
double result = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
double minX = dataArea.getX();
double maxX = dataArea.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 = dataArea.getMinY();
double maxY = dataArea.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 dataArea 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 translateDateToJava2D(Date date, Rectangle2D dataArea, RectangleEdge edge) {
double value = (double) date.getTime();
return translateValueToJava2D(value, dataArea, edge);
}
/**
* Translates the Java2D (vertical) coordinate back to the corresponding
* data value.
*
* @param java2DValue the coordinate in Java2D space.
* @param dataArea the rectangle (in Java2D space) where the data is to be plotted.
* @param edge the axis location.
*
* @return the data value corresponding to the Java2D coordinate.
*/
public double translateJava2DtoValue(float java2DValue,
Rectangle2D dataArea, RectangleEdge edge) {
DateRange range = (DateRange) getRange();
double axisMin = timeline.toTimelineValue(range.getLowerDate());
double axisMax = timeline.toTimelineValue(range.getUpperDate());
double min = 0.0;
double max = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
min = dataArea.getX();
max = dataArea.getMaxX();
}
else if (RectangleEdge.isLeftOrRight(edge)) {
min = dataArea.getMaxY();
max = dataArea.getY();
}
double result;
if (isInverted()) {
result = axisMax - ((java2DValue - min) / (max - min) * (axisMax - axisMin));
}
else {
result = axisMin + ((java2DValue - min) / (max - min) * (axisMax - axisMin));
}
result = timeline.toTimelineValue((long) result);
return (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();
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -