How to change the entry label in line chart?

Viewed 32

I am using com.github.mikephil.charting.charts.LineChart to display the chart of weights, but I want to change the value 208 Kg to 208L or 208K. I am using the famous MpAndroidChart. I am not able to change the line data unit from "Kg" to "L".

This is the code->

entries.add(new Entry(i, 208f));

private void setUpChart() {
        mBinding.flChartHolder.removeAllViews();
        mBinding.barChart.setData(null);
        mBinding.barChart.invalidate();
        mBinding.barChart.setDescription(null);

        mBinding.barChart.setGridBackgroundColor(R.color.white);
        mBinding.barChart.getLegend().setEnabled(false);
        mBinding.barChart.setScaleEnabled(false);

        mBinding.barChart.setData(generateLineData());

    YAxis rightAxis = mBinding.barChart.getAxisRight();
    rightAxis.setDrawLabels(false);
    rightAxis.setDrawGridLines(false);
    rightAxis.setXOffset(8f);
    rightAxis.setAxisLineColor(Color.TRANSPARENT);
    rightAxis.setGranularity(0f);


    XAxis xAxis = mBinding.barChart.getXAxis();

    xAxis.setDrawLabels(true);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setYOffset(4f);
    xAxis.setAxisLineColor(Color.TRANSPARENT);
    xAxis.setGranularity(0f);
    xAxis.setTextColor(ContextCompat.getColor(this, R.color.colorGreen));
    xAxis.setGranularityEnabled(true);
    xAxis.setTextSize(10f);

    YAxis leftAxis = mBinding.barChart.getAxisLeft();
    leftAxis.setXOffset(4f);
    leftAxis.setAxisLineColor(Color.TRANSPARENT);
    leftAxis.setGranularity(0f);
    leftAxis.setDrawZeroLine(false);
    leftAxis.setTextColor(ContextCompat.getColor(this, R.color.dinner_color));
    leftAxis.setGranularityEnabled(true);
    if (mResult.getGraph() != null && mResult.getGraph().size() > 0) {
        leftAxis.setAxisMinimum((float) (startValue - (float) 60));
        leftAxis.setAxisMaximum((float) (startValue + (float) 60));
    }
    leftAxis.setLabelCount((int) leftAxis.getAxisMaximum() / 20, true);
    leftAxis.setTextSize(10f);

   xAxis.setLabelCount(mResult.getGraph().size(), true);
    xAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            Logger.e("valus---" + value);
            int index = (int) value;
            return getStringFromDate(index);
        }
    });


    mBinding.barChart.invalidate();
    mBinding.flChartHolder.addView(mBinding.barChart);
}

private LineData generateLineData() {

    LineData d = new LineData();

    ArrayList<Entry> entries = new ArrayList<>();
    for (int i = 0; i < mResult.getGraph().size(); i++) {

        if (mUserData.getCurrentUnitMeasure() != IMERIAL_UNIT) {
            entries.add(new Entry(i, toLbs(Float.valueOf(String.valueOf(mResult.getGraph().get(i).getWeightKg())))));
        } else {
            entries.add(new Entry(i, Float.valueOf(String.valueOf(mResult.getGraph().get(i).getWeightKg()))));
        }
    }

    entries.add(new Entry(0, 0f));

    LineDataSet set = new LineDataSet(entries, "rttrtrt");
    set.notifyDataSetChanged();
    set.setDrawIcons(false);
    set.setColors(ContextCompat.getColor(this, R.color.colorGreen));

    set.setDrawCircles(true);
    set.setLabel("test");
    set.setCircleColor(ContextCompat.getColor(this, R.color.colorGreen));
    set.setCircleRadius(3.5f);
    set.setCircleHoleRadius(0f);
    set.setLineWidth(1.5f);
    set.setValueTextColor(ContextCompat.getColor(this, R.color.colorGreen));
    set.setDrawHorizontalHighlightIndicator(true);
    set.setValueTextSize(12f);
    set.setDrawHorizontalHighlightIndicator(false);
    set.setDrawVerticalHighlightIndicator(false);

    set.setValueFormatter(new ValueFormatter() {

        @Override
        public String getPointLabel(Entry entry) {

            DecimalFormat df = new DecimalFormat("#.00");
            Float weight2DecimalsAtEnd = Float.valueOf(df.format(entry.getY()));

            Log.d("weight2DecimalsAtEnd", weight2DecimalsAtEnd.toString());

            if (mUserData.getCurrentUnitMeasure() != IMERIAL_UNIT) {
                return weight2DecimalsAtEnd + "L";
            } else {
                return weight2DecimalsAtEnd + "K";
            }
        }
    });

    d.addDataSet(set);

    return d;
}

enter image description here

The output should be 208.45L

I am using com.github.mikephil.charting.charts.LineChart to display the chart of weights, but I want to change the value 208 Kg to 208L or 208K. I am using the famous MpAndroidChart. I am not able to change the line data unit from "Kg" to "L".

1 Answers

You can use the ValueFormatter provided by the library like this

  val formatter: ValueFormatter = object : ValueFormatter() {

        override fun getPointLabel(entry: Entry?): String {
            //formatted string label
            return entry?.y.toString()+" Lbs"
        }
    }

Set the formatter to your dataset

// add entries to dataset
val dataSet = LineDataSet(entries, "Random Chart") 
dataSet.valueFormatter = formatter

...// rest of your code

Kotlin is just to give you example you can find Java code here in documentation.

Related