Search in sources :

Example 1 with Highlight

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);
}
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 Highlight

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

Example 3 with Highlight

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;
}
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)

Example 4 with Highlight

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

Example 5 with Highlight

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);
            }
        }
    }
}
Also used : Path(android.graphics.Path) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) Highlight(com.github.mikephil.charting.utils.Highlight) BarDataSet(com.github.mikephil.charting.data.BarDataSet) Paint(android.graphics.Paint)

Aggregations

Highlight (com.github.mikephil.charting.utils.Highlight)5 Paint (android.graphics.Paint)3 OnChartGestureListener (com.github.mikephil.charting.interfaces.OnChartGestureListener)2 SelInfo (com.github.mikephil.charting.utils.SelInfo)2 SuppressLint (android.annotation.SuppressLint)1 Path (android.graphics.Path)1 RadarChart (com.github.mikephil.charting.charts.RadarChart)1 BarDataSet (com.github.mikephil.charting.data.BarDataSet)1 BarEntry (com.github.mikephil.charting.data.BarEntry)1 Entry (com.github.mikephil.charting.data.Entry)1