Testing of calculate min max of dataset, changes in remove entry methods

This commit is contained in:
Philipp Jahoda 2016-06-06 22:51:20 +02:00
parent 67911b1317
commit b4109d3248
7 changed files with 144 additions and 76 deletions

View file

@ -91,7 +91,7 @@ public class DynamicalAddingActivity extends DemoBase implements OnChartValueSel
data.removeEntry(e, 0);
// or remove by index
// mData.removeEntry(xIndex, dataSetIndex);
// mData.removeEntryByXPos(xIndex, dataSetIndex);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();

View file

@ -363,9 +363,16 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
}
@Override
public boolean removeEntry(int xIndex) {
public boolean removeEntryByXPos(float xPos) {
T e = getEntryForXPos(xIndex);
T e = getEntryForXPos(xPos);
return removeEntry(e);
}
@Override
public boolean removeEntry(int index) {
T e = getEntryForIndex(index);
return removeEntry(e);
}

View file

@ -7,7 +7,6 @@ import android.util.Log;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import java.util.ArrayList;

View file

@ -76,18 +76,7 @@ public abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
for (T e : mValues) {
if (e != null && !Float.isNaN(e.getY())) {
if (e.getY() < mYMin)
mYMin = e.getY();
if (e.getY() > mYMax)
mYMax = e.getY();
if (e.getX() < mXMin)
mXMin = e.getX();
if (e.getX() > mXMax)
mXMax = e.getX();
calcMinMax(e);
}
}
@ -102,6 +91,26 @@ public abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
}
}
/**
* Updates the min and max x and y value of this DataSet based on the given Entry.
*
* @param e
*/
protected void calcMinMax(T e) {
if (e.getY() < mYMin)
mYMin = e.getY();
if (e.getY() > mYMax)
mYMax = e.getY();
if (e.getX() < mXMin)
mXMin = e.getX();
if (e.getX() > mXMax)
mXMax = e.getX();
}
@Override
public int getEntryCount() {
return mValues.size();
@ -182,21 +191,11 @@ public abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
if (e == null)
return;
float val = e.getY();
if (mValues == null) {
mValues = new ArrayList<T>();
}
if (mValues.size() == 0) {
mYMax = val;
mYMin = val;
} else {
if (mYMax < val)
mYMax = val;
if (mYMin > val)
mYMin = val;
}
calcMinMax(e);
if (mValues.size() > 0 && mValues.get(mValues.size() - 1).getX() > e.getX()) {
int closestIndex = getEntryIndex(e.getX(), Rounding.UP);
@ -219,25 +218,15 @@ public abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
if (e == null)
return false;
float val = e.getY();
List<T> yVals = getValues();
if (yVals == null) {
yVals = new ArrayList<T>();
List<T> values = getValues();
if (values == null) {
values = new ArrayList<T>();
}
if (yVals.size() == 0) {
mYMax = val;
mYMin = val;
} else {
if (mYMax < val)
mYMax = val;
if (mYMin > val)
mYMin = val;
}
calcMinMax(e);
// add the entry
yVals.add(e);
values.add(e);
return true;
}

View file

@ -145,18 +145,7 @@ public abstract class RealmBaseDataSet<T extends RealmObject, S extends Entry> e
for (S e : mValues) {
if (e != null && !Float.isNaN(e.getY())) {
if (e.getY() < mYMin)
mYMin = e.getY();
if (e.getY() > mYMax)
mYMax = e.getY();
if (e.getX() < mXMin)
mXMin = e.getX();
if (e.getX() > mXMax)
mXMax = e.getX();
calcMinMax(e);
}
}
@ -171,6 +160,26 @@ public abstract class RealmBaseDataSet<T extends RealmObject, S extends Entry> e
}
}
/**
* Updates the min and max x and y value of this DataSet based on the given Entry.
*
* @param e
*/
protected void calcMinMax(S e) {
if (e.getY() < mYMin)
mYMin = e.getY();
if (e.getY() > mYMax)
mYMax = e.getY();
if (e.getX() < mXMin)
mXMin = e.getX();
if (e.getX() > mXMax)
mXMax = e.getX();
}
@Override
public S getEntryForXPos(float xPos) {
//DynamicRealmObject o = new DynamicRealmObject(results.where().equalTo(mXValuesField, xIndex).findFirst());
@ -298,15 +307,7 @@ public abstract class RealmBaseDataSet<T extends RealmObject, S extends Entry> e
mValues = new ArrayList<S>();
}
if (mValues.size() == 0) {
mYMax = val;
mYMin = val;
} else {
if (mYMax < val)
mYMax = val;
if (mYMin > val)
mYMin = val;
}
calcMinMax(e);
// add the entry
mValues.add(e);

