Refactored highlighting detection.
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
index 3079a88..0374dd9 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
@@ -3,10 +3,12 @@
import android.content.Context;
import android.graphics.Color;
+import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
+import android.util.Log;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
@@ -14,6 +16,7 @@
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.utils.Highlight;
+import com.github.mikephil.charting.utils.SelInfo;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
@@ -315,6 +318,7 @@
mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
}
+ // draw the stack
for (int k = 0; k < vals.length; k++) {
all -= vals[k];
@@ -611,6 +615,64 @@
}
/**
+ * Returns the Highlight object (contains x-index and DataSet index) of the
+ * selected value at the given touch point inside the BarChart.
+ *
+ * @param x
+ * @param y
+ * @return
+ */
+ @Override
+ public Highlight getHighlightByTouchPoint(float x, float y) {
+
+ if (mDataNotSet) {
+ Log.e(LOG_TAG, "Can't select by touch. No data set.");
+ return null;
+ }
+
+ // create an array of the touch-point
+ float[] pts = new float[2];
+ pts[0] = x;
+ pts[1] = y;
+
+ Matrix tmp = new Matrix();
+
+ // invert all matrixes to convert back to the original value
+ mMatrixOffset.invert(tmp);
+ tmp.mapPoints(pts);
+
+ mMatrixTouch.invert(tmp);
+ tmp.mapPoints(pts);
+
+ mMatrixValueToPx.invert(tmp);
+ tmp.mapPoints(pts);
+
+ // for barchart, we only need x-val
+ double xTouchVal = pts[0];
+ double base = Math.floor(xTouchVal);
+
+ if (xTouchVal < 0 || xTouchVal > mDeltaX)
+ return null;
+
+ if (base < 0)
+ base = 0;
+ if (base >= mDeltaX)
+ base = mDeltaX - 1;
+
+ int xIndex = (int) base;
+ // reduce x-index depending on DataSet count (because bars are next
+ // to each other)
+ xIndex /= mOriginalData.getDataSetCount();
+
+ int dataSetIndex = ((int) base) % mOriginalData.getDataSetCount();
+
+ if (dataSetIndex == -1)
+ return null;
+
+ return new Highlight(xIndex, dataSetIndex);
+ }
+
+ /**
* sets the skew (default 0.3f), the skew indicates how much the 3D effect
* of the chart is turned to the right
*
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java
index 7b7d25d..1ab9272 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java
@@ -1,7 +1,6 @@
package com.github.mikephil.charting.charts;
-import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -11,13 +10,10 @@
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
-import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewParent;
-import android.view.View.OnTouchListener;
-import android.widget.Toast;
import com.github.mikephil.charting.data.BarLineScatterCandleData;
import com.github.mikephil.charting.data.ChartData;
@@ -1568,8 +1564,9 @@
}
/**
- * returns the Highlight object (contains x-index and DataSet index) of the
- * selected value at the given touch point.
+ * Returns the Highlight object (contains x-index and DataSet index) of the
+ * selected value at the given touch point inside the Line-, Scatter-, or
+ * CandleStick-Chart.
*
* @param x
* @param y
@@ -1577,6 +1574,11 @@
*/
public Highlight getHighlightByTouchPoint(float x, float y) {
+ if (mDataNotSet) {
+ Log.e(LOG_TAG, "Can't select by touch. No data set.");
+ return null;
+ }
+
// create an array of the touch-point
float[] pts = new float[2];
pts[0] = x;
@@ -1607,11 +1609,10 @@
// Toast.LENGTH_SHORT).show();
// touch out of chart
- if ((this instanceof LineChart || this instanceof ScatterChart)
- && (xTouchVal < -touchOffset || xTouchVal > mDeltaX + touchOffset))
+ if (xTouchVal < -touchOffset || xTouchVal > mDeltaX + touchOffset)
return null;
- if (this instanceof BarChart && (xTouchVal < 0 || xTouchVal > mDeltaX))
- return null;
+
+ if(this instanceof CandleStickChart) base -= 0.5;
if (base < 0)
base = 0;
@@ -1620,49 +1621,31 @@
int xIndex = (int) base;
- if (this instanceof BarChart) {
-
- // reduce x-index depending on DataSet count (because bars are next
- // to each other)
- xIndex /= mOriginalData.getDataSetCount();
- }
-
int dataSetIndex = 0; // index of the DataSet inside the ChartData
// object
- if (this instanceof LineChart || this instanceof ScatterChart) {
-
- // check if we are more than half of a x-value or not
- if (xTouchVal - base > 0.5) {
- xIndex = (int) base + 1;
- }
- }
-
- if (mDataNotSet) {
- Log.e(LOG_TAG, "Can't select by touch. No data set.");
- return null;
+ // check if we are more than half of a x-value or not
+ if (xTouchVal - base > 0.5) {
+ xIndex = (int) base + 1;
}
ArrayList<SelInfo> valsAtIndex = getYValsAtIndex(xIndex);
- if (this instanceof BarChart) {
-
- dataSetIndex = ((int) base) % mOriginalData.getDataSetCount();
- } else {
- dataSetIndex = getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
- }
+ dataSetIndex = getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
if (dataSetIndex == -1)
return null;
-// Toast.makeText(getContext(), "xindex: " + xIndex + ", dataSetIndex: " + dataSetIndex,
-// Toast.LENGTH_SHORT).show();
+ // Toast.makeText(getContext(), "xindex: " + xIndex + ", dataSetIndex: "
+ // + dataSetIndex,
+ // Toast.LENGTH_SHORT).show();
return new Highlight(xIndex, dataSetIndex);
}
/**
- * returns the index of the DataSet that contains the closest value
+ * Returns the index of the DataSet that contains the closest value on the
+ * y-axis.
*
* @param valsAtIndex all the values at a specific index
* @return
@@ -1681,7 +1664,7 @@
}
}
- Log.i(LOG_TAG, "Closest DataSet index: " + index);
+ // Log.i(LOG_TAG, "Closest DataSet index: " + index);
return index;
}
diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
index ebc7e2c..2236da3 100644
--- a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
+++ b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
@@ -17,6 +17,12 @@
import com.github.mikephil.charting.utils.Highlight;
import com.github.mikephil.charting.utils.PointD;
+/**
+ * TouchListener for Bar-, Line-, Scatter- and CandleStickChart with handles all
+ * touch interaction. Longpress == Zoom out. Double-Tap == Zoom in.
+ *
+ * @author Philipp Jahoda
+ */
public class BarLineChartTouchListener extends SimpleOnGestureListener implements OnTouchListener {
private Matrix mMatrix = new Matrix();
@@ -413,7 +419,7 @@
@Override
public boolean onSingleTapUp(MotionEvent e) {
-
+
Highlight h = mChart.getHighlightByTouchPoint(e.getX(), e.getY());
if (h == null || h.equalTo(mLastHighlighted)) {
@@ -423,7 +429,7 @@
mLastHighlighted = h;
mChart.highlightTouch(h);
}
-
+
return true;
}