1.创建MarkerView继承类
public class ChartMarkerView extends MarkerView { private TextView data; private String dataName; public ChartMarkerView(Context context, String dataName) { super(context, R.layout.chart_marker); data = findViewById(R.id.marker_data); this.dataName = dataName; } @Override //进行重绘 public void refreshContent(Entry e, Highlight highlight) { try { data.setText(dataName + ":" + (int) e.getY()); } catch (Exception exception) { exception.printStackTrace(); } super.refreshContent(e, highlight); } @Override public MPPointF getOffset() { return new MPPointF(-(getWidth() / 2), -getHeight()); } }
2.markerView布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="60dp" android:layout_height="30dp" android:background="@drawable/chart_marker" android:orientation="vertical" tools:ignore="MissingDefaultResource"> <TextView android:id="@+id/marker_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="" android:textColor="#9ed2f9" android:textSize="13sp" /> </LinearLayout>
3.markerview背景文件chart_marker.png
4.设置到chart
MarkerView rateMarker = new ChartMarkerView(this, "数据");
lineChart.setMarker(rateMarker);
rateMarker.setChartView(lineChart);