📄 numberaxis.java
字号:
}
/**
* Rescales the axis to ensure that all data is visible.
*/
protected void autoAdjustRange() {
Plot plot = getPlot();
if (plot == null) {
return; // no plot, no data
}
if (plot instanceof ValueAxisPlot) {
ValueAxisPlot vap = (ValueAxisPlot) plot;
Range r = vap.getDataRange(this);
if (r == null) {
r = new Range(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);
}
double upper = r.getUpperBound();
double lower = r.getLowerBound();
double range = upper - lower;
// if fixed auto range, then derive lower bound...
double fixedAutoRange = getFixedAutoRange();
if (fixedAutoRange > 0.0) {
lower = upper - fixedAutoRange;
}
else {
// ensure the autorange is at least <minRange> in size...
double minRange = getAutoRangeMinimumSize();
if (range < minRange) {
double expand = (minRange - range) / 2;
upper = upper + expand;
lower = lower - expand;
}
if (autoRangeIncludesZero()) {
if (autoRangeStickyZero()) {
if (upper <= 0.0) {
upper = 0.0;
}
else {
upper = upper + getUpperMargin() * range;
}
if (lower >= 0.0) {
lower = 0.0;
}
else {
lower = lower - getLowerMargin() * range;
}
}
else {
upper = Math.max(0.0, upper + getUpperMargin() * range);
lower = Math.min(0.0, lower - getLowerMargin() * range);
}
}
else {
if (autoRangeStickyZero()) {
if (upper <= 0.0) {
upper = Math.min(0.0, upper + getUpperMargin() * range);
}
else {
upper = upper + getUpperMargin() * range;
}
if (lower >= 0.0) {
lower = Math.max(0.0, lower - getLowerMargin() * range);
}
else {
lower = lower - getLowerMargin() * range;
}
}
else {
upper = upper + getUpperMargin() * range;
lower = lower - getLowerMargin() * range;
}
}
}
setRange(new Range(lower, upper), false, false);
}
}
/**
* Converts a data value to a coordinate in Java2D space, assuming that the
* axis runs along one edge of the specified dataArea.
* <p>
* Note that it is possible for the coordinate to fall outside the plotArea.
*
* @param value the data value.
* @param dataArea the area for plotting the data.
* @param edge the axis location.
*
* @return The Java2D coordinate.
*/
public double translateValueToJava2D(double value,
Rectangle2D dataArea, RectangleEdge edge) {
Range range = getRange();
double axisMin = range.getLowerBound();
double axisMax = range.getUpperBound();
double min = 0.0;
double max = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
min = dataArea.getX();
max = dataArea.getMaxX();
}
else if (RectangleEdge.isLeftOrRight(edge)) {
max = dataArea.getMinY();
min = dataArea.getMaxY();
}
if (isInverted()) {
return max - ((value - axisMin) / (axisMax - axisMin)) * (max - min);
}
else {
return min + ((value - axisMin) / (axisMax - axisMin)) * (max - min);
}
}
/**
* Converts a coordinate in Java2D space to the corresponding data value,
* assuming that the axis runs along one edge of the specified dataArea.
*
* @param java2DValue the coordinate in Java2D space.
* @param dataArea the area in which the data is plotted.
* @param edge the location.
*
* @return The data value.
*/
public double translateJava2DtoValue(float java2DValue, Rectangle2D dataArea,
RectangleEdge edge) {
Range range = getRange();
double axisMin = range.getLowerBound();
double axisMax = range.getUpperBound();
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();
}
if (isInverted()) {
return axisMax - (java2DValue - min) / (max - min) * (axisMax - axisMin);
}
else {
return axisMin + (java2DValue - min) / (max - min) * (axisMax - axisMin);
}
}
/**
* Calculates the value of the lowest visible tick on the axis.
*
* @return the value of the lowest visible tick on the axis.
*/
public double calculateLowestVisibleTickValue() {
double unit = getTickUnit().getSize();
double index = Math.ceil(getRange().getLowerBound() / unit);
return index * unit;
}
/**
* Calculates the value of the highest visible tick on the axis.
*
* @return the value of the highest visible tick on the axis.
*/
public double calculateHighestVisibleTickValue() {
double unit = getTickUnit().getSize();
double index = Math.floor(getRange().getUpperBound() / unit);
return index * unit;
}
/**
* Calculates the number of visible ticks.
*
* @return the number of visible ticks on the axis.
*/
public int calculateVisibleTickCount() {
double unit = getTickUnit().getSize();
Range range = getRange();
return (int) (Math.floor(range.getUpperBound() / unit)
- Math.ceil(range.getLowerBound() / unit) + 1);
}
/**
* Returns the space required to draw the axis.
*
* @param g2 the graphics device.
* @param plot a reference to the plot;
* @param plotArea the area within which the plot should be drawn.
* @param edge the axis location.
* @param space the space already reserved.
*
* @return Space required to draw the axis.
*/
public AxisSpace reserveSpace(Graphics2D g2, Plot plot, Rectangle2D plotArea,
RectangleEdge edge, AxisSpace space) {
// create a new space object if one wasn't supplied...
if (space == null) {
space = new AxisSpace();
}
this.reservedForAxisLabel = 0.0;
this.reservedForTickLabels = 0.0;
// if the axis is not visible, no additional space is required...
if (!isVisible()) {
return space;
}
// if the axis has a fixed dimension, return it...
double dimension = getFixedDimension();
if (dimension > 0.0) {
space.ensureAtLeast(dimension, edge);
}
// calculate the max size of the tick labels (if visible)...
double tickLabelHeight = 0.0;
double tickLabelWidth = 0.0;
if (isTickLabelsVisible()) {
g2.setFont(getTickLabelFont());
refreshTicks(g2, 0.0, plotArea, plotArea, edge);
Insets tickLabelInsets = getTickLabelInsets();
if (RectangleEdge.isTopOrBottom(edge)) {
tickLabelHeight = tickLabelInsets.top + tickLabelInsets.bottom;
tickLabelHeight += getMaxTickLabelHeight(g2, plotArea, isVerticalTickLabels());
this.reservedForTickLabels = tickLabelHeight;
}
else if (RectangleEdge.isLeftOrRight(edge)) {
tickLabelWidth = tickLabelInsets.left + tickLabelInsets.right;
tickLabelWidth += getMaxTickLabelWidth(g2, plotArea);
this.reservedForTickLabels = tickLabelWidth;
}
}
// calculate the size of the marker band (if there is one)...
double markerBandHeight = 0.0;
if (this.markerBand != null) {
markerBandHeight = this.markerBand.getHeight(g2);
//this.reserved += markerBandHeight;
}
// get the axis label size and update the space object...
Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge);
double labelHeight = 0.0;
double labelWidth = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
labelHeight = labelEnclosure.getHeight();
this.reservedForAxisLabel = labelHeight;
space.add(labelHeight + tickLabelHeight + markerBandHeight, edge);
}
else if (RectangleEdge.isLeftOrRight(edge)) {
labelWidth = labelEnclosure.getWidth();
this.reservedForAxisLabel = labelWidth;
space.add(labelWidth + tickLabelWidth, edge);
}
return space;
}
/**
* Draws the axis on a Java 2D graphics device (such as the screen or a printer).
*
* @param g2 the graphics device.
* @param cursor the cursor location.
* @param plotArea the area within which the axes and data should be drawn.
* @param dataArea the area within which the data should be drawn.
* @param edge the location of the axis.
*
* @return The width or height used by the axis.
*/
public double draw(Graphics2D g2, double cursor,
Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge) {
// if the axis is not visible, don't draw it...
if (!isVisible()) {
return 0.0;
}
// draw the tick marks and labels...
double used1 = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);
if (edge == RectangleEdge.TOP || edge == RectangleEdge.LEFT) {
cursor = cursor - used1;
}
else if (edge == RectangleEdge.BOTTOM || edge == RectangleEdge.RIGHT) {
cursor = cursor + used1;
}
// // draw the marker band (if there is one)...
// if (getMarkerBand() != null) {
// if (edge == RectangleEdge.BOTTOM) {
// cursor = cursor - getMarkerBand().getHeight(g2);
// }
// getMarkerBand().draw(g2, plotArea, dataArea, 0, cursor);
// }
// draw the axis label...
double used2 = drawLabel(getLabel(), g2, cursor, plotArea, dataArea, edge);
return used1 + used2;
}
/**
* Draws the axis line, tick marks and tick mark labels.
*
* @param g2 the graphics device.
* @param cursor the cursor.
* @param plotArea the plot area.
* @param dataArea the data area.
* @param edge the edge that the axis is aligned with.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -