diff --git a/MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java b/MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java index a24a1120c8..c143935f74 100644 --- a/MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java +++ b/MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java @@ -84,8 +84,9 @@ public void feed(List entries) { } else { - float allPos = e.getPositiveSum(); - float allNeg = e.getNegativeSum(); + float posY = 0f; + float negY = 0f; + float yStart = 0f; // fill the stack for (int k = 0; k < vals.length; k++) { @@ -93,30 +94,29 @@ public void feed(List entries) { float value = vals[k]; if(value >= 0f) { - - allPos -= value; - y = value + allPos; + y = posY; + yStart = posY + value; + posY = yStart; } else { - allNeg -= Math.abs(value); - y = value + allNeg; + y = negY; + yStart = negY + value; + negY = yStart; } float left = x - barWidth + barSpaceHalf; float right = x + barWidth - barSpaceHalf; float bottom, top; if (mInverted) { - bottom = y >= 0 ? y : 0; - top = y <= 0 ? y : 0; + bottom = y >= yStart ? y : yStart; + top = y <= yStart ? y : yStart; } else { - top = y >= 0 ? y : 0; - bottom = y <= 0 ? y : 0; + top = y >= yStart ? y : yStart; + bottom = y <= yStart ? y : yStart; } // multiply the height of the rect with the phase - if (top > 0) - top *= phaseY; - else - bottom *= phaseY; + top *= phaseY; + bottom *= phaseY; addBar(left, top, right, bottom); } diff --git a/MPChartLib/src/com/github/mikephil/charting/buffer/HorizontalBarBuffer.java b/MPChartLib/src/com/github/mikephil/charting/buffer/HorizontalBarBuffer.java index d405466f69..06814f9fa0 100644 --- a/MPChartLib/src/com/github/mikephil/charting/buffer/HorizontalBarBuffer.java +++ b/MPChartLib/src/com/github/mikephil/charting/buffer/HorizontalBarBuffer.java @@ -54,8 +54,9 @@ public void feed(List entries) { } else { - float allPos = e.getPositiveSum(); - float allNeg = e.getNegativeSum(); + float posY = 0f; + float negY = 0f; + float yStart = 0f; // fill the stack for (int k = 0; k < vals.length; k++) { @@ -63,30 +64,29 @@ public void feed(List entries) { float value = vals[k]; if(value >= 0f) { - - allPos -= value; - y = value + allPos; + y = posY; + yStart = posY + value; + posY = yStart; } else { - allNeg -= Math.abs(value); - y = value + allNeg; + y = negY; + yStart = negY + value; + negY = yStart; } float bottom = x - barWidth + barSpaceHalf; float top = x + barWidth - barSpaceHalf; float left, right; if (mInverted) { - left = y >= 0 ? y : 0; - right = y <= 0 ? y : 0; + left = y >= yStart ? y : yStart; + right = y <= yStart ? y : yStart; } else { - right = y >= 0 ? y : 0; - left = y <= 0 ? y : 0; + right = y >= yStart ? y : yStart; + left = y <= yStart ? y : yStart; } // multiply the height of the rect with the phase - if (right > 0) - right *= phaseY; - else - left *= phaseY; + right *= phaseY; + left *= phaseY; addBar(left, top, right, bottom); } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index db0639e4da..fad6e03023 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -371,11 +371,25 @@ protected void calcMinMax() { .getAxisMinValue() : minRight - bottomSpaceRight; // consider starting at zero (0) - if (mAxisLeft.isStartAtZeroEnabled()) - mAxisLeft.mAxisMinimum = 0f; + if (mAxisLeft.isStartAtZeroEnabled()) { + if (mAxisLeft.mAxisMinimum < 0f && mAxisLeft.mAxisMaximum < 0f) { + // If the values are all negative, let's stay in the negative zone + mAxisLeft.mAxisMaximum = 0f; + } else { + // We have positive values, stay in the positive zone + mAxisLeft.mAxisMinimum = 0f; + } + } - if (mAxisRight.isStartAtZeroEnabled()) - mAxisRight.mAxisMinimum = 0f; + if (mAxisRight.isStartAtZeroEnabled()) { + if (mAxisRight.mAxisMinimum < 0.0 && mAxisRight.mAxisMaximum < 0.0) { + // If the values are all negative, let's stay in the negative zone + mAxisRight.mAxisMaximum = 0f; + } else { + // We have positive values, stay in the positive zone + mAxisRight.mAxisMinimum = 0f; + } + } mAxisLeft.mAxisRange = Math.abs(mAxisLeft.mAxisMaximum - mAxisLeft.mAxisMinimum); mAxisRight.mAxisRange = Math.abs(mAxisRight.mAxisMaximum - mAxisRight.mAxisMinimum); diff --git a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java index e004b08e13..39db210106 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java @@ -203,7 +203,7 @@ public void calcMinMax(int start, int end) { mLastEnd = end; mYMin = Float.MAX_VALUE; - mYMax = -Float.MIN_VALUE; + mYMax = -Float.MAX_VALUE; for (int i = 0; i < mDataSets.size(); i++) { diff --git a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java index 1d08ad783d..902d032fdc 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java @@ -109,13 +109,15 @@ public void notifyDataSetChanged() { * calc minimum and maximum y value */ protected void calcMinMax(int start, int end) { - if (mYVals.size() == 0) + final int yValCount = mYVals.size(); + + if (yValCount == 0) return; int endValue; - if (end == 0) - endValue = mYVals.size() - 1; + if (end == 0 || end >= yValCount) + endValue = yValCount - 1; else endValue = end; @@ -123,7 +125,7 @@ protected void calcMinMax(int start, int end) { mLastEnd = endValue; mYMin = Float.MAX_VALUE; - mYMax = -Float.MIN_VALUE; + mYMax = -Float.MAX_VALUE; for (int i = start; i <= endValue; i++) { diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java index 5bb30e0a85..ea912a7c9f 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java @@ -260,21 +260,21 @@ public void drawValues(Canvas c) { } else { float[] transformed = new float[vals.length * 2]; - float allPos = e.getPositiveSum(); - float allNeg = e.getNegativeSum(); + + float posY = 0f; + float negY = 0f; for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) { float value = vals[idx]; float y; - if(value >= 0f) { - - allPos -= value; - y = value + allPos; + if (value >= 0f) { + posY += value; + y = posY; } else { - allNeg -= Math.abs(value); - y = value + allNeg; + negY += value; + y = negY; } transformed[k + 1] = y * mAnimator.getPhaseY(); diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java index f0576876bc..7270969cdc 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java @@ -206,21 +206,21 @@ public void drawValues(Canvas c) { } else { float[] transformed = new float[vals.length * 2]; - float allPos = e.getPositiveSum(); - float allNeg = e.getNegativeSum(); + + float posY = 0f; + float negY = 0f; for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) { float value = vals[idx]; float y; - if(value >= 0f) { - - allPos -= value; - y = value + allPos; + if (value >= 0f) { + posY += value; + y = posY; } else { - allNeg -= Math.abs(value); - y = value + allNeg; + negY += value; + y = negY; } transformed[k] = y * mAnimator.getPhaseY();