Skip to content

Commit

Permalink
Merge pull request PhilJay#843 from danielgindi/minor-improvements
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
PhilJay committed Jul 9, 2015
2 parents 44e12a4 + 3e08a13 commit 3419dff
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ protected void calcMinMax() {
}

@Override
protected void calculateOffsets() {
public void calculateOffsets() {

if (!mCustomViewPortEnabled) {

Expand Down Expand Up @@ -598,6 +598,12 @@ public void computeScroll() {
public void zoomIn() {
Matrix save = mViewPortHandler.zoomIn(getWidth() / 2f, -(getHeight() / 2f));
mViewPortHandler.refresh(save, this, true);

// Range might have changed, which means that Y-axis labels
// could have changed in size, affecting Y-axis size.
// So we need to recalculate offsets.
calculateOffsets();
postInvalidate();
}

/**
Expand All @@ -606,6 +612,12 @@ public void zoomIn() {
public void zoomOut() {
Matrix save = mViewPortHandler.zoomOut(getWidth() / 2f, -(getHeight() / 2f));
mViewPortHandler.refresh(save, this, true);

// Range might have changed, which means that Y-axis labels
// could have changed in size, affecting Y-axis size.
// So we need to recalculate offsets.
calculateOffsets();
postInvalidate();
}

/**
Expand All @@ -620,6 +632,12 @@ public void zoomOut() {
public void zoom(float scaleX, float scaleY, float x, float y) {
Matrix save = mViewPortHandler.zoom(scaleX, scaleY, x, -y);
mViewPortHandler.refresh(save, this, true);

// Range might have changed, which means that Y-axis labels
// could have changed in size, affecting Y-axis size.
// So we need to recalculate offsets.
calculateOffsets();
postInvalidate();
}

/**
Expand All @@ -629,6 +647,12 @@ public void zoom(float scaleX, float scaleY, float x, float y) {
public void fitScreen() {
Matrix save = mViewPortHandler.fitScreen();
mViewPortHandler.refresh(save, this, true);

// Range might have changed, which means that Y-axis labels
// could have changed in size, affecting Y-axis size.
// So we need to recalculate offsets.
calculateOffsets();
postInvalidate();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void init() {
}

@Override
protected void calculateOffsets() {
public void calculateOffsets() {

float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected void onDraw(Canvas canvas) {
}

@Override
protected void calculateOffsets() {
public void calculateOffsets() {
super.calculateOffsets();

// prevent nullpointer when no data set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void notifyDataSetChanged() {
}

@Override
protected void calculateOffsets() {
public void calculateOffsets() {

float legendLeft = 0f, legendRight = 0f, legendBottom = 0f, legendTop = 0f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void init() {
}

// @Override
// protected void calculateOffsets() {
// public void calculateOffsets() {
// super.calculateOffsets();
//
// float offset = mData.getGreatestShapeSize() / 2f;
Expand Down
44 changes: 29 additions & 15 deletions MPChartLib/src/com/github/mikephil/charting/data/ChartData.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,29 +636,37 @@ public boolean removeDataSet(int index) {
}

/**
* Adds an Entry to the DataSet at the specified index. Entries are added to
* the end of the list.
* Adds an Entry to the DataSet at the specified index.
* Entries are added to the end of the list.
*
* @param entry
* @param e
* @param dataSetIndex
*/
public void addEntry(Entry e, int dataSetIndex) {

if (mDataSets.size() > dataSetIndex && dataSetIndex >= 0) {

float val = e.getVal();
T set = mDataSets.get(dataSetIndex);

mYValCount += 1;
mYValueSum += val;

if (mYMax < val)
mYMax = val;
if (mYMin > val)
if (mYValCount == 0) {
mYMin = val;
mYMax = val;

T set = mDataSets.get(dataSetIndex);
if (set.getAxisDependency() == AxisDependency.LEFT) {

mLeftAxisMax = e.getVal();
mLeftAxisMin = e.getVal();
} else {
mRightAxisMax = e.getVal();
mRightAxisMin = e.getVal();
}
} else {

if (set != null) {
if (mYMax < val)
mYMax = val;
if (mYMin > val)
mYMin = val;

if (set.getAxisDependency() == AxisDependency.LEFT) {

Expand All @@ -672,12 +680,15 @@ public void addEntry(Entry e, int dataSetIndex) {
if (mRightAxisMin > e.getVal())
mRightAxisMin = e.getVal();
}
}

mYValCount += 1;
mYValueSum += val;

handleEmptyAxis(getFirstLeft(), getFirstRight());
handleEmptyAxis(getFirstLeft(), getFirstRight());

// add the entry to the dataset
set.addEntry(e);
}
// add the entry to the dataset
set.addEntry(e);
} else {
Log.e("addEntry", "Cannot add Entry because dataSetIndex too high or too low.");
}
Expand Down Expand Up @@ -728,6 +739,9 @@ public boolean removeEntry(int xIndex, int dataSetIndex) {
T dataSet = mDataSets.get(dataSetIndex);
Entry e = dataSet.getEntryForXIndex(xIndex);

if (e == null || e.getXIndex() != xIndex)
return false;

return removeEntry(e, dataSetIndex);
}

Expand Down
82 changes: 73 additions & 9 deletions MPChartLib/src/com/github/mikephil/charting/data/DataSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,37 @@ public float getYValForXIndex(int xIndex) {

/**
* Returns the first Entry object found at the given xIndex with binary
* search. If the no Entry at the specifed x-index is found, this method
* returns the Entry at the closest x-index. Returns null if no Entry object
* search. If the no Entry at the specified x-index is found, this method
* returns the index at the closest x-index. Returns null if no Entry object
* at that index. INFORMATION: This method does calculations at runtime. Do
* not over-use in performance critical situations.
*
* @param xIndex
* @param x
* @return
*/
public T getEntryForXIndex(int x) {

int index = getEntryIndex(x);
if (index > -1)
return mYVals.get(index);
return null;
}

/**
* Returns the first Entry index found at the given xIndex with binary
* search. If the no Entry at the specified x-index is found, this method
* returns the index at the closest x-index. Returns -1 if no Entry object
* at that index. INFORMATION: This method does calculations at runtime. Do
* not over-use in performance critical situations.
*
* @param x
* @return
*/
public int getEntryIndex(int x) {

int low = 0;
int high = mYVals.size() - 1;
T closest = null;
int closest = -1;

while (low <= high) {
int m = (high + low) / 2;
Expand All @@ -210,15 +228,15 @@ public T getEntryForXIndex(int x) {
while (m > 0 && mYVals.get(m - 1).getXIndex() == x)
m--;

return mYVals.get(m);
return m;
}

if (x > mYVals.get(m).getXIndex())
low = m + 1;
else
high = m - 1;

closest = mYVals.get(m);
closest = m;
}

return closest;
Expand Down Expand Up @@ -442,10 +460,12 @@ public boolean isDrawValuesEnabled() {
}

/**
* Adds an Entry to the DataSet dynamically. This will also recalculate the
* current minimum and maximum values of the DataSet and the value-sum.
* Adds an Entry to the DataSet dynamically.
* Entries are added to the end of the list.
* This will also recalculate the current minimum and maximum
* values of the DataSet and the value-sum.
*
* @param d
* @param e
*/
@SuppressWarnings("unchecked")
public void addEntry(Entry e) {
Expand Down Expand Up @@ -475,6 +495,50 @@ public void addEntry(Entry e) {
mYVals.add((T) e);
}

/**
* Adds an Entry to the DataSet dynamically.
* Entries are added to their appropriate index respective to it's x-index.
* This will also recalculate the current minimum and maximum
* values of the DataSet and the value-sum.
*
* @param e
*/
@SuppressWarnings("unchecked")
public void addEntryOrdered(Entry e) {

if (e == null)
return;

float val = e.getVal();

if (mYVals == null) {
mYVals = new ArrayList<T>();
}

if (mYVals.size() == 0) {
mYMax = val;
mYMin = val;
} else {
if (mYMax < val)
mYMax = val;
if (mYMin > val)
mYMin = val;
}

mYValueSum += val;

if (mYVals.size() > 0 && mYVals.get(mYVals.size() - 1).getXIndex() > e.getXIndex())
{
int closestIndex = getEntryIndex(e.getXIndex());
if (mYVals.get(closestIndex).getXIndex() < e.getXIndex())
closestIndex++;
mYVals.add(closestIndex, (T) e);
return;
}

mYVals.add((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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ public boolean onTouch(View v, MotionEvent event) {
}
}

if (mTouchMode == X_ZOOM ||
mTouchMode == Y_ZOOM ||
mTouchMode == PINCH_ZOOM ||
mTouchMode == POST_ZOOM) {

// Range might have changed, which means that Y-axis labels
// could have changed in size, affecting Y-axis size.
// So we need to recalculate offsets.
mChart.calculateOffsets();
mChart.postInvalidate();
}

mTouchMode = NONE;
mChart.enableScroll();

Expand Down Expand Up @@ -571,7 +583,14 @@ public void computeScroll() {

if (Math.abs(mDecelerationVelocity.x) >= 0.01 || Math.abs(mDecelerationVelocity.y) >= 0.01)
Utils.postInvalidateOnAnimation(mChart); // This causes computeScroll to fire, recommended for this by Google
else
else {
// Range might have changed, which means that Y-axis labels
// could have changed in size, affecting Y-axis size.
// So we need to recalculate offsets.
mChart.calculateOffsets();
mChart.postInvalidate();

stopDeceleration();
}
}
}
Loading

0 comments on commit 3419dff

Please sign in to comment.