?? logarithmicaxis.java
字號:
//only 1 power of 10 value, it's > 0 and its resulting
// tick value will be larger than lower bound of data
--iBegCount; //decrement to generate more ticks
}
double currentTickValue;
String tickLabel;
boolean zeroTickFlag = false;
for (int i = iBegCount; i <= iEndCount; i++) {
//for each power of 10 value; create ten ticks
for (int j = 0; j < 10; ++j) {
//for each tick to be displayed
if (smallLogFlag) {
//small log values in use; create numeric value for tick
currentTickValue = Math.pow(10, i) + (Math.pow(10, i) * j);
if (expTickLabelsFlag
|| (i < 0 && currentTickValue > 0.0 && currentTickValue < 1.0)) {
//showing "1e#"-style ticks or negative exponent
// generating tick value between 0 & 1; show fewer
if (j == 0 || (i > -4 && j < 2)
|| currentTickValue >= upperBoundVal) {
//first tick of series, or not too small a value and
// one of first 3 ticks, or last tick to be displayed
//set exact number of fractional digits to be shown
// (no effect if showing "1e#"-style ticks):
numberFormatterObj.setMaximumFractionDigits(-i);
//create tick label (force use of fmt obj):
tickLabel = makeTickLabel(currentTickValue, true);
}
else { //no tick label to be shown
tickLabel = "";
}
}
else { //tick value not between 0 & 1
//show tick label if it's the first or last in
// the set, or if it's 1-5; beyond that show
// fewer as the values get larger:
tickLabel = (j < 1 || (i < 1 && j < 5) || (j < 4 - i)
|| currentTickValue >= upperBoundVal)
? makeTickLabel(currentTickValue) : "";
}
}
else { //not small log values in use; allow for values <= 0
if (zeroTickFlag) { //if did zero tick last iter then
--j; //decrement to do 1.0 tick now
} //calculate power-of-ten value for tick:
currentTickValue = (i >= 0)
? Math.pow(10, i) + (Math.pow(10, i) * j)
: -(Math.pow(10, -i) - (Math.pow(10, -i - 1) * j));
if (!zeroTickFlag) { //did not do zero tick last iteration
if (Math.abs(currentTickValue - 1.0) < 0.0001
&& lowerBoundVal <= 0.0 && upperBoundVal >= 0.0) {
//tick value is 1.0 and 0.0 is within data range
currentTickValue = 0.0; //set tick value to zero
zeroTickFlag = true; //indicate zero tick
}
}
else { //did zero tick last iteration
zeroTickFlag = false; //clear flag
} //create tick label string:
//show tick label if "1e#"-style and it's one
// of the first two, if it's the first or last
// in the set, or if it's 1-5; beyond that
// show fewer as the values get larger:
tickLabel = ((expTickLabelsFlag && j < 2)
|| j < 1
|| (i < 1 && j < 5) || (j < 4 - i)
|| currentTickValue >= upperBoundVal)
? makeTickLabel(currentTickValue) : "";
}
if (currentTickValue > upperBoundVal) {
return; //if past highest data value then exit method
}
if (currentTickValue >= lowerBoundVal - SMALL_LOG_VALUE) {
//tick value not below lowest data value
double xx = translateValueToJava2D(currentTickValue, dataArea, edge);
Rectangle2D tickLabelBounds = getTickLabelFont().getStringBounds(
tickLabel, g2.getFontRenderContext());
float x = 0.0f;
float y = 0.0f;
Insets tickLabelInsets = getTickLabelInsets();
if (isVerticalTickLabels()) {
x = (float) (xx + tickLabelBounds.getHeight() / 2);
if (edge == RectangleEdge.TOP) {
y = (float) (dataArea.getMinY() - tickLabelInsets.bottom
- tickLabelBounds.getWidth());
}
else {
y = (float) (dataArea.getMaxY() + tickLabelInsets.top
+ tickLabelBounds.getWidth());
}
}
else {
x = (float) (xx - tickLabelBounds.getWidth() / 2);
if (edge == RectangleEdge.TOP) {
y = (float) (dataArea.getMinY() - tickLabelInsets.bottom);
}
else {
y = (float) (dataArea.getMaxY() + tickLabelInsets.top
+ tickLabelBounds.getHeight());
}
}
Tick tick = new Tick(new Double(currentTickValue), tickLabel, x, y);
getTicks().add(tick);
}
}
}
}
/**
* Calculates the positions of the tick labels for the axis, storing the
* results in the tick label list (ready for drawing).
*
* @param g2 the graphics device.
* @param plotArea the area in which the plot and the axes should be drawn.
* @param dataArea the area in which the plot should be drawn.
* @param edge the axis location.
*/
public void refreshTicksVertical(Graphics2D g2,
Rectangle2D plotArea, Rectangle2D dataArea,
RectangleEdge edge) {
getTicks().clear();
//get lower bound value:
double lowerBoundVal = getRange().getLowerBound();
//if small log values and lower bound value too small
// then set to a small value (don't allow <= 0):
if (smallLogFlag && lowerBoundVal < SMALL_LOG_VALUE) {
lowerBoundVal = SMALL_LOG_VALUE;
}
//get upper bound value
final double upperBoundVal = getRange().getUpperBound();
//get log10 version of lower bound and round to integer:
int iBegCount = (int) Math.rint(switchedLog10(lowerBoundVal));
//get log10 version of upper bound and round to integer:
final int iEndCount = (int) Math.rint(switchedLog10(upperBoundVal));
if (iBegCount == iEndCount && iBegCount > 0 && Math.pow(10, iBegCount) > lowerBoundVal) {
//only 1 power of 10 value, it's > 0 and its resulting
// tick value will be larger than lower bound of data
--iBegCount; //decrement to generate more ticks
}
double tickVal;
String tickLabel;
boolean zeroTickFlag = false;
for (int i = iBegCount; i <= iEndCount; i++) {
//for each tick with a label to be displayed
int jEndCount = 10;
if (i == iEndCount) {
jEndCount = 1;
}
for (int j = 0; j < jEndCount; j++) {
//for each tick to be displayed
if (smallLogFlag) {
//small log values in use
tickVal = Math.pow(10, i) + (Math.pow(10, i) * j);
if (j == 0) {
//first tick of group; create label text
if (log10TickLabelsFlag) {
//if flag then
tickLabel = "10^" + i; //create "log10"-type label
}
else { //not "log10"-type label
if (expTickLabelsFlag) {
//if flag then
tickLabel = "1e" + i; //create "1e#"-type label
}
else { //not "1e#"-type label
if (i >= 0) { //if positive exponent then make integer
tickLabel = Long.toString((long) Math.rint(tickVal));
}
else {
//negative exponent; create fractional value
//set exact number of fractional digits to be shown:
numberFormatterObj.setMaximumFractionDigits(-i);
//create tick label:
tickLabel = numberFormatterObj.format(tickVal);
}
}
}
}
else { //not first tick to be displayed
tickLabel = ""; //no tick label
}
}
else { //not small log values in use; allow for values <= 0
if (zeroTickFlag) { //if did zero tick last iter then
--j;
} //decrement to do 1.0 tick now
tickVal = (i >= 0) ? Math.pow(10, i) + (Math.pow(10, i) * j)
: -(Math.pow(10, -i) - (Math.pow(10, -i - 1) * j));
if (j == 0) { //first tick of group
if (!zeroTickFlag) { //did not do zero tick last iteration
if (i > iBegCount && i < iEndCount
&& Math.abs(tickVal - 1.0) < 0.0001) {
//not first or last tick on graph and value is 1.0
tickVal = 0.0; //change value to 0.0
zeroTickFlag = true; //indicate zero tick
tickLabel = "0"; //create label for tick
}
else {
//first or last tick on graph or value is 1.0
//create label for tick:
if (log10TickLabelsFlag) {
//create "log10"-type label
tickLabel = (((i < 0) ? "-" : "") + "10^" + Math.abs(i));
}
else {
if (expTickLabelsFlag) {
//create "1e#"-type label
tickLabel = (((i < 0) ? "-" : "") + "1e" + Math.abs(i));
}
else {
//create regular numeric label
tickLabel = Long.toString((long) Math.rint(tickVal));
}
}
}
}
else { // did zero tick last iteration
tickLabel = ""; //no label
zeroTickFlag = false; //clear flag
}
}
else { // not first tick of group
tickLabel = ""; //no label
zeroTickFlag = false; //make sure flag cleared
}
}
if (tickVal > upperBoundVal) {
return; //if past highest data value then exit method
}
if (tickVal >= lowerBoundVal - SMALL_LOG_VALUE) {
//tick value not below lowest data value
//get Y-position for tick:
double yy = translateValueToJava2D(tickVal, dataArea, edge);
//get bounds for tick label:
Rectangle2D tickLabelBounds
= getTickLabelFont().getStringBounds(tickLabel, g2.getFontRenderContext());
//get X-position for tick label:
float x;
if (edge == RectangleEdge.LEFT) {
x = (float) (dataArea.getX()
- tickLabelBounds.getWidth() - getTickLabelInsets().right);
}
else {
x = (float) (dataArea.getMaxX() + getTickLabelInsets().left);
}
//get Y-position for tick label:
float y = (float) (yy + (tickLabelBounds.getHeight() / 3));
//create tick object and add to list:
getTicks().add(new Tick(new Double(tickVal), tickLabel, x, y));
}
}
}
}
/**
* Converts the given value to a tick label string.
*
* @param val the value to convert.
* @param forceFmtFlag true to force the number-formatter object
* to be used.
*
* @return The tick label string.
*/
protected String makeTickLabel(double val, boolean forceFmtFlag) {
if (expTickLabelsFlag || forceFmtFlag) {
//using exponents or force-formatter flag is set
// (convert 'E' to lower-case 'e'):
return numberFormatterObj.format(val).toLowerCase();
}
return getTickUnit().valueToString(val);
}
/**
* Converts the given value to a tick label string.
* @param val the value to convert.
*
* @return The tick label string.
*/
protected String makeTickLabel(double val) {
return makeTickLabel(val, false);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -