android - MPAndroidChart - 条形图标记 View 无法显示条形数据

标签 android mpandroidchart

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

2.标记背景图像显示正确,但值未显示标记顶部。

3.单击显示值的每个栏Toast时,但无法设置标记 View 的顶部

请帮我解决这个问题,提前致谢。

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>

自定义markerview.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);

最佳答案

将您的 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);

此外,如果您在 Activity 中,请使用它作为第一个参数,如果您在 fragment 中,请使用 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>

关于android - MPAndroidChart - 条形图标记 View 无法显示条形数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49913705/

相关文章:

android - 标签和条形图在 MPAndroidchart 条形图中未对齐

android - 来自 Keras 的卡住模型在恢复后无法预测

android - MPAndroid 图表 : "Unable to execute dex" issue

android - 获取标记打开链接 Google map React Native

android - .apk 文件中是否存在库文件?

android - 如何从 MpAndroidChart 中删除饼图上的多余空格?

android - MPAndroidChart - 饼图 - 自定义标签线

android - MPAndroidChart 线帽/结束样式

java - 此代码有什么问题?应用程序应获取位置

android - Gradle:任务 compileReleaseAidl 执行失败