Skip to content

Commit

Permalink
Adds ability to set human rounding in X separately from Y.
Browse files Browse the repository at this point in the history
  • Loading branch information
dektar committed Apr 4, 2017
1 parent 98b3b10 commit e85ac64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
38 changes: 29 additions & 9 deletions src/main/java/com/jjoe64/graphview/GridLabelRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ public void setSecondScaleLabelVerticalWidth(Integer newWidth) {
mLabelVerticalSecondScaleWidth = newWidth;
}

/**
* activate or deactivate human rounding of the
* horizontal axis. GraphView tries to fit the labels
* to display numbers that can be divided by 1, 2, or 5.
*/
private boolean mHumanRoundingY;

/**
* activate or deactivate human rounding of the
* horizontal axis. GraphView tries to fit the labels
Expand All @@ -332,7 +339,7 @@ public void setSecondScaleLabelVerticalWidth(Integer newWidth) {
* By default this is enabled. It makes sense to deactivate it
* when using Dates on the x axis.
*/
private boolean mHumanRounding;
private boolean mHumanRoundingX;

/**
* create the default grid label renderer.
Expand All @@ -346,7 +353,8 @@ public GridLabelRenderer(GraphView graphView) {
resetStyles();
mNumVerticalLabels = 5;
mNumHorizontalLabels = 5;
mHumanRounding = true;
mHumanRoundingX = true;
mHumanRoundingY = true;
}

/**
Expand Down Expand Up @@ -436,8 +444,18 @@ public void reloadStyles() {
* @return if human rounding is enabled
*/
public boolean isHumanRounding() {
return mHumanRounding;
public boolean isHumanRoundingX() {
return mHumanRoundingX;
}

/**
* GraphView tries to fit the labels
* to display numbers that can be divided by 1, 2, or 5.
*
* @return if human rounding is enabled
*/
public boolean isHumanRoundingY() {
return mHumanRoundingY;
}

/**
Expand All @@ -448,10 +466,12 @@ public boolean isHumanRounding() {
* By default this is enabled. It makes sense to deactivate it
* when using Dates on the x axis.
*
* @param humanRounding false to deactivate
* @param humanRoundingX false to deactivate
* @param humanRoundingY false to deactivate
*/
public void setHumanRounding(boolean humanRounding) {
this.mHumanRounding = humanRounding;
public void setHumanRounding(boolean humanRoundingX, boolean humanRoundingY) {
this.mHumanRoundingX = humanRoundingX;
this.mHumanRoundingY = humanRoundingY;
}

/**
Expand Down Expand Up @@ -692,7 +712,7 @@ protected boolean adjustVertical(boolean changeBounds) {
}

// human rounding to have nice numbers (1, 2, 5, ...)
if (isHumanRounding()) {
if (isHumanRoundingY()) {
exactSteps = humanRound(exactSteps, changeBounds);
} else if (mStepsVertical != null && mStepsVertical.size() > 1) {
// else choose other nice steps that previous
Expand Down Expand Up @@ -831,7 +851,7 @@ protected boolean adjustHorizontal(boolean changeBounds) {
}

// human rounding to have nice numbers (1, 2, 5, ...)
if (isHumanRounding()) {
if (isHumanRoundingX()) {
exactSteps = humanRound(exactSteps, false);
} else if (mStepsHorizontal != null && mStepsHorizontal.size() > 1) {
// else choose other nice steps that previous
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/jjoe64/graphview/Viewport.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public class Viewport {
/**
* this reference value is used to generate the
* vertical labels. It is used when the y axis bounds
* is set manual and humanRounding=false. it will be the minValueY value.
* is set manual and humanRoundingY=false. it will be the minValueY value.
*/
protected double referenceY = Double.NaN;

/**
* this reference value is used to generate the
* horizontal labels. It is used when the x axis bounds
* is set manual and humanRounding=false. it will be the minValueX value.
* is set manual and humanRoundingX=false. it will be the minValueX value.
*/
protected double referenceX = Double.NaN;

Expand All @@ -83,7 +83,7 @@ public class Viewport {
protected double getReferenceX() {
// if the bounds is manual then we take the
// original manual min y value as reference
if (isXAxisBoundsManual() && !mGraphView.getGridLabelRenderer().isHumanRounding()) {
if (isXAxisBoundsManual() && !mGraphView.getGridLabelRenderer().isHumanRoundingX()) {
if (Double.isNaN(referenceX)) {
referenceX = getMinX(false);
}
Expand Down Expand Up @@ -1263,7 +1263,7 @@ public void setScrollableY(boolean scrollableY) {
protected double getReferenceY() {
// if the bounds is manual then we take the
// original manual min y value as reference
if (isYAxisBoundsManual() && !mGraphView.getGridLabelRenderer().isHumanRounding()) {
if (isYAxisBoundsManual() && !mGraphView.getGridLabelRenderer().isHumanRoundingY()) {
if (Double.isNaN(referenceY)) {
referenceY = getMinY(false);
}
Expand Down

0 comments on commit e85ac64

Please sign in to comment.