MPAndroidChart - 长按显示 MarkerView

MPAndroidChart - Show MarkerView on long click

我在 MPAndroidChart 上有 CustomMarkerViewPieChart,但是我只想在长按图表时显示标记。

有什么方法可以实现这个结果吗?

当然可以。我从 github. The first hint was from this post 重建了这个项目。您继承您的 PieChart 或您想要的每个图表并在其上应用 onTouchEvent。您创建一个名为 ChartMarkerView 的 class 并扩展它的 MarkerView。在那里你绑定一个视图并设置 ClickTime.

的持续时间

希望它能帮助你接近你的目标!

更新:

除了上面的部分@AinulBedjo 发现您可以使用 @Overridedraw 方法实现长按,然后只有 drawlongClickedtrue:

public class CustomPieChart extends PieChart {
    public CustomPieChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        public void onLongPress(MotionEvent e) {
            CustomMarker markerView = (CustomMarker) getMarker();
            isLongClicked = true;

            //get angle and index of dataset
            float angle = getAngleForPoint(e.getX(), e.getY());
            int indexForAngle = getIndexForAngle(angle);

            // takes index of angle, Y-Position and dataSetIndex of 0
            Highlight highlight = new Highlight(indexForAngle, e.getY(), 0);
            highlightValue(highlight);

            // redraw
            invalidate();
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            isLongClicked = false;
            return super.onSingleTapUp(e);
        }
    });

    Boolean isLongClicked = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = true;
        // if there is no marker view or drawing marker is disabled
        if (isShowingMarker() && this.getMarker() instanceof CustomMarker){
            gestureDetector.onTouchEvent(event);
        }
        handled = super.onTouchEvent(event);
        return handled;
    }

    // draw markers on highlighter values
    @Override
    protected void drawMarkers(Canvas canvas) {
        // if there is no marker view or drawing marker is disabled
        if (mMarker == null || !isDrawMarkersEnabled() || !valuesToHighlight())
            return;

        for (Highlight highlight : mIndicesToHighlight) {

            IDataSet set = mData.getDataSetByIndex(highlight.getDataSetIndex());

            Entry e = mData.getEntryForHighlight(highlight);
            int entryIndex = set.getEntryIndex(e);

            // make sure entry not null
            if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX())
                continue;

            float[] pos = getMarkerPosition(highlight);

            // check bounds
            if (!mViewPortHandler.isInBounds(pos[0], pos[1]))
                continue;

            // callbacks to update the content
            mMarker.refreshContent(e, highlight);

            // draw the marker
            if (isLongClicked) {
                mMarker.draw(canvas, pos[0], pos[1]);
            }
        }
    }

    private boolean isShowingMarker(){
        return mMarker != null && isDrawMarkersEnabled();
    }
}