Skip to content

Commit

Permalink
Fix autoScaleMinMax
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Aug 14, 2016
1 parent 66ea845 commit 336dfb9
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void init() {
protected void calcMinMax() {

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

if (mFitBars) {
mXAxis.calculate(mData.getXMin() - mData.getBarWidth() / 2f, mData.getXMax() + mData.getBarWidth() / 2f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<
* flag that indicates if auto scaling on the y axis is enabled
*/
protected boolean mAutoScaleMinMaxEnabled = false;
private Float mAutoScaleLastLowestVisibleXIndex = null;
private Float mAutoScaleLastHighestVisibleXIndex = null;

/**
* flag that indicates if pinch-zoom is enabled. if true, both x and y axis
Expand Down Expand Up @@ -206,20 +204,9 @@ protected void onDraw(Canvas canvas) {
mAxisRendererRight.renderAxisLine(canvas);

if (mAutoScaleMinMaxEnabled) {
final float lowestVisibleXIndex = getLowestVisibleX();
final float highestVisibleXIndex = getHighestVisibleX();

if (mAutoScaleLastLowestVisibleXIndex == null ||
mAutoScaleLastLowestVisibleXIndex != lowestVisibleXIndex ||
mAutoScaleLastHighestVisibleXIndex == null ||
mAutoScaleLastHighestVisibleXIndex != highestVisibleXIndex) {

calcMinMax();
calculateOffsets();

mAutoScaleLastLowestVisibleXIndex = lowestVisibleXIndex;
mAutoScaleLastHighestVisibleXIndex = highestVisibleXIndex;
}
calcMinMax();
calculateOffsets();
}

mXAxisRenderer.renderGridLines(canvas);
Expand Down Expand Up @@ -258,7 +245,7 @@ protected void onDraw(Canvas canvas) {

if (!mAxisRight.isDrawLimitLinesBehindDataEnabled())
mAxisRendererRight.renderLimitLines(canvas);

mXAxisRenderer.renderAxisLabels(canvas);
mAxisRendererLeft.renderAxisLabels(canvas);
mAxisRendererRight.renderAxisLabels(canvas);
Expand Down Expand Up @@ -342,7 +329,7 @@ public void notifyDataSetChanged() {
protected void calcMinMax() {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,42 +107,31 @@ private void calcStackSize(List<BarEntry> yVals) {
}

@Override
public void calcMinMax() {
protected void calcMinMax(BarEntry e) {

if (mValues == null || mValues.isEmpty())
return;
if (e != null && !Float.isNaN(e.getY())) {

mYMax = -Float.MAX_VALUE;
mYMin = Float.MAX_VALUE;
mXMax = -Float.MAX_VALUE;
mXMin = Float.MAX_VALUE;
if (e.getYVals() == null) {

for (BarEntry e : mValues) {
if (e.getY() < mYMin)
mYMin = e.getY();

if (e != null && !Float.isNaN(e.getY())) {
if (e.getY() > mYMax)
mYMax = e.getY();
} else {

if (e.getYVals() == null) {
if (-e.getNegativeSum() < mYMin)
mYMin = -e.getNegativeSum();

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

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

if (-e.getNegativeSum() < mYMin)
mYMin = -e.getNegativeSum();

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,13 @@ public float getHighlightCircleWidth() {
}

@Override
public void calcMinMax() {
protected void calcMinMax(BubbleEntry e) {
super.calcMinMax(e);

if (mValues == null || mValues.isEmpty())
return;
final float size = e.getSize();

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

for (BubbleEntry e : mValues) {

calcMinMax(e);

final float size = e.getSize();

if (size > mMaxSize) {
mMaxSize = size;
}
if (size > mMaxSize) {
mMaxSize = size;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CandleDataSet extends LineScatterCandleRadarDataSet<CandleEntry> im
/**
* should the candle bars show?
* when false, only "ticks" will show
*
* <p/>
* - default: true
*/
private boolean mShowCandleBar = true;
Expand Down Expand Up @@ -101,30 +101,19 @@ public DataSet<CandleEntry> copy() {
}

@Override
public void calcMinMax() {

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

mYMax = -Float.MAX_VALUE;
mYMin = Float.MAX_VALUE;
mXMax = -Float.MAX_VALUE;
mXMin = Float.MAX_VALUE;
protected void calcMinMax(CandleEntry e) {

for (CandleEntry e : mValues) {
if (e.getLow() < mYMin)
mYMin = e.getLow();

if (e.getLow() < mYMin)
mYMin = e.getLow();
if (e.getHigh() > mYMax)
mYMax = e.getHigh();

if (e.getHigh() > mYMax)
mYMax = e.getHigh();
if (e.getX() < mXMin)
mXMin = e.getX();

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,26 @@ public void notifyDataChanged() {
calcMinMax();
}

/**
* 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.
*
* @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) {

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

calcMinMax();
}

/**
* Calc minimum and maximum values (both x and y) over all DataSets.
*/
public void calcMinMax() {
protected void calcMinMax() {

if (mDataSets == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ public void calcMinMax() {
}
}

@Override
public void calcMinMax(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);

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

/**
* Updates the min and max x and y value of this DataSet based on the given Entry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public interface IDataSet<T extends Entry> {
*/
void calcMinMax();

/**
* Calculates the min and max values from the given x-value to the given x-value.
*
* @param fromX
* @param toX
*/
void calcMinMax(float fromX, float toX);

/**
* Returns the first Entry object found at the given x-value with binary
* search. If the no Entry at the specified x-value is found, this method
Expand Down Expand Up @@ -372,28 +380,28 @@ public interface IDataSet<T extends Entry> {

/**
* The form to draw for this dataset in the legend.
*
* <p/>
* Return `DEFAULT` to use the default legend form.
*/
Legend.LegendForm getForm();

/**
* The form size to draw for this dataset in the legend.
*
* <p/>
* Return `Float.NaN` to use the default legend form size.
*/
float getFormSize();

/**
* The line width for drawing the form of this dataset in the legend
*
* <p/>
* Return `Float.NaN` to use the default legend form line width.
*/
float getFormLineWidth();

/**
* The line dash path effect used for shapes that consist of lines.
*
* <p/>
* Return `null` to use the default legend form line dash effect.
*/
DashPathEffect getFormLineDashEffect();
Expand Down

0 comments on commit 336dfb9

Please sign in to comment.