Home » Android » android – How to get x and two y axis values of chart-Exceptionshub

android – How to get x and two y axis values of chart-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I would like to get the value of X, and the values of the two axes Y and display them in MarkerViev. How can I do this? My markerview code and the desired result are presented below.

public class MyMarkerView extends MarkerView {

    private final TextView tvContent;
    private final TextView tvTime;

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

        tvContent = findViewById(R.id.tvFuel);
        tvTime = findViewById(R.id.time_chart);
    }

    // runs every time 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 {

            String fuelText = "Топливо: " + Utils.formatNumber(e.getY(), 0, true) + " л.";
            tvContent.setText(fuelText);
            //here get x string and second Y axis values...

        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}

Desired Result

How to&Answers:

Thanks for the comment, Manu. I solved the problem as follows.

public class MyMarkerView extends MarkerView {

private final TextView tvFuel;
private final TextView tvTime;
private final TextView tvSpeed;

private Float[] mFuelSet;
private Float[] mSpeedSet;
private String[] mTimeSet;

public MyMarkerView(Context context, int layoutResource, Float[] fuelSet, Float[] speedSet, String[] timeSet) {
    super(context, layoutResource);

    tvFuel = findViewById(R.id.tvFuel);
    tvSpeed = findViewById(R.id.tvSpeed);
    tvTime = findViewById(R.id.time_chart);

    mFuelSet = fuelSet;
    mSpeedSet = speedSet;
    mTimeSet = timeSet;
}

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

    if (e instanceof CandleEntry) {
        CandleEntry ce = (CandleEntry) e;
        tvFuel.setText(Utils.formatNumber(ce.getHigh(), 0, true));
    } else {

        int index = (int) e.getX();

        String fuelText = "Топливо: " + mFuelSet[index] + " л.";
        tvFuel.setText(fuelText);
        String speedText = "Скорость: " + mSpeedSet[index] + " км/ч";
        tvSpeed.setText(speedText);
        String timeText = mTimeSet[index];
        tvTime.setText(timeText);
    }

    super.refreshContent(e, highlight);
}

}