MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

How to extend MarkerView

Open romangromov opened this issue 12 years ago • 7 comments

Hi Phil, thank you for really hard work and nice library. I'm trying to have my own MarkerView and I'm little confused with it. Could you please look into my code (it's the same as bundled examples):

res/layout/marker.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="65dp"
    android:layout_height="40dp"
    android:background="@drawable/ic_marker" >
    <TextView
        android:id="@+id/txtValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
MyMarkerView.java
public class MyMarkerView extends MarkerView {
    private TextView txtValue;
    public MyMarkerView (Context context, int layoutResource) {
        super(context, layoutResource);
        txtValue = (TextView) findViewById(R.id.txtValue);
    }
    @Override
    public void refreshContent(Entry e, int dataSetIndex) {
        txtValue.setText(" " + Utils.formatNumber(e.getVal(), 0, true));      
    }
}
MainActivity.java
...
mChart = (LineChart) findViewById(R.id.chart);
mChart.setHighlightEnabled(true);
mChart.setTouchEnabled(true);
mChart.setPinchZoom(false);
MyMarkerView marker = new MyMarkerView(this, R.layout.marker);
marker.setOffsets(-marker.getMeasuredWidth() / 2, -marker.getMeasuredHeight());
mChart.setMarkerView(marker);
mChart.setHighlightIndicatorEnabled(false);
mChart.setData(data);
mChart.invalidate();

In this way, when I taps on any Chart Entry my MyMarkerView pops up, but shows absolutely nothing. But if I add this line to my txtValue in res/layout/marker.xml

android:text="Selected."

then it start to work like a charm. Could you please show me the right way how to extend MarkerView? Thanks in advance!

romangromov avatar Sep 09 '14 19:09 romangromov

That's quite weird. It looks like you are doing everything correctly. I will have to look into that as soon as I return from my travels next week.

Is it a big issue for you to keep the line android:text="Selected." until then?

I believe the issue has something to do with the measuring of the size of the TextView. If it is empty prior to being drawn / rendered, it might not be able to adjust to the new value that is placed into the TextView when refreshContent(...) is called.

Regards, Phil

PhilJay avatar Sep 09 '14 20:09 PhilJay

Thank you for your time,

you're right: refreshContent(...) do not adjust size of the TextView with new value. Since that TextView text size is limited with initial string length provided by android:text="1234" attribute.

That's ok for me, but is there any way to just center TextView content horizontally? No luck with android:gravity="center"

romangromov avatar Sep 09 '14 20:09 romangromov

You probably will have to experiment with this until I get the time to fix it. It is most likely related to the same issue that causes no text to appear if no text is set for the TextView by default.

What you could try to do is remeasure the MarkerView upon calling refreshContent(...).

Unfortunately I currently do not have acces to a "real" computer, so I cannot fully address the issue.

PhilJay avatar Sep 09 '14 21:09 PhilJay

I just made some improvements to the MarkerView. Please let me know if these improvements solved your issue.

Regards, Phil

PhilJay avatar Sep 15 '14 18:09 PhilJay

Hi Phil,

thank you so much for your fast fix and examples update as well ! Now MarkerView works just fine as expected!

Closing issue right now, thank you!

romangromov avatar Sep 16 '14 06:09 romangromov

I am using version 3.0.3 of the library and I don't find the changes that you made here https://github.com/PhilJay/MPAndroidChart/commit/3b43c1187021a5fa20e4df25c2009796ee4585e9 in the Chart.java file. And the issue that you say these changes fixed, that issue is still there

zulkarnain-shah avatar Apr 05 '18 12:04 zulkarnain-shah

Calling the super method after setting the values in your view seems to resolve the issue.

 override fun refreshContent(e: Entry, highlight: Highlight) {

        xValueText.text = "x: " + e.x

        yValueText.text = "y: " + e.y

        super.refreshContent(e, highlight)

    }

thijsvdbb avatar Aug 24 '18 12:08 thijsvdbb