Search code examples
javaandroidpie-chartmpandroidchart

MPAndroidChart - Show MarkerView on long click


I have CustomMarkerView with PieChart on MPAndroidChart, however I want to show the marker only when the chart is longClicked.

Is there any way I can achieve this result ?


Solution

  • Yes of course. I rebuilded this project from github. The first hint was from this post. You inherit your PieChart or every chart you want and apply a onTouchEvent on it. You create a class called ChartMarkerView and extends it's MarkerView. There you bind a view and set the duration of the ClickTime.

    Hope it will help you to approach your target!

    Update:

    In addition to the part above @AinulBedjo found out that you can reach the long click with @Override the draw method then only draw when the longClicked is true:

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