How to set two decimal point into Bar Entry in Mp android Bar Chart in Android
Asked Answered
N

1

8

How to set J SON data in bar entry in two decimal point?

enter image description here

Nashom answered 20/12, 2017 at 6:39 Comment(1)
Do you want to apply formatted values on axis labels or whole set of values present above bars as bar labels?Shufu
C
7

You can use The ValueFormatter interface

The IValueFormatter interface can be used to create custom-made formatter classes that allow to format values within the chart (from DataSets) in a specific way before drawing them.

For using the IValueFormatter, simply create a new class and let it implement the interface and return whatever you want to be displayed from the getFormattedValue(...) method.

Creating a Formatter

public class MyValueFormatter implements IValueFormatter {

    private DecimalFormat mFormat;

    public MyValueFormatter() {
        mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        // write your logic here
        return mFormat.format(value) + " $"; // e.g. append a dollar-sign
    }
}

Then, set your formatter to the ChartData or DataSet object:

// usage on whole data object
lineData.setValueFormatter(new MyValueFormatter());

// usage on individual dataset object
lineDataSet.setValueFormatter(new MyValueFormatter());

Predefined Formatters

  • LargeValueFormatter: Can be used for formatting large values > "1.000". It will turn values like "1.000" into "1k", "1.000.000" will be "1m" (million), "1.000.000.000" will be "1b" (billion) and values like one trillion will be e.g. "1t".

  • PercentFormatter: Used for displaying a "%" sign after each value with 1 decimal digit. Especially useful for the PieChart. 50 -> 50.0 %

  • StackedValueFormatter: A formatter specifically designed to be used with stacked BarChart. It allows to specify whether all stack values should be drawn or just the top value.

Conductor answered 20/12, 2017 at 6:44 Comment(3)
I used this but this can not applied show some errorNashom
Show me what kind of errConductor
Great Anand, please up-vote so other guys better found my solutionConductor

© 2022 - 2024 — McMap. All rights reserved.