Skip to content

Commit ae2d917

Browse files
committed
Refactoring, tests and fixes related to min and max calculation
1 parent 33b1581 commit ae2d917

10 files changed

Lines changed: 179 additions & 156 deletions

File tree

‎MPChartExample/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ buildscript {
3838
jcenter()
3939
}
4040
dependencies {
41-
classpath 'com.android.tools.build:gradle:2.1.0'
41+
classpath 'com.android.tools.build:gradle:2.1.2'
4242
//classpath 'io.realm:realm-gradle-plugin:0.88.2'
4343
// NOTE: Do not place your application dependencies here; they belong
4444
// in the individual module build.gradle files

‎MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,5 @@ public void calculate(float dataMin, float dataMax) {
375375

376376
// calc actual range
377377
this.mAxisRange = Math.abs(this.mAxisMaximum - this.mAxisMinimum);
378-
379-
// // in case granularity is not customized, auto-calculate it
380-
// if (!mCustomGranularity && mGranularityEnabled) {
381-
//
382-
// double granularity = Utils.granularity(mAxisRange, mLabelCount);
383-
// this.mGranularity = (float) granularity;
384-
// }
385378
}
386379
}

‎MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,13 @@ private void calcStackSize(List<BarEntry> yVals) {
108108
@Override
109109
public void calcMinMax() {
110110

111-
if (mValues == null)
112-
return;
113-
114-
if (mValues.size() == 0)
111+
if (mValues == null || mValues.isEmpty())
115112
return;
116113

114+
mYMax = Float.MIN_VALUE;
117115
mYMin = Float.MAX_VALUE;
118-
mYMax = -Float.MAX_VALUE;
119-
116+
mXMax = Float.MIN_VALUE;
120117
mXMin = Float.MAX_VALUE;
121-
mXMax = -Float.MAX_VALUE;
122118

123119
for (BarEntry e : mValues) {
124120

@@ -147,16 +143,6 @@ public void calcMinMax() {
147143
mXMax = e.getX();
148144
}
149145
}
150-
151-
if (mYMin == Float.MAX_VALUE) {
152-
mYMin = 0.f;
153-
mYMax = 0.f;
154-
}
155-
156-
if(mXMin == Float.MAX_VALUE) {
157-
mXMin = 0.f;
158-
mXMax = 0.f;
159-
}
160146
}
161147

162148
@Override

‎MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,13 @@ public float getHighlightCircleWidth() {
3131
@Override
3232
public void calcMinMax() {
3333

34-
if (mValues == null)
35-
return;
36-
37-
if (mValues.size() == 0)
34+
if (mValues == null || mValues.isEmpty())
3835
return;
3936

37+
mYMax = Float.MIN_VALUE;
4038
mYMin = Float.MAX_VALUE;
41-
mYMax = -Float.MAX_VALUE;
42-
39+
mXMax = Float.MIN_VALUE;
4340
mXMin = Float.MAX_VALUE;
44-
mXMax = -Float.MAX_VALUE;
4541

4642
// need chart width to guess this properly
4743

@@ -75,16 +71,6 @@ public void calcMinMax() {
7571
mMaxSize = size;
7672
}
7773
}
78-
79-
if (mYMin == Float.MAX_VALUE) {
80-
mYMin = 0.f;
81-
mYMax = 0.f;
82-
}
83-
84-
if(mXMin == Float.MAX_VALUE) {
85-
mXMin = 0.f;
86-
mXMax = 0.f;
87-
}
8874
}
8975

9076
@Override

‎MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,13 @@ public DataSet<CandleEntry> copy() {
102102
@Override
103103
public void calcMinMax() {
104104

105-
if (mValues == null)
106-
return;
107-
108-
if (mValues.size() == 0)
105+
if (mValues == null || mValues.isEmpty())
109106
return;
110107

108+
mYMax = Float.MIN_VALUE;
111109
mYMin = Float.MAX_VALUE;
112-
mYMax = -Float.MAX_VALUE;
113-
110+
mXMax = Float.MIN_VALUE;
114111
mXMin = Float.MAX_VALUE;
115-
mXMax = -Float.MAX_VALUE;
116112

117113
for (CandleEntry e : mValues) {
118114

@@ -128,16 +124,6 @@ public void calcMinMax() {
128124
if (e.getX() > mXMax)
129125
mXMax = e.getX();
130126
}
131-
132-
if (mYMin == Float.MAX_VALUE) {
133-
mYMin = 0.f;
134-
mYMax = 0.f;
135-
}
136-
137-
if(mXMin == Float.MAX_VALUE) {
138-
mXMin = 0.f;
139-
mXMax = 0.f;
140-
}
141127
}
142128

143129
/**

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

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@ public abstract class ChartData<T extends IDataSet<? extends Entry>> {
2424
/**
2525
* maximum y-value in the value array across all axes
2626
*/
27-
protected float mYMax = 0.0f;
27+
protected float mYMax = -Float.MAX_VALUE;
2828

2929
/**
3030
* the minimum y-value in the value array across all axes
3131
*/
32-
protected float mYMin = 0.0f;
32+
protected float mYMin = Float.MAX_VALUE;
3333

3434
/**
3535
* maximum x-value in the value array
3636
*/
37-
protected float mXMax = 0f;
37+
protected float mXMax = -Float.MAX_VALUE;
3838

3939
/**
4040
* minimum x-value in the value array
4141
*/
42-
protected float mXMin = 0f;
42+
protected float mXMin = Float.MAX_VALUE;
4343

44-
protected float mLeftAxisMax = Float.MIN_VALUE;
44+
45+
protected float mLeftAxisMax = -Float.MAX_VALUE;
4546

4647
protected float mLeftAxisMin = Float.MAX_VALUE;
4748

48-
protected float mRightAxisMax = Float.MIN_VALUE;
49+
protected float mRightAxisMax = -Float.MAX_VALUE;
4950

5051
protected float mRightAxisMin = Float.MAX_VALUE;
5152

@@ -125,68 +126,57 @@ public void notifyDataChanged() {
125126
*/
126127
public void calcMinMax() {
127128

128-
if (mDataSets == null || mDataSets.size() < 1) {
129-
130-
mYMax = 0f;
131-
mYMin = 0f;
132-
133-
mXMax = 0f;
134-
mXMin = 0f;
135-
136-
} else {
137-
138-
mYMin = Float.MAX_VALUE;
139-
mYMax = -Float.MAX_VALUE;
140-
141-
mXMin = Float.MAX_VALUE;
142-
mXMax = -Float.MAX_VALUE;
129+
if (mDataSets == null)
130+
return;
143131

144-
for (int i = 0; i < mDataSets.size(); i++) {
132+
mYMax = -Float.MAX_VALUE;
133+
mYMin = Float.MAX_VALUE;
134+
mXMax = -Float.MAX_VALUE;
135+
mXMin = Float.MAX_VALUE;
145136

146-
T set = mDataSets.get(i);
147-
calcMinMax(set);
148-
}
137+
for (T set : mDataSets) {
138+
calcMinMax(set);
139+
}
149140

150-
if (mYMin == Float.MAX_VALUE) {
151-
mYMin = 0.f;
152-
mYMax = 0.f;
153-
}
141+
mLeftAxisMax = -Float.MAX_VALUE;
142+
mLeftAxisMin = Float.MAX_VALUE;
143+
mRightAxisMax = -Float.MAX_VALUE;
144+
mRightAxisMin = Float.MAX_VALUE;
154145

155-
// left axis
156-
T firstLeft = getFirstLeft();
146+
// left axis
147+
T firstLeft = getFirstLeft();
157148

158-
if (firstLeft != null) {
149+
if (firstLeft != null) {
159150

160-
mLeftAxisMax = firstLeft.getYMax();
161-
mLeftAxisMin = firstLeft.getYMin();
151+
mLeftAxisMax = firstLeft.getYMax();
152+
mLeftAxisMin = firstLeft.getYMin();
162153

163-
for (IDataSet dataSet : mDataSets) {
164-
if (dataSet.getAxisDependency() == AxisDependency.LEFT) {
165-
if (dataSet.getYMin() < mLeftAxisMin)
166-
mLeftAxisMin = dataSet.getYMin();
154+
for (IDataSet dataSet : mDataSets) {
155+
if (dataSet.getAxisDependency() == AxisDependency.LEFT) {
156+
if (dataSet.getYMin() < mLeftAxisMin)
157+
mLeftAxisMin = dataSet.getYMin();
167158

168-
if (dataSet.getYMax() > mLeftAxisMax)
169-
mLeftAxisMax = dataSet.getYMax();
170-
}
159+
if (dataSet.getYMax() > mLeftAxisMax)
160+
mLeftAxisMax = dataSet.getYMax();
171161
}
172162
}
163+
}
173164

174-
// right axis
175-
T firstRight = getFirstRight();
165+
// right axis
166+
T firstRight = getFirstRight();
176167

177-
if (firstRight != null) {
168+
if (firstRight != null) {
178169

179-
mRightAxisMax = firstRight.getYMax();
180-
mRightAxisMin = firstRight.getYMin();
170+
mRightAxisMax = firstRight.getYMax();
171+
mRightAxisMin = firstRight.getYMin();
181172

182-
for (IDataSet dataSet : mDataSets) {
183-
if (dataSet.getAxisDependency() == AxisDependency.RIGHT) {
184-
if (dataSet.getYMin() < mRightAxisMin)
185-
mRightAxisMin = dataSet.getYMin();
173+
for (IDataSet dataSet : mDataSets) {
174+
if (dataSet.getAxisDependency() == AxisDependency.RIGHT) {
175+
if (dataSet.getYMin() < mRightAxisMin)
176+
mRightAxisMin = dataSet.getYMin();
186177

187-
if (dataSet.getYMax() > mRightAxisMax)
188-
mRightAxisMax = dataSet.getYMax();
189-
}
178+
if (dataSet.getYMax() > mRightAxisMax)
179+
mRightAxisMax = dataSet.getYMax();
190180
}
191181
}
192182
}
@@ -275,12 +265,12 @@ public float getYMax() {
275265
public float getYMax(AxisDependency axis) {
276266
if (axis == AxisDependency.LEFT) {
277267

278-
if (mLeftAxisMax == Float.MIN_VALUE) {
268+
if (mLeftAxisMax == -Float.MAX_VALUE) {
279269
return mRightAxisMax;
280270
} else
281271
return mLeftAxisMax;
282272
} else {
283-
if (mRightAxisMax == Float.MIN_VALUE) {
273+
if (mRightAxisMax == -Float.MAX_VALUE) {
284274
return mLeftAxisMax;
285275
} else
286276
return mRightAxisMax;
@@ -325,6 +315,11 @@ public int getYValCount() {
325315
return mValueCount;
326316
}
327317

318+
/**
319+
* Returns all DataSet objects this ChartData object holds.
320+
*
321+
* @return
322+
*/
328323
public List<T> getDataSets() {
329324
return mDataSets;
330325
}
@@ -578,23 +573,23 @@ public boolean removeEntry(Entry e, int dataSetIndex) {
578573
}
579574

580575
/**
581-
* Removes the Entry object at the given xIndex from the DataSet at the
576+
* Removes the Entry object closest to the given DataSet at the
582577
* specified index. Returns true if an Entry was removed, false if no Entry
583578
* was found that meets the specified requirements.
584579
*
585-
* @param xIndex
580+
* @param xPos
586581
* @param dataSetIndex
587582
* @return
588583
*/
589-
public boolean removeEntry(int xIndex, int dataSetIndex) {
584+
public boolean removeEntry(float xPos, int dataSetIndex) {
590585

591586
if (dataSetIndex >= mDataSets.size())
592587
return false;
593588

594589
IDataSet dataSet = mDataSets.get(dataSetIndex);
595-
Entry e = dataSet.getEntryForXPos(xIndex);
590+
Entry e = dataSet.getEntryForXPos(xPos);
596591

597-
if (e == null || e.getX() != xIndex)
592+
if (e == null)
598593
return false;
599594

600595
return removeEntry(e, dataSetIndex);
@@ -658,13 +653,14 @@ public int[] getColors() {
658653
return colors;
659654
}
660655

656+
/**
657+
* Returns the index of the provided DataSet in the DataSet array of this data object, or -1 if it does not exist.
658+
*
659+
* @param dataSet
660+
* @return
661+
*/
661662
public int getIndexOfDataSet(T dataSet) {
662-
for (int i = 0; i < mDataSets.size(); i++) {
663-
if (mDataSets.get(i) == dataSet)
664-
return i;
665-
}
666-
667-
return -1;
663+
return mDataSets.indexOf(dataSet);
668664
}
669665

670666
/**
@@ -678,7 +674,6 @@ public T getFirstLeft() {
678674
if (dataSet.getAxisDependency() == AxisDependency.LEFT)
679675
return dataSet;
680676
}
681-
682677
return null;
683678
}
684679

@@ -693,7 +688,6 @@ public T getFirstRight() {
693688
if (dataSet.getAxisDependency() == AxisDependency.RIGHT)
694689
return dataSet;
695690
}
696-
697691
return null;
698692
}
699693

@@ -840,12 +834,15 @@ public int getEntryCount() {
840834
}
841835

842836
/**
843-
* Returns the DataSet object with the maximum number of entries.
837+
* Returns the DataSet object with the maximum number of entries or null if there are no DataSets.
844838
*
845839
* @return
846840
*/
847841
public T getMaxEntryCountSet() {
848842

843+
if (mDataSets == null || mDataSets.isEmpty())
844+
return null;
845+
849846
T max = mDataSets.get(0);
850847

851848
for (T set : mDataSets) {

0 commit comments

Comments
 (0)