【问题标题】:How to remove or hide MarkerView in MPAndroidChart?如何在 MPAndroidChart 中删除或隐藏 MarkerView?
【发布时间】:2017-09-30 00:41:39
【问题描述】:

我用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;
}

}

【问题讨论】:

  • 你找到这个问题的答案了吗?

标签: android bar-chart mpandroidchart


【解决方案1】:

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

mChart.highlightValue(null);

为我工作

【讨论】:

  • 有没有办法从单个数据集中删除标记,而另一个数据集仍然可以拥有它们?
  • @mtsahakis,不确切知道该库是否可能,但您可以做的是跟踪突出显示的元素以及何时需要从特定数据集中删除突出显示 - 只是调用 mChart.highlightValue(null);然后重新突出显示所有需要的值。
  • @mtsahakis 试试这个 dataSet.setDrawHighlightIndicators(false)
  • @mtsahakis dataSet.setHighlightEnabled(false) 禁用对特定数据集而不是所有图表的交互
【解决方案2】:

为您的图表设置一个 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;
        }
    });

【讨论】:

  • 另外,如果你需要隐藏这个标记,例如在滚动变化时,最好使用chart.valuesToHighlight()检查标记可见性
猜你喜欢
  • 1970-01-01
  • 2016-08-25
  • 2021-01-18
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
相关资源
最近更新 更多