MarkerView no aparece con MPAndroidChart

Estoy utilizando MPandroidchart para mostrar los gráficos de línea en mi aplicación. He añadido código siguiente para mostrar Marker View, pero no se muestra en

private void initializeChart(LineChart chart, String chartName) { // Chart view chart.setDrawGridBackground(false); chart.setDescription(""); chart.getLegend().setEnabled(true); //chart.setTouchEnabled(false); int color = getResources().getColor(R.color.white); chart.getAxisLeft().setTextColor(color); // left y-axis chart.getXAxis().setTextColor(color); chart.setTouchEnabled(true); CustomMarkerView mv = new CustomMarkerView(this.getActivity(), R.layout.marker_view_tv); chart.setMarkerView(mv); //X axis XAxis xAxis = chart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setDrawLabels(true); //Y axis YAxis leftAxis = chart.getAxisLeft(); YAxis rightAxis = chart.getAxisRight(); rightAxis.setDrawLabels(false); rightAxis.setDrawGridLines(false); leftAxis.setDrawLabels(true); leftAxis.setDrawGridLines(false); leftAxis.setStartAtZero(false); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity()); leftAxis.setLabelCount(Constants.KEY_LINE_YAXIS_SCALECOUNT, true); ChartItem item = CannonJsonParser.parseCanonJson(act, act.res); int maxYVal = pref.getInt(Constants.KEY_YAXIS_VALUE, 0); leftAxis.setAxisMaxValue(maxYVal); leftAxis.setAxisMinValue(0); setLineData(item, chartName); // set data chart.setData(lineData); chart.getLegend().setEnabled(false); //animate //chart.animateX(2000, Easing.EasingOption.EaseInExpo); chart.setDragEnabled(true); chart.setScaleXEnabled(true); chart.setScaleYEnabled(false); chart.setHighlightPerDragEnabled(false); chart.setHighlightPerTapEnabled(false); } 

Mi clase CustomMarkerView

 public class CustomMarkerView extends MarkerView { private TextView tvContent; public CustomMarkerView(Context context, int layoutResource) { super(context, layoutResource); 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) { if (e instanceof CandleEntry) { CandleEntry ce = (CandleEntry) e; tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true)); } else { tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true)); } } @Override public int getXOffset(float xpos) { // this will center the marker-view horizontally return -(getWidth() / 2); } @Override public int getYOffset(float ypos) { // this will cause the marker-view to be above the selected value return -getHeight(); } 

}

Nota: Estoy usando fragmento para mostrar gráficos.

Tu MarkerView no se muestra porque no has resaltado ninguna entrada en tu gráfico. MarkerView sólo se muestra para las entradas que están resaltadas.

Puesto que inhabilitó la funcionalidad para resaltar las entradas por toque (llamando a chart.setHighlightPerTapEnabled(false) ), sólo puede resaltar los valores de forma programática, como esto:

chart.highlightValue(...)

Más sobre eso en la documentación .

Prueba esta clase

 import android.content.Context; import android.widget.TextView; import com.github.mikephil.charting.components.MarkerView; import com.github.mikephil.charting.data.CandleEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.utils.Utils; /** * Custom implementation of the MarkerView. * * @author Philipp Jahoda */ public class MyMarkerView extends MarkerView { private TextView tvContent; public MyMarkerView(Context context, int layoutResource) { super(context, layoutResource); 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, int dataSetIndex) { if (e instanceof CandleEntry) { CandleEntry ce = (CandleEntry) e; tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true)); } else { tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true)); } } @Override public int getXOffset() { // this will center the marker-view horizontally return -(getWidth() / 2); } @Override public int getYOffset() { // this will cause the marker-view to be above the selected value return -getHeight(); } } 

Y en Fragment Class, Add

  MyMarkerView mv = new MyMarkerView(this.getActivity(), R.layout.custom_marker_view); // set the marker to the chart mChart.setMarkerView(mv); 

Que esto te sea útil.

  • ¿Cómo auto scroll MPAndroidChart gráfico de línea?
  • Cómo desactivar la línea de resaltado en MPAndroidChart?
  • Gráfico de barras apiladas de Android
  • MPAndroidChart - Eliminar borde superior / eje desde v2
  • En MPAndroidChart, ¿cómo agregar el evento de clic para cada barra en Barchart?
  • Mostrar el valor cuando se pulsa
  • Cómo MPAndroidChart mostrar todos los valores xaxis
  • Cómo establecer el valor de incremento para el eje Y en MPAndroidChart
  • MPAndroidChart ¿cómo establecer el valor mínimo y máximo del eje y, o establecer el zoom predeterminado?
  • Dibuja el valor de la barra en la barra
  • MPAndroidChart MarkerView
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.