📄 numberaxis.java
字号:
return this.markerBand;
}
/**
* Sets the marker band for the axis.
* <P>
* The marker band is optional, leave it set to <code>null</code> if you don't require it.
*
* @param band the new band (<code>null<code> permitted).
*/
public void setMarkerBand(MarkerAxisBand band) {
this.markerBand = band;
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();
}
}
/**
* 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();
if (autoRangeIncludesZero()) {
lower = Math.min(lower, 0.0);
upper = Math.max(upper, 0.0);
}
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 (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 area the area for plotting the data.
* @param edge the axis location.
*
* @return The Java2D coordinate.
*
* @deprecated Use valueToJava2D() instead.
*/
public double translateValueToJava2D(double value, Rectangle2D area, RectangleEdge edge) {
return valueToJava2D(value, area, edge);
}
/**
* 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 area the area for plotting the data.
* @param edge the axis location.
*
* @return The Java2D coordinate.
*/
public double valueToJava2D(double value, Rectangle2D area, 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 = area.getX();
max = area.getMaxX();
}
else if (RectangleEdge.isLeftOrRight(edge)) {
max = area.getMinY();
min = area.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 area the area in which the data is plotted.
* @param edge the location.
*
* @return The data value.
*
* @deprecated Use java2DToValue() instead.
*/
public double translateJava2DToValue(double java2DValue, Rectangle2D area, RectangleEdge edge) {
return java2DToValue(java2DValue, area, edge);
}
/**
* 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 area the area in which the data is plotted.
* @param edge the location.
*
* @return The data value.
*/
public double java2DToValue(double java2DValue, Rectangle2D area, 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 = area.getX();
max = area.getMaxX();
}
else if (RectangleEdge.isLeftOrRight(edge)) {
min = area.getMaxY();
max = area.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);
}
/**
* Draws the axis on a Java 2D graphics device (such as the screen or a printer).
*
* @param g2 the graphics device (<code>null</code> not permitted).
* @param cursor the cursor location.
* @param plotArea the area within which the axes and data should be drawn (<code>null</code>
* not permitted).
* @param dataArea the area within which the data should be drawn (<code>null</code> not
* permitted).
* @param edge the location of the axis (<code>null</code> not permitted).
* @param plotState collects information about the plot (<code>null</code> permitted).
*
* @return the axis state (never <code>null</code>).
*/
public AxisState draw(Graphics2D g2,
double cursor,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge,
PlotRenderingInfo plotState) {
AxisState state = null;
// if the axis is not visible, don't draw it...
if (!isVisible()) {
state = new AxisState(cursor);
// even though the axis is not visible, we need ticks for the gridlines...
List ticks = refreshTicks(g2, state, plotArea, dataArea, edge);
state.setTicks(ticks);
return state;
}
// draw the tick marks and labels...
state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);
// // 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...
state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);
return state;
}
/**
* Creates the standard tick units.
* <P>
* If you don't like these defaults, create your own instance of TickUnits
* and then pass it to the setStandardTickUnits(...) method in the
* NumberAxis class.
*
* @return the standard tick units.
*/
public static TickUnitSource createStandardTickUnits() {
TickUnits units = new TickUnits();
// we can add the units in any order, the TickUnits collection will sort them...
units.add(new NumberTickUnit(0.0000001, new DecimalFormat("0.0000000")));
units.add(new NumberTickUnit(0.000001, new DecimalFormat("0.000000")));
units.add(new NumberTickUnit(0.00001, new DecimalFormat("0.00000")));
units.add(new NumberTickUnit(0.0001, new DecimalFormat("0.0000")));
units.add(new NumberTickUnit(0.001, new DecimalFormat("0.000")));
units.add(new NumberTickUnit(0.01, new DecimalFormat("0.00")));
units.add(new NumberTickUnit(0.1, new DecimalFormat("0.0")));
units.add(new NumberTickUnit(1, new DecimalFormat("0")));
units.add(new NumberTickUnit(10, new DecimalFormat("0")));
units.add(new NumberTickUnit(100, new DecimalFormat("0")));
units.add(new NumberTickUnit(1000, new DecimalFormat("#,##0")));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -