如何在MPAndroid折线图中高亮显示所选值

A P*_*P R 3 mpandroidchart

我已经禁用了LineChart中的所有值setDrawValues(false).现在,我希望在用户触摸该点时启用折线图中的特定值.我尝试使用它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);
    }
Run Code Online (Sandbox Code Playgroud)

Mad*_*dhu 7

你需要添加MarkerViewLineChart

首先创建一个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();
} }
Run Code Online (Sandbox Code Playgroud)

在.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>
Run Code Online (Sandbox Code Playgroud)

最后,将其设置为图表

    lineChart.setDrawMarkerViews(true);
    CustomMarkerView customMarkerView = new CustomMarkerView(context, R.layout.custom_marker_view_layout);
    lineChart.setMarkerView(customMarkerView);
Run Code Online (Sandbox Code Playgroud)

并确保在图表上启用了触控功能.

lineChart.setTouchEnabled(true);

你会得到理想的结果.

在此输入图像描述