Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

android - MPAndroidChart display markerview of Xaxis value when a xaxis value is clicked?

I want to show xaxis value in line chart. for example if a line one is clicked then it need to show line1 in markerview .I attach my code in below:

String[] xAxis= {"Line1","Line2","Line3","Line4"}

lineChart.setPinchZoom(false);
    lineChart.setDrawGridBackground(false);
    lineChart.getDescription().setText("");
    lineChart.setScaleEnabled(false);
    XAxis xAxis = lineChart.getXAxis();
    xAxis.setTextColor(Color.parseColor("#ef9a9a"));
    xAxis.setValueFormatter(new IndexAxisValueFormatter(xValues));
    xAxis.setEnabled(true);
    xAxis.setGranularity(1f);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularityEnabled(true);
    xAxis.setAvoidFirstLastClipping(false);
    xAxis.setDrawAxisLine(true);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTextSize(8);
    YAxis yAxisRight = lineChart.getAxisRight();
    yAxisRight.setDrawLabels(false);
    Legend legend = lineChart.getLegend();
    legend.setEnabled(true);
    CustomMarker cm = new CustomMarker(context, R.layout.custom_marker_view);
    cm.setChartView(lineChart);
    lineChart.setMarker(cm);
    lineChart.setVisibleXRangeMaximum(7);
   lineChart.moveViewToX(1);
    lineChart.invalidate();

//Markerclass: public class CustomMarkerView extends MarkerView {

private TextView tvContent;

public CustomMarkerView (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("" + e.getVal()); // set the entry-value as the display text
}

@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();
}

}

here I dnt know how to pass my Xaxis value in theis line tvContent.setText("" + e.getVal());//to show the particular x axis value if line one is clicked then it want to show line1

question from:https://stackoverflow.com/questions/66057525/mpandroidchart-display-markerview-of-xaxis-value-when-a-xaxis-value-is-clicked

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...