1.Iam尝试将标记视图设置为条形图。
2.Marker背景图像显示正确,但Value未显示标记的顶部。
3.点击每个栏显示值的Toast。但无法设置markerview的顶部
请提前帮助我解决这个问题。
gradle lib。
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
barchart_marker_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_action_markericon"
android:gravity="center">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="6565"
android:textColor="@color/color_white"
android:textSize="8sp" />
</RelativeLayout>
Custommarkerview.class
public class CustomMarkerView extends MarkerView implements IMarker{
Context mContext;
private TextView tvContent;
public BarchartCustomMarkerView1(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());
ToastMsg.toastmsg(mContext,String.valueOf("values"+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;
}
}
来自Fragment的调用CustomMarkerView类
IMarker marker = new CustomMarkerView(this.getContext(), R.layout.barchart_marker_view_layout);
mBarChart.setMarker(marker);
mBarChart.setDrawMarkers(true);
答案 0 :(得分:1)
将CustomMarker类替换为以下类:
public class MyMarkerView extends MarkerView {
private TextView tvContent;
public MyMarkerView(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("x: " + e.getX() + " , y: " + e.getY()); // set the entry-value as the display text
}
}
更改以下内容:
IMarker marker = new CustomMarkerView(this.getContext(), R.layout.barchart_marker_view_layout);
为:
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker);
combinedChart.setMarker(mv);
此外,如果你在活动中使用它作为第一个参数,如果你在片段中,则使用getContext()作为第一个参数。
将您的布局文件更改为以下内容:这是配对的最后一件事
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/colorPrimary"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="12dp"
android:textColor="@android:color/black"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
答案 1 :(得分:0)
在Marker类上,设置完文本后,您必须要刷新Content
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("HELLO");
//Add this
super.refreshContent(e, highlight);
}