diff options
author | Philipp Jahoda <phil.jahoda@gmail.com> | 2016-08-14 21:48:05 +0200 |
---|---|---|
committer | Philipp Jahoda <phil.jahoda@gmail.com> | 2016-08-14 21:48:05 +0200 |
commit | 336dfb93e5c6cb681aa8c660de780617a90480be (patch) | |
tree | 4f82b021fd53ff9fd3af0c78334145a679c88ff1 /MPChartLib | |
parent | 66ea845e142119fe7e7df6f04e131eca6ed63346 (diff) | |
download | MPAndroidChart-336dfb93e5c6cb681aa8c660de780617a90480be.tar.gz |
Fix autoScaleMinMax
Diffstat (limited to 'MPChartLib')
8 files changed, 85 insertions, 89 deletions
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java index a149adf9..8fa91bf5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java @@ -63,7 +63,7 @@ public class BarChart extends BarLineChartBase<BarData> implements BarDataProvid 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); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java index 494c8bce..a80530bd 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -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 @@ -206,20 +204,9 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData< 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); @@ -258,7 +245,7 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData< if (!mAxisRight.isDrawLimitLinesBehindDataEnabled()) mAxisRendererRight.renderLimitLines(canvas); - + mXAxisRenderer.renderAxisLabels(canvas); mAxisRendererLeft.renderAxisLabels(canvas); mAxisRendererRight.renderAxisLabels(canvas); @@ -342,7 +329,7 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData< protected void calcMinMax() { if (mAutoScaleMinMaxEnabled) - mData.calcMinMax(); + mData.calcMinMax(getLowestVisibleX(), getHighestVisibleX()); mXAxis.calculate(mData.getXMin(), mData.getXMax()); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java index 63b52221..c11eb2b9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java @@ -107,42 +107,31 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet<BarEntry> impl } @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(); } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java index ec288123..d8c0c130 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java @@ -29,25 +29,13 @@ public class BubbleDataSet extends BarLineScatterCandleBubbleDataSet<BubbleEntry } @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; } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java index 139a1bad..8d883f05 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java @@ -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; @@ -101,30 +101,19 @@ public class CandleDataSet extends LineScatterCandleRadarDataSet<CandleEntry> im } @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(); } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java index 9bb8dbf1..866f258e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java @@ -109,8 +109,24 @@ public abstract class ChartData<T extends IDataSet<? extends Entry>> { /** * 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; diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java index d337a048..6bd5a78c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java @@ -74,6 +74,25 @@ public abstract class DataSet<T extends Entry> extends BaseDataSet<T> { } } + @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. * diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java index 62c10b3f..8130909b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java @@ -60,6 +60,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 * returns the Entry at the x-value according to the rounding. @@ -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(); |