Rod*_*uin 11 java android linechart mpandroidchart
我一直在寻找一种方法来使MPAndroidChart只在点击时显示数据点的值(标签).但似乎即使在文档中我也无法在线找到它.
我使用的line chart和我想要的只是在点击时只显示某个点的标签.
rgu*_*rra 14
1- 在图表中启用触摸
chart.setTouchEnabled(true);
Run Code Online (Sandbox Code Playgroud)
2 - 创建MarkerView
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() {
// this will center the marker-view horizontally
return -(getWidth() / 2);
}
@Override
public int getYOffset() {
// this will cause the marker-view to be above the selected value
return -getHeight();
}
}
Run Code Online (Sandbox Code Playgroud)
3 - 创建tvContent视图
<?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:layout_marginTop="7dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
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)
4.在图表中设置视图标记
CustomMarkerView mv = new CustomMarkerView (Context, R.layout.custom_marker_view_layout);
chart.setMarkerView(mv);
Run Code Online (Sandbox Code Playgroud)
https://github.com/PhilJay/MPAndroidChart/wiki/MarkerView
使用IMarker界面(从3.0.0版开始不推荐使用MarkerView)
1.创建一个实现IMarker接口的新类
public class YourMarkerView extends MarkerView {
private TextView tvContent;
public MyMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
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.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}}
Run Code Online (Sandbox Code Playgroud)
2.将标记设置为图表
IMarker marker = new YourMarkerView();
chart.setMarker(marker);
Run Code Online (Sandbox Code Playgroud)
参考:https : //github.com/PhilJay/MPAndroidChart/wiki/IMarker-Interface
| 归档时间: |
|
| 查看次数: |
8968 次 |
| 最近记录: |