MPAndroidChart 在单击 xaxis 值时显示 Xaxis 值的标记视图?

时间:2021-02-05 04:20:01

标签: android android-studio

我想在折线图中显示 xaxis 值。例如,如果单击第一行,则需要在标记视图中显示 line1。我在下面附上我的代码:

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

标记类:

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

这里我不知道如何在这一行中传递我的 Xaxis 值:
tvContent.setText("" + e.getVal());
如果单击第 1 行,则显示特定的 x 轴值,则应显示第 1 行。

0 个答案:

没有答案