I have a MarkerView setup that worked in previous 2.x versions and the 3.0.0 beta. When changing to the 3.0.0 release version I updated to correct offset method.
@Override
public MPPointF getOffset()
{
if( offset == null )
{
offset = new MPPointF(-(getWidth() / 2), -getHeight() - 10);
}
return offset;
}
But now my marker view isn't being drawn on any of my charts. I've tried changing from the now deprecated setMarkerView to setMarker but that didn't result in any change. I've also added some debugging and my refreshContent method is being called with each touch.
I have a similar problem with 3.0.0.beta-1. In my case the markerView shows up fine within a Fragment but not on Activity.
I have a OnChartGuestureListener on the Activity whereas the Fragment does not have any gesture listener.
With the activity and OnChartGuestureListener I only get the guesture events like onChartGestureEnd, onChartSingleTapped etc and not the marker refreshContent. But the fragment just works fine.
I am not sure how I could get the MarkerView working with the OnChartGuestureListener in place.
The Marker class and its invoking is given below.
public class XYMarkerView extends MarkerView implements IMarker {
private TextView tvContent;
public XYMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
Log.d("XYMarkerView", "Constructor called");
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) {
Log.d("XYMarkerView", "refreshContent: " + e.toString());
tvContent.setText("" + e.getY());
// this will perform necessary layouting
//super.refreshContent(e, highlight);
}
@Override
public int getXOffset(float xpos) {
return 0;
}
@Override
public int getYOffset(float ypos) {
return 0;
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}
@Override
public MPPointF getOffsetForDrawingAtPos(float posX, float posY) {
return null;
}
}
Set the marker in the Activity as below
XYMarkerView marker = new XYMarkerView(getApplicationContext(), R.layout.custom_marker_view);
barChart.setMarkerView(marker);
Set the marker in the Fragment as below
XYMarkerView marker = new XYMarkerView(this.getContext(), R.layout.custom_marker_view);
mBarChart.setMarkerView(marker);
Even the marker on activity started working now. I found that in my activity I disabled the highlight where as in the fragment it is not.
When I commented the following, it works fine.
dataSet.setHighlightEnabled(false);
The wiki clearly says that the custom marker is for the highlighted entries....
Unfortunately that's not the case for me as I have setHighlightEnabled(true)
Sad, I am not sure of the issue with the version 3.0.0, but again I am not sure why you have to move from 3.0.0-beta to 3.0.0..
Faced this same issue. It only reproduced when my marker's TextView layout_width was set to wrap_content. I workaround this issue by changing it to a fixed size (40dp, for example) in the xml.
I was also faced with the same issue, and I solved it by calling:
super.refreshContent(e, highlight);
in the end of the refreshContent method, of my MarkerView subclass.
see #2650
I've tried calling the super method that has no affect. I have yet to get a chance to try the suggestion of changing the text view to a fixed width.
It looks everything go fine
MyChartMarkerView mv = new MyChartMarkerView(context, layoutResource);
linechart.setMarkerView(mv);
`public class MyChartMarkerView extends MarkerView {
private TextView tvContent;
public MyChartMarkerView (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());
// 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;
}
}`
Supply a fixed width to the layout for the marker view instead of wrap_content fixed the problem. Not sure when this change was made but it should probably be documented.
All suggestions above is not work for me. At last, I delete this android:gravity="center" in my textview. Then, It magical worked!!! @PhilJay
Following user 603530013's comment works for me. Wrap Content doesn't work because we did use its parent refreshConten() method.
//Be sure to call super.refreshContent(e, highlight) in your overridden refreshContent(...)
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("" + e.getY());
//This parent method will wrap your content as well
super.refreshContent(e, highlight);
}
none of the previous answers worked for me, i found that when you fill the entries from higher to lowest in xAxis the marker don't appear and even other bugs may appear, something like this will cause the error
int countX = speedTest.size();
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < speedTests.size(); i++) {
SpeedTest st = speedTests.get(i);
yVals1.add(new BarEntry(countX,st.getResultFloat()));
countX--;
}
for me, the only way to fix this was to fill the entries in xAxis from lowest to higher, so the next code worked for me after hours of debugging
int countX = 0;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < speedTests.size(); i++) {
SpeedTest st = speedTests.get(i);
yVals1.add(new BarEntry(countX,st.getResultFloat()));
countX++;
}
I was facing this problem as well so tried different method
Most helpful comment
Supply a fixed width to the layout for the marker view instead of wrap_content fixed the problem. Not sure when this change was made but it should probably be documented.