Kumar
Kumar

Reputation: 979

MPAndroidChart - Barchart markerview not able to Display the Bar Data

1.Iam trying to set the markerview to the Bar chart.

2.Marker background image is showing correctly ,but Value is not displaying top of the marker.

3.while clicking on the each bar Toast displaying the values.but not able to set top of markerview

Kindly help me to fix this issue , thanks in advance.

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


    }

from Fragment calling CustomMarkerView class

IMarker marker = new CustomMarkerView(this.getContext(), R.layout.barchart_marker_view_layout);
            mBarChart.setMarker(marker);
            mBarChart.setDrawMarkers(true);

Upvotes: 2

Views: 2244

Answers (2)

Al-Punk
Al-Punk

Reputation: 3660

On your Marker class, you have to refreshContent once you are done with setting a text

 @Override
public void refreshContent(Entry e, Highlight highlight) {


    tvContent.setText("HELLO");
    //Add this
    super.refreshContent(e, highlight);
}

Upvotes: 0

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

Replace your CustomMarker Class with following class:

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
}

}

Change following:

IMarker marker = new CustomMarkerView(this.getContext(), R.layout.barchart_marker_view_layout);

to:

      MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker);
      combinedChart.setMarker(mv);

Also if you are in activity use this as first argument and if you are in fragment use getContext() as first argument.

Change your layout file to following: This is the last thing 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>

Upvotes: 2

Related Questions