Skip to content

Commit 10f3076

Browse files
committed
Merge pull request #873 from danielgindi/bug-fixes
Bug fixes
2 parents 5ac58fc + 1f571a3 commit 10f3076

File tree

7 files changed

+71
-55
lines changed

7 files changed

+71
-55
lines changed

MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,39 @@ public void feed(List<BarEntry> entries) {
8484

8585
} else {
8686

87-
float allPos = e.getPositiveSum();
88-
float allNeg = e.getNegativeSum();
87+
float posY = 0f;
88+
float negY = 0f;
89+
float yStart = 0f;
8990

9091
// fill the stack
9192
for (int k = 0; k < vals.length; k++) {
9293

9394
float value = vals[k];
9495

9596
if(value >= 0f) {
96-
97-
allPos -= value;
98-
y = value + allPos;
97+
y = posY;
98+
yStart = posY + value;
99+
posY = yStart;
99100
} else {
100-
allNeg -= Math.abs(value);
101-
y = value + allNeg;
101+
y = negY;
102+
yStart = negY + value;
103+
negY = yStart;
102104
}
103105

104106
float left = x - barWidth + barSpaceHalf;
105107
float right = x + barWidth - barSpaceHalf;
106108
float bottom, top;
107109
if (mInverted) {
108-
bottom = y >= 0 ? y : 0;
109-
top = y <= 0 ? y : 0;
110+
bottom = y >= yStart ? y : yStart;
111+
top = y <= yStart ? y : yStart;
110112
} else {
111-
top = y >= 0 ? y : 0;
112-
bottom = y <= 0 ? y : 0;
113+
top = y >= yStart ? y : yStart;
114+
bottom = y <= yStart ? y : yStart;
113115
}
114116

115117
// multiply the height of the rect with the phase
116-
if (top > 0)
117-
top *= phaseY;
118-
else
119-
bottom *= phaseY;
118+
top *= phaseY;
119+
bottom *= phaseY;
120120

121121
addBar(left, top, right, bottom);
122122
}

MPChartLib/src/com/github/mikephil/charting/buffer/HorizontalBarBuffer.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,39 @@ public void feed(List<BarEntry> entries) {
5454

5555
} else {
5656

57-
float allPos = e.getPositiveSum();
58-
float allNeg = e.getNegativeSum();
57+
float posY = 0f;
58+
float negY = 0f;
59+
float yStart = 0f;
5960

6061
// fill the stack
6162
for (int k = 0; k < vals.length; k++) {
6263

6364
float value = vals[k];
6465

6566
if(value >= 0f) {
66-
67-
allPos -= value;
68-
y = value + allPos;
67+
y = posY;
68+
yStart = posY + value;
69+
posY = yStart;
6970
} else {
70-
allNeg -= Math.abs(value);
71-
y = value + allNeg;
71+
y = negY;
72+
yStart = negY + value;
73+
negY = yStart;
7274
}
7375

7476
float bottom = x - barWidth + barSpaceHalf;
7577
float top = x + barWidth - barSpaceHalf;
7678
float left, right;
7779
if (mInverted) {
78-
left = y >= 0 ? y : 0;
79-
right = y <= 0 ? y : 0;
80+
left = y >= yStart ? y : yStart;
81+
right = y <= yStart ? y : yStart;
8082
} else {
81-
right = y >= 0 ? y : 0;
82-
left = y <= 0 ? y : 0;
83+
right = y >= yStart ? y : yStart;
84+
left = y <= yStart ? y : yStart;
8385
}
8486

8587
// multiply the height of the rect with the phase
86-
if (right > 0)
87-
right *= phaseY;
88-
else
89-
left *= phaseY;
88+
right *= phaseY;
89+
left *= phaseY;
9090

9191
addBar(left, top, right, bottom);
9292
}

MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,25 @@ protected void calcMinMax() {
371371
.getAxisMinValue() : minRight - bottomSpaceRight;
372372

373373
// consider starting at zero (0)
374-
if (mAxisLeft.isStartAtZeroEnabled())
375-
mAxisLeft.mAxisMinimum = 0f;
374+
if (mAxisLeft.isStartAtZeroEnabled()) {
375+
if (mAxisLeft.mAxisMinimum < 0f && mAxisLeft.mAxisMaximum < 0f) {
376+
// If the values are all negative, let's stay in the negative zone
377+
mAxisLeft.mAxisMaximum = 0f;
378+
} else {
379+
// We have positive values, stay in the positive zone
380+
mAxisLeft.mAxisMinimum = 0f;
381+
}
382+
}
376383

377-
if (mAxisRight.isStartAtZeroEnabled())
378-
mAxisRight.mAxisMinimum = 0f;
384+
if (mAxisRight.isStartAtZeroEnabled()) {
385+
if (mAxisRight.mAxisMinimum < 0.0 && mAxisRight.mAxisMaximum < 0.0) {
386+
// If the values are all negative, let's stay in the negative zone
387+
mAxisRight.mAxisMaximum = 0f;
388+
} else {
389+
// We have positive values, stay in the positive zone
390+
mAxisRight.mAxisMinimum = 0f;
391+
}
392+
}
379393

380394
mAxisLeft.mAxisRange = Math.abs(mAxisLeft.mAxisMaximum - mAxisLeft.mAxisMinimum);
381395
mAxisRight.mAxisRange = Math.abs(mAxisRight.mAxisMaximum - mAxisRight.mAxisMinimum);

MPChartLib/src/com/github/mikephil/charting/data/ChartData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void calcMinMax(int start, int end) {
203203
mLastEnd = end;
204204

205205
mYMin = Float.MAX_VALUE;
206-
mYMax = -Float.MIN_VALUE;
206+
mYMax = -Float.MAX_VALUE;
207207

208208
for (int i = 0; i < mDataSets.size(); i++) {
209209

MPChartLib/src/com/github/mikephil/charting/data/DataSet.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,23 @@ public void notifyDataSetChanged() {
109109
* calc minimum and maximum y value
110110
*/
111111
protected void calcMinMax(int start, int end) {
112-
if (mYVals.size() == 0)
112+
final int yValCount = mYVals.size();
113+
114+
if (yValCount == 0)
113115
return;
114116

115117
int endValue;
116118

117-
if (end == 0)
118-
endValue = mYVals.size() - 1;
119+
if (end == 0 || end >= yValCount)
120+
endValue = yValCount - 1;
119121
else
120122
endValue = end;
121123

122124
mLastStart = start;
123125
mLastEnd = endValue;
124126

125127
mYMin = Float.MAX_VALUE;
126-
mYMax = -Float.MIN_VALUE;
128+
mYMax = -Float.MAX_VALUE;
127129

128130
for (int i = start; i <= endValue; i++) {
129131

MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,21 @@ public void drawValues(Canvas c) {
260260
} else {
261261

262262
float[] transformed = new float[vals.length * 2];
263-
float allPos = e.getPositiveSum();
264-
float allNeg = e.getNegativeSum();
263+
264+
float posY = 0f;
265+
float negY = 0f;
265266

266267
for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) {
267268

268269
float value = vals[idx];
269270
float y;
270271

271-
if(value >= 0f) {
272-
273-
allPos -= value;
274-
y = value + allPos;
272+
if (value >= 0f) {
273+
posY += value;
274+
y = posY;
275275
} else {
276-
allNeg -= Math.abs(value);
277-
y = value + allNeg;
276+
negY += value;
277+
y = negY;
278278
}
279279

280280
transformed[k + 1] = y * mAnimator.getPhaseY();

MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -206,21 +206,21 @@ public void drawValues(Canvas c) {
206206
} else {
207207

208208
float[] transformed = new float[vals.length * 2];
209-
float allPos = e.getPositiveSum();
210-
float allNeg = e.getNegativeSum();
209+
210+
float posY = 0f;
211+
float negY = 0f;
211212

212213
for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) {
213214

214215
float value = vals[idx];
215216
float y;
216217

217-
if(value >= 0f) {
218-
219-
allPos -= value;
220-
y = value + allPos;
218+
if (value >= 0f) {
219+
posY += value;
220+
y = posY;
221221
} else {
222-
allNeg -= Math.abs(value);
223-
y = value + allNeg;
222+
negY += value;
223+
y = negY;
224224
}
225225

226226
transformed[k] = y * mAnimator.getPhaseY();

0 commit comments

Comments
 (0)