Skip to content

Commit

Permalink
Fixes and finishing up of autoScaleMinMax feature
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Aug 14, 2016
1 parent 336dfb9 commit 1bbf4be
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ protected void init() {
@Override
protected void calcMinMax() {

if (mAutoScaleMinMaxEnabled)
mData.calcMinMax(getLowestVisibleX(), getHighestVisibleX());

if (mFitBars) {
mXAxis.calculate(mData.getXMin() - mData.getBarWidth() / 2f, mData.getXMax() + mData.getBarWidth() / 2f);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ protected void onDraw(Canvas canvas) {
mAxisRendererRight.renderAxisLine(canvas);

if (mAutoScaleMinMaxEnabled) {

calcMinMax();
calculateOffsets();
autoScale();
}

mXAxisRenderer.renderGridLines(canvas);
Expand Down Expand Up @@ -325,12 +323,29 @@ public void notifyDataSetChanged() {
calculateOffsets();
}

/**
* Performs auto scaling of the axis by recalculating the minimum and maximum y-values based on the entries currently in view.
*/
protected void autoScale() {

final float fromX = getLowestVisibleX();
final float toX = getHighestVisibleX();

mData.calcMinMaxY(fromX, toX);

mXAxis.calculate(mData.getXMin(), mData.getXMax());

// calculate axis range (min / max) according to provided data
mAxisLeft.calculate(mData.getYMin(AxisDependency.LEFT), mData.getYMax(AxisDependency.LEFT));
mAxisRight.calculate(mData.getYMin(AxisDependency.RIGHT), mData.getYMax(AxisDependency
.RIGHT));

calculateOffsets();
}

@Override
protected void calcMinMax() {

if (mAutoScaleMinMaxEnabled)
mData.calcMinMax(getLowestVisibleX(), getHighestVisibleX());

mXAxis.calculate(mData.getXMin(), mData.getXMax());

// calculate axis range (min / max) according to provided data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ protected void calcMinMax(BarEntry e) {
mYMax = e.getPositiveSum();
}

if (e.getX() < mXMin)
mXMin = e.getX();

if (e.getX() > mXMax)
mXMax = e.getX();
calcMinMaxX(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ protected void calcMinMax(CandleEntry e) {
if (e.getHigh() > mYMax)
mYMax = e.getHigh();

if (e.getX() < mXMin)
mXMin = e.getX();

if (e.getX() > mXMax)
mXMax = e.getX();
calcMinMaxX(e);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,19 @@ public void notifyDataChanged() {
}

/**
* Calc minimum and maximum values (both x and y) over all DataSets.
* Tell DataSets to recalculate their min and max values, this is needed for autoScaleMinMax.
* Calc minimum and maximum y-values over all DataSets.
* Tell DataSets to recalculate their min and max y-values, this is only needed for autoScaleMinMax.
*
* @param fromX the x-value to start the calculation from
* @param toX the x-value to which the calculation should be performed
*/
public void calcMinMax(float fromX, float toX) {
public void calcMinMaxY(float fromX, float toX) {

for (T set : mDataSets) {
set.calcMinMax(fromX, toX);
set.calcMinMaxY(fromX, toX);
}

// apply the new data
calcMinMax();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ public void calcMinMax() {
}

@Override
public void calcMinMax(float fromX, float toX) {
public void calcMinMaxY(float fromX, float toX) {

if (mValues == null || mValues.isEmpty())
return;

mYMax = -Float.MAX_VALUE;
mYMin = Float.MAX_VALUE;
mXMax = -Float.MAX_VALUE;
mXMin = Float.MAX_VALUE;

int indexFrom = getEntryIndex(fromX, Rounding.CLOSEST);
int indexTo = getEntryIndex(toX, Rounding.CLOSEST);
int indexFrom = getEntryIndex(fromX, Rounding.DOWN);
int indexTo = getEntryIndex(toX, Rounding.UP);

for (int i = indexFrom; i <= indexTo; i++) {
calcMinMax(mValues.get(i));

// only recalculate y
calcMinMaxY(mValues.get(i));
}
}

Expand All @@ -103,11 +103,12 @@ protected void calcMinMax(T e) {
if (e == null)
return;

if (e.getY() < mYMin)
mYMin = e.getY();
calcMinMaxX(e);

if (e.getY() > mYMax)
mYMax = e.getY();
calcMinMaxY(e);
}

protected void calcMinMaxX(T e) {

if (e.getX() < mXMin)
mXMin = e.getX();
Expand All @@ -116,6 +117,15 @@ protected void calcMinMax(T e) {
mXMax = e.getX();
}

protected void calcMinMaxY(T e) {

if (e.getY() < mYMin)
mYMin = e.getY();

if (e.getY() > mYMax)
mYMax = e.getY();
}

@Override
public int getEntryCount() {
return mValues.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ protected void calcMinMax(PieEntry e) {
if (e == null)
return;

if (e.getY() < mYMin)
mYMin = e.getY();

if (e.getY() > mYMax)
mYMax = e.getY();
calcMinMaxY(e);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ public interface IDataSet<T extends Entry> {
void calcMinMax();

/**
* Calculates the min and max values from the given x-value to the given x-value.
* Calculates the min and max y-values from the Entry closest to the given fromX to the Entry closest to the given toX value.
* This is only needed for the autoScaleMinMax feature.
*
* @param fromX
* @param toX
*/
void calcMinMax(float fromX, float toX);
void calcMinMaxY(float fromX, float toX);

/**
* Returns the first Entry object found at the given x-value with binary
Expand Down

0 comments on commit 1bbf4be

Please sign in to comment.