【问题标题】:MPAndroidChart - Barchart markerview not able to Display the Bar DataMPAndroidChart - 条形图标记视图无法显示条形数据
【发布时间】:2018-04-19 05:56:32
【问题描述】:

1.我正在尝试将标记视图设置为条形图。

2.Marker 背景图片显示正确,但 Value 未显示在标记顶部。

3.同时单击每个条 Toast 显示值。但无法设置标记视图的顶部

请帮我解决这个问题,在此先感谢。

gradle 库。

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);

【问题讨论】:

    标签: android mpandroidchart


    【解决方案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() 作为第一个参数。

    将您的布局文件更改为以下内容:这是最后一件事 mate

    <?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>
    

    【讨论】:

    • 仍然数据没有显示在markerview中,只有marker图像显示
    • 尝试将textview和textsize的文字颜色改为16dp
    • 好友查看已编辑的答案。按照我添加的说明进行操作。
    • 仍然遇到同样的问题空 Markerview :-(
    • 查看已编辑的答案这是我的工作示例伙伴
    【解决方案2】:

    在您的 Marker 类中,您必须在完成设置文本后刷新内容

     @Override
    public void refreshContent(Entry e, Highlight highlight) {
    
    
        tvContent.setText("HELLO");
        //Add this
        super.refreshContent(e, highlight);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      相关资源
      最近更新 更多