如何在MPAndroid Line Chart中突出显示所选的值

3

我已经使用setDrawValues(false)禁用了LineChart中的所有数值。现在我希望当用户触摸该点时,启用Line Chart中特定的值。
我尝试使用highLightVales(),但它对我没有起作用。

@Override
    public void onValueSelected(Entry entry, int i, Highlight highlight) { 
        //mchart.setHighlightEnabled(true);
        //mchart.highlightValue(1,1);
        Highlight[] highlights=mchart.getHighlighted();
        mchart.highlightValues(highlights);
    }

你成功了吗?我也需要这个。 - Heisenberg
3个回答

9
你需要将MarkerView添加到LineChart中。
首先创建一个CustomMarkerView类。
public class CustomMarkerView extends MarkerView {

private TextView tvContent;

public CustomMarkerView (Context context, int layoutResource) {
    super(context, layoutResource);
    // this markerview only displays a textview
    tvContent = (TextView) findViewById(R.id.tvContent);
}

// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
    tvContent.setText("" + e.getVal()); // set the entry-value as the display text
}

@Override
public int getXOffset(float xpos) {
    // this will center the marker-view horizontally
    return -(getWidth() / 2);
}

@Override
public int getYOffset(float ypos) {
    // this will cause the marker-view to be above the selected value
    return -getHeight();
} }

创建一个在.xml中表示您的标记的布局。
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:background="@drawable/markerImage" >

<TextView
    android:id="@+id/tvContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text=""
    android:textSize="12dp"
    android:textColor="@android:color/white"
    android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

最后,将其设置到图表中。
    lineChart.setDrawMarkerViews(true);
    CustomMarkerView customMarkerView = new CustomMarkerView(context, R.layout.custom_marker_view_layout);
    lineChart.setMarkerView(customMarkerView);

请确保图表启用了触摸功能。 lineChart.setTouchEnabled(true); 这样就可以得到所需的结果。 enter image description here

如何增加圆的大小? - Atif AbbAsi

1
我这样做
@Override public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
    mChart.highlightValue(e.getXIndex(), 0);
}

0
突出显示图表中选定的值
    @Override
        public void onValueSelected(Entry e, Highlight h) {
            fragmentHomeBinding.lineChart.highlightValue(h);
            Log.d("Highlight", "onValueSelected: " + h.getY());
        }

您可以在图表中设置标记并显示所选值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接