如何在MPAndroidChart中删除或隐藏MarkerView?

时间:2017-05-02 13:09:07

标签: android bar-chart mpandroidchart

我用MPAndroidChart做了一个条形图。我做了一个自定义标记视图类来查看条形图的值 几秒后我会删除或隐藏视图。它应该像吐司一样起作用。但我做不到。
这首先是一个问题,因为在我第一次触摸图表后,它变得可见,只有当我找到合适的拍摄条件时它才会消失。

有人知道如何解决这个问题吗?

在我的代码中,我有:

mChart.invalidate();
mv = new CustomMarkerView(getContext(), R.layout.alert_chart, position, jsonResponse);
mChart.setMarker(mv);

我的CustomMarkerView是

public class CustomMarkerViewextends MarkerView {

public TextView correct;
public TextView wrong ;
public int position;
public JSONArray jsonArray;

public CustomMarkerView(Context context, int layoutResource, int pos, JSONArray json) {
    super(context, layoutResource);

    correct = (TextView) findViewById(R.id.correct);
    wrong = (TextView) findViewById(R.id.wrong);

    position = pos;
    jsonArray = json;

}

@Override
public void refreshContent(Entry e, Highlight highlight) {
    switch (position){
        case 1:
            try {
                correct.setText(getContext().getString(R.string.correct_alert) + "   " + jsonArray.getJSONObject((int)(e.getX())).getString("TP"));
                wrong.setText(getContext().getString(R.string.wrong_alert) + "   " + jsonArray.getJSONObject((int)(e.getX())).getString("FP"));        
            } catch (JSONException e1) {
                e1.printStackTrace();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            break;
        case 2:
            try {
                correct.setText(getContext().getString(R.string.tpr_alert) + "   " + Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("TPR"))*100.0)/100.0);
                wrong.setText(getContext().getString(R.string.msr_alert) + "   " + Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("MSR"))*100.0)/100.0);
            } catch (JSONException e1) {
                e1.printStackTrace();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            break;
        case 3:
            try {
                correct.setText(getContext().getString(R.string.nmean_alert) + "   " +Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("n_mean"))*100.0)/100.0);
                wrong.setText(getContext().getString(R.string.nmax_alert) + "   " +Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("n_max"))*100.0)/100.0);
            } catch (JSONException e1) {
                e1.printStackTrace();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            break;
    }

    // this will perform necessary layouting
    super.refreshContent(e, highlight);

}

private MPPointF mOffset;

@Override
public MPPointF getOffset() {

    if(mOffset == null) {
        // center the marker horizontally and vertically
        mOffset = new MPPointF(-(getWidth()/2), -getHeight());
    }

    return mOffset;
}

}

2 个答案:

答案 0 :(得分:2)

我知道,已经有点晚了,但是:

mChart.highlightValue(null);

为我工作

答案 1 :(得分:0)

为图表设置onTouchListener,然后在MotionEvent ACTION_UP中将突出显示值设置为null。代码:

chart.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_UP: {
                    chart.highlightValue(null);
                    break;
                }
            }
            return false;
        }
    });