0
votes

I want to show x and y value on single tap on chart. I followed all steps but still it's not working. Is there anything going wrong?

This is my code

    LineData data = new LineData(getDataSet());

    chart.setData(data);
    chart.setDrawMarkerViews(true);
    chart.setTouchEnabled(true);
    chart.getDescription().setEnabled(false);
    chart.setOnChartGestureListener(this);
    chart.setHighlightPerTapEnabled(true);
    CustomMarkerView mv = new CustomMarkerView(this,R.layout.custom_marker_view_layout);
    mv.setChartView(chart);
    mv.setOffset(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());
    chart.setMarkerView(mv);
    chart.setPinchZoom(false);
    chart.setDrawGridBackground(false);

following is my code for custom marker view

public class CustomMarkerView extends MarkerView {

    private TextView tvContent;

    public CustomMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);

        tvContent = (TextView) findViewById(R.id.tvContent);

    }


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



        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

            tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
        } else {

            tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true));
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
3
Please explain more what you want it to do, what you did to try and make it so. - Flummox - don't be evil SE
i want to show x and y value on singlr tap(the point where we tap). For example i have a chart price vs date then on single tab marker view will show price value and date at that point. - vishal
Not enough info to debug this. What does "not working" mean? Have you determined with the debugger which parts are working and which not? - David Rawson

3 Answers

1
votes

I have done so using a Toast. To acheive this in my class I need to implement OnChartValueSelectedListener

public class MainPage implements OnChartValueSelectedListener

During the creation of the chart set the value selected listener to this class

mChart.setOnChartValueSelectedListener(this);

Now you need to override onValueSelected and implement something to display your selected items. For example a Toast:

@Override
public void onValueSelected(Entry e, Highlight h) {
    if(mCurrentToast != null)
    {
        mCurrentToast.cancel();
    }

    String res = String.format(Locale.ENGLISH, "Item: %f; Value: %.2f", e.getX(), e.getY());
    mCurrentToast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
    mCurrentToast.show();
}
0
votes

Your CustomMarkerview is not showing because you have not highlighted any value.

I have solved my issue using barchart.highlightValue(...);. See the code below:

@Override
public void onValueSelected(Entry e, Highlight h) {

        Highlight h1 = barchartCalories.getHighlighter().getHighlight(float x, float y);

        barchartCalories.highlightValue(h1);

        barchart.setMarkerView(new CustomMarkerView(activity,  R.layout.custom_marker_view));

}
-1
votes

setDrawMarkerViews and setMarkerView is deprecated,you can use below code:

final CustomMarkerView mv = new CustomMarkerView(this,R.layout.custom_marker_view_layout);
mv. setChartView (chart);
chart.setMarker(mv);