use of com.github.mikephil.charting.utils.Highlight in project carat by amplab.
the class BarLineChartBase method getHighlightByTouchPoint.
/**
* 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
* @return
*/
public Highlight getHighlightByTouchPoint(float x, float y) {
if (mDataNotSet || mData == null) {
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;
mTrans.pixelsToValue(pts);
double xTouchVal = pts[0];
double yTouchVal = pts[1];
double base = Math.floor(xTouchVal);
double touchOffset = mDeltaX * 0.025;
// touch out of chart
if (xTouchVal < -touchOffset || xTouchVal > mDeltaX + touchOffset)
return null;
if (this instanceof CandleStickChart)
base -= 0.5;
if (base < 0)
base = 0;
if (base >= mDeltaX)
base = mDeltaX - 1;
int xIndex = (int) base;
// index of the DataSet inside the ChartData
int dataSetIndex = 0;
// 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);
dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
if (dataSetIndex == -1)
return null;
return new Highlight(xIndex, dataSetIndex);
}
use of com.github.mikephil.charting.utils.Highlight in project carat by amplab.
the class BarLineChartTouchListener method onSingleTapUp.
@Override
public boolean onSingleTapUp(MotionEvent e) {
OnChartGestureListener l = mChart.getOnChartGestureListener();
if (l != null) {
l.onChartSingleTapped(e);
}
Highlight h = mChart.getHighlightByTouchPoint(e.getX(), e.getY());
if (h == null || h.equalTo(mLastHighlighted)) {
mChart.highlightTouch(null);
mLastHighlighted = null;
} else {
mLastHighlighted = h;
mChart.highlightTouch(h);
}
return super.onSingleTapUp(e);
}
use of com.github.mikephil.charting.utils.Highlight in project carat by amplab.
the class PieRadarChartTouchListener method onSingleTapUp.
@Override
public boolean onSingleTapUp(MotionEvent e) {
OnChartGestureListener l = mChart.getOnChartGestureListener();
if (l != null) {
l.onChartSingleTapped(e);
}
float distance = mChart.distanceToCenter(e.getX(), e.getY());
// check if a slice was touched
if (distance > mChart.getRadius()) {
// if no slice was touched, highlight nothing
mChart.highlightValues(null);
mLastHighlight = null;
} else {
float angle = mChart.getAngleForPoint(e.getX(), e.getY());
int index = mChart.getIndexForAngle(angle);
// check if the index could be found
if (index < 0) {
mChart.highlightValues(null);
mLastHighlight = null;
} else {
ArrayList<SelInfo> valsAtIndex = mChart.getYValsAtIndex(index);
int dataSetIndex = 0;
// has one DataSet)
if (mChart instanceof RadarChart) {
dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, distance / ((RadarChart) mChart).getFactor());
}
Highlight h = new Highlight(index, dataSetIndex);
if (h.equalTo(mLastHighlight)) {
mChart.highlightTouch(null);
mLastHighlight = null;
} else {
mChart.highlightTouch(h);
mLastHighlight = h;
}
}
}
return true;
}
use of com.github.mikephil.charting.utils.Highlight in project carat by amplab.
the class BarChart method getHighlightByTouchPoint.
/**
* 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 || mData == null) {
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;
mTrans.pixelsToValue(pts);
// for barchart, we only need x-val
double xTouchVal = pts[0];
double base = xTouchVal;
if (xTouchVal < 0 || xTouchVal > mDeltaX)
return null;
if (base < 0)
base = 0;
if (base >= mDeltaX)
base = mDeltaX - 1;
int setCount = mData.getDataSetCount();
int valCount = setCount * mData.getXValCount();
// calculate the amount of bar-space between index 0 and touch position
float space = (float) (((float) valCount / (float) setCount) / (mDeltaX / base));
float reduction = (float) space * mData.getGroupSpace();
int xIndex = (int) ((base - reduction) / setCount);
int dataSetIndex = ((int) (base - reduction)) % setCount;
if (dataSetIndex == -1)
return null;
return new Highlight(xIndex, dataSetIndex);
}
use of com.github.mikephil.charting.utils.Highlight in project carat by amplab.
the class BarChart method drawHighlights.
@Override
protected void drawHighlights() {
int setCount = mData.getDataSetCount();
for (int i = 0; i < mIndicesToHightlight.length; i++) {
Highlight h = mIndicesToHightlight[i];
int index = h.getXIndex();
int dataSetIndex = h.getDataSetIndex();
BarDataSet set = (BarDataSet) mData.getDataSetByIndex(dataSetIndex);
if (set == null)
continue;
mHighlightPaint.setColor(set.getHighLightColor());
mHighlightPaint.setAlpha(set.getHighLightAlpha());
// check outofbounds
if (index < mData.getYValCount() && index >= 0 && index < (mDeltaX * mPhaseX) / mData.getDataSetCount()) {
Entry e = getEntryByDataSetIndex(index, dataSetIndex);
if (e == null)
continue;
// calculate the correct x-position
float x = index * setCount + dataSetIndex + mData.getGroupSpace() / 2f + mData.getGroupSpace() * index;
float y = e.getVal();
prepareBar(x, y, set.getBarSpace());
mDrawCanvas.drawRect(mBarRect, mHighlightPaint);
if (mDrawHighlightArrow) {
mHighlightPaint.setAlpha(255);
// distance between highlight arrow and bar
float offsetY = mDeltaY * 0.07f;
Path arrow = new Path();
arrow.moveTo(x + 0.5f, y + offsetY * 0.3f);
arrow.lineTo(x + 0.2f, y + offsetY);
arrow.lineTo(x + 0.8f, y + offsetY);
mTrans.pathValueToPixel(arrow);
mDrawCanvas.drawPath(arrow, mHighlightPaint);
}
}
}
}
Aggregations