Search in sources :

Example 1 with SelInfo

use of com.github.mikephil.charting.utils.SelInfo 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);
}
Also used : Highlight(com.github.mikephil.charting.utils.Highlight) SelInfo(com.github.mikephil.charting.utils.SelInfo) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Example 2 with SelInfo

use of com.github.mikephil.charting.utils.SelInfo 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;
}
Also used : Highlight(com.github.mikephil.charting.utils.Highlight) OnChartGestureListener(com.github.mikephil.charting.interfaces.OnChartGestureListener) RadarChart(com.github.mikephil.charting.charts.RadarChart) SelInfo(com.github.mikephil.charting.utils.SelInfo)

Aggregations

Highlight (com.github.mikephil.charting.utils.Highlight)2 SelInfo (com.github.mikephil.charting.utils.SelInfo)2 SuppressLint (android.annotation.SuppressLint)1 Paint (android.graphics.Paint)1 RadarChart (com.github.mikephil.charting.charts.RadarChart)1 OnChartGestureListener (com.github.mikephil.charting.interfaces.OnChartGestureListener)1