2015-07-18 56 views
2

我已經使用setDrawValues(false)禁用了LineChart中的所有值。現在,我希望在用戶觸及該點時啓用折線圖中的特定值。 我試過用highLightVales(),但它對我不起作用。如何在MPAndroid折線圖中高亮選中的值

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

它適合你嗎?我也需要這個。 – Heisenberg

回答

1

我不喜歡這樣

@Override public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { 
    mChart.highlightValue(e.getXIndex(), 0); 
} 
5

您需要添加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(); 
} } 

在.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