Java Code Examples for com.github.mikephil.charting.utils.Utils#getClosestDataSetIndex()

The following examples show how to use com.github.mikephil.charting.utils.Utils#getClosestDataSetIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PieRadarChartTouchListener.java    From iMoney with Apache License 2.0 4 votes vote down vote up
@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);
        mLastHighlighted = null;

    } else {

        float angle = mChart.getAngleForPoint(e.getX(), e.getY());

        if (mChart instanceof PieChart) {
            angle /= mChart.getAnimator().getPhaseY();
        }

        int index = mChart.getIndexForAngle(angle);

        // check if the index could be found
        if (index < 0) {

            mChart.highlightValues(null);
            mLastHighlighted = null;

        } else {

            List<SelectionDetail> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);

            int dataSetIndex = 0;

            // get the dataset that is closest to the selection (PieChart
            // only
            // has one DataSet)
            if (mChart instanceof RadarChart) {

                dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, distance
                        / ((RadarChart) mChart).getFactor(), null);
            }

            if (dataSetIndex < 0) {
                mChart.highlightValues(null);
                mLastHighlighted = null;
            } else {
                Highlight h = new Highlight(index, dataSetIndex);

                if (h.equalTo(mLastHighlighted)) {

                    mChart.highlightTouch(null);
                    mLastHighlighted = null;
                } else {

                    mChart.highlightTouch(h);
                    mLastHighlighted = h;
                }
            }
        }
    }

    return true;
}
 
Example 2
Source File: PieRadarChartTouchListener.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onSingleTapUp(MotionEvent e) {

    mLastGesture = ChartGesture.SINGLE_TAP;

    OnChartGestureListener l = mChart.getOnChartGestureListener();

    if (l != null) {
        l.onChartSingleTapped(e);
    }

    if(!mChart.isHighlightPerTapEnabled()) {
        return false;
    }

    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

        if (mLastHighlighted == null)
            mChart.highlightValues(null); // no listener callback
        else
            mChart.highlightTouch(null); // listener callback

        mLastHighlighted = null;

    } else {

        float angle = mChart.getAngleForPoint(e.getX(), e.getY());

        if (mChart instanceof PieChart) {
            angle /= mChart.getAnimator().getPhaseY();
        }

        int index = mChart.getIndexForAngle(angle);

        // check if the index could be found
        if (index < 0) {

            mChart.highlightValues(null);
            mLastHighlighted = null;

        } else {

            List<SelectionDetail> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);

            int dataSetIndex = 0;

            // get the dataset that is closest to the selection (PieChart
            // only
            // has one DataSet)
            if (mChart instanceof RadarChart) {

                dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, distance
                        / ((RadarChart) mChart).getFactor(), null);
            }

            if (dataSetIndex < 0) {
                mChart.highlightValues(null);
                mLastHighlighted = null;
            } else {
                Highlight h = new Highlight(index, dataSetIndex);
                performHighlight(h, e);
            }
        }
    }

    return true;
}
 
Example 3
Source File: BarLineChartBase.java    From Notification-Analyser with MIT License 4 votes vote down vote up
/**
 * 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 || mCurrentData == 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;

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

    double xTouchVal = pts[0];
    double yTouchVal = pts[1];
    double base = Math.floor(xTouchVal);

    double touchOffset = mDeltaX * 0.025;
    // Log.i(LOG_TAG, "touchindex x: " + xTouchVal + ", touchindex y: " +
    // yTouchVal + ", offset: "
    // + touchOffset);
    // Toast.makeText(getContext(), "touchindex x: " + xTouchVal +
    // ", touchindex y: " + yTouchVal + ", offset: " + touchOffset,
    // Toast.LENGTH_SHORT).show();

    // 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;

    int dataSetIndex = 0; // index of the DataSet inside the ChartData
                          // object

    // 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;

    // Toast.makeText(getContext(), "xindex: " + xIndex + ", dataSetIndex: "
    // + dataSetIndex,
    // Toast.LENGTH_SHORT).show();

    return new Highlight(xIndex, dataSetIndex);
}
 
Example 4
Source File: PieRadarChartTouchListener.java    From Notification-Analyser with MIT License 4 votes vote down vote up
@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;

            // get the dataset that is closest to the selection (PieChart
            // only
            // 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;
}
 
Example 5
Source File: ChartHighlighter.java    From iMoney with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the corresponding dataset-index for a given xIndex and xy-touch position in pixels.
 * 
 * @param xIndex
 * @param x
 * @param y
 * @return
 */
protected int getDataSetIndex(int xIndex, float x, float y) {

	List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xIndex);

	float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
	float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);

	YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;

	int dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, y, axis);

	return dataSetIndex;
}
 
Example 6
Source File: ChartHighlighter.java    From Stayfit with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the corresponding dataset-index for a given xIndex and xy-touch position in pixels.
 * 
 * @param xIndex
 * @param x
 * @param y
 * @return
 */
protected int getDataSetIndex(int xIndex, float x, float y) {

	List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xIndex);

	float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
	float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);

	YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;

	int dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, y, axis);

	return dataSetIndex;
}