View file

@ -166,19 +166,10 @@ public interface IDataSet<T extends Entry> {
*/
boolean addEntry(T e);
/**
* Removes an Entry from the DataSets entries array. This will also
* recalculate the current minimum and maximum values of the DataSet and the
* value-sum. Returns true if an Entry was removed, false if no Entry could
* be removed.
*
* @param e
*/
boolean removeEntry(T e);
/**
* Adds an Entry to the DataSet dynamically.
* Entries are added to their appropriate index respective to it's x-index.
* Entries are added to their appropriate index in the values array respective to their x-position.
* This will also recalculate the current minimum and maximum
* values of the DataSet and the value-sum.
*
@ -203,12 +194,31 @@ public interface IDataSet<T extends Entry> {
boolean removeLast();
/**
* Removes the Entry object that has the given xIndex from the DataSet.
* Removes an Entry from the DataSets entries array. This will also
* recalculate the current minimum and maximum values of the DataSet and the
* value-sum. Returns true if an Entry was removed, false if no Entry could
* be removed.
*
* @param e
*/
boolean removeEntry(T e);
/**
* Removes the Entry object that has the given xPos from the DataSet.
* Returns true if an Entry was removed, false if no Entry could be removed.
*
* @param xIndex
* @param xPos
*/
boolean removeEntry(int xIndex);
boolean removeEntryByXPos(float xPos);
/**
* Removes the Entry object at the given index in the values array from the DataSet.
* Returns true if an Entry was removed, false if no Entry could be removed.
*
* @param index
* @return
*/
boolean removeEntry(int index);
/**
* Checks if this DataSet contains the specified Entry. Returns true if so,

View file

@ -16,6 +16,68 @@ import static junit.framework.Assert.assertEquals;
*/
public class DataSetTest {
@Test
public void testCalcMinMax() {
List<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry(10, 10));
entries.add(new Entry(15, 2));
entries.add(new Entry(21, 5));
ScatterDataSet set = new ScatterDataSet(entries, "");
assertEquals(10f, set.getXMin(), 0.01f);
assertEquals(21f, set.getXMax(), 0.01f);
assertEquals(2f, set.getYMin(), 0.01f);
assertEquals(10f, set.getYMax(), 0.01f);
assertEquals(3, set.getEntryCount());
set.addEntry(new Entry(25, 1));
assertEquals(10f, set.getXMin(), 0.01f);
assertEquals(25f, set.getXMax(), 0.01f);
assertEquals(1f, set.getYMin(), 0.01f);
assertEquals(10f, set.getYMax(), 0.01f);
assertEquals(4, set.getEntryCount());
}
@Test
public void testAddRemoveEntry() {
List<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry(10, 10));
entries.add(new Entry(15, 2));
entries.add(new Entry(21, 5));
ScatterDataSet set = new ScatterDataSet(entries, "");
assertEquals(3, set.getEntryCount());
set.addEntryOrdered(new Entry(5, 1));
assertEquals(4, set.getEntryCount());
assertEquals(5, set.getXMin(), 0.01f);
assertEquals(21, set.getXMax(), 0.01f);
assertEquals(1f, set.getYMin(), 0.01f);
assertEquals(10f, set.getYMax(), 0.01f);
assertEquals(5, set.getEntryForIndex(0).getX(), 0.01f);
assertEquals(1, set.getEntryForIndex(0).getY(), 0.01f);
set.addEntryOrdered(new Entry(20, 50));
assertEquals(5, set.getEntryCount());
assertEquals(20, set.getEntryForIndex(3).getX(), 0.01f);
assertEquals(50, set.getEntryForIndex(3).getY(), 0.01f);
}
@Test
public void testGetEntryForXPos() {