MarkerView

MakerView类能被任何用户创建的类扩展为了展示自定义的(弹窗)View无论何时要在图表中高亮一个值。

设置或者得到marker

  • setMarkerView(MarkerView mv): 为图表设置一个MarkerView为了展示被选择的值在哪里。
  • getMarkerView(): 返回在图表中被设置的MarkerView,没有就返回null

自定义实现

下面是一个看上去比较想MarkerView自定义实现的例子。比较重要的是你可以根据以下方法实现它:

  • refreshContent(Entry e, int dataSetIndex):每次这个方法被调用MarkerView被重新绘制,给你机会来更新它显示的内容(例如为TexView设置文本)
  • getXOffset():返回标记被绘制位置在x轴上的偏移。默认的,标记将被绘制在条目位置的左上边缘。
  • getYOffset(): 返回标记被绘制位置在y轴上的偏移。默认的,标记将被绘制在条目位置的左上边缘。
1. public class CustomMarkerView extends MarkerView {

3. private TextView tvContent;

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

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

18. @Override
19. public int getXOffset() {
20. // this will center the marker-view horizontally
21. return -(getWidth() / 2);
22. }

24. @Override
25. public int getYOffset() {
26. // this will cause the marker-view to be above the selected value
27. return -getHeight();
28. }
29. }

在设置了自定义的标记类之后,创建一个.xml的布局来呈现你的标记。在这个例子中布局包含一个带背景图片的相对布局还包含一个TextView。当然你可以创建任何你想到的布局在这里。

1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="wrap_content"
4. android:layout_height="40dp"
5. android:background="@drawable/markerImage" >

7. <TextView
8. android:id="@+id/tvContent"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_centerHorizontal="true"
12. android:layout_marginTop="7dp"
13. android:layout_marginLeft="5dp"
14. android:layout_marginRight="5dp"
15. android:text=""
16. android:textSize="12dp"
17. android:textColor="@android:color/white"
18. android:ellipsize="end"
19. android:singleLine="true"
20. android:textAppearance="?android:attr/textAppearanceSmall" />

22. </RelativeLayout>

最后,在你创建了你自己的MarkerView之后,在图表中设置它。在创建你的MarkerView的时候,确保你提供了布局资源在你创建的.xml中。

1. CustomMarkerView mv = new CustomMarkerView (Context, R.layout.custom_marker_view_layout);

3. // set the marker to the chart
4. chart.setMarkerView(mv);