如何在水平mpandroidchart中自定义x轴值?

时间:2016-05-30 12:23:27

标签: android mpandroidchart

使用水平mpandroid条形图可能是一个愚蠢的问题我在x轴上有数字像6,000,000所以x轴变得崩溃所以需要做的是将数字转换成印度卢比如6 cr我怎么能这样做到目前为止什么我试过的是:

这是设置水平条形图的值:

  ArrayList<BarEntry> entries = new ArrayList<>();
        ArrayList<String> labels = new ArrayList<String>();
        Format format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
      //  System.out.println(format.format(new BigDecimal("100000000")));



        for(int i=0;i<listobj.size();i++){
          String s=  format.format(listobj.get(i).getData());
        //    Log.e("ss", s);
//            Integer numberformat=Integer.parseInt(s);
            entries.add(new BarEntry(listobj.get(i).getData(),i));
            labels.add(listobj.get(i).getLabel());
        }
        XAxis axis=barChart.getXAxis();
        BarDataSet dataset = new BarDataSet(entries, "# of Calls");
        //dataset.setValueFormatter(new LargeValueFormatter());
        BarData datas = new BarData(labels, dataset);
      ///  datas.setValueFormatter(new LargeValueFormatter());
        barChart.setData(datas);
        barChart.setDescription("Description");
        barChart.getLegend().setWordWrapEnabled(true);

这是值Formatter:

/**
 * Created by 4264 on 30-05-2016.
 */

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;

/**
 * Predefined value-formatter that formats large numbers in a pretty way.
 * Outputs: 856 = 856; 1000 = 1k; 5821 = 5.8k; 10500 = 10k; 101800 = 102k;
 * 2000000 = 2m; 7800000 = 7.8m; 92150000 = 92m; 123200000 = 123m; 9999999 =
 * 10m; 1000000000 = 1b; Special thanks to Roman Gromov
 * (https://github.com/romangromov) for this piece of code.
 *
 * @author Philipp Jahoda
 * @author Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
 */
class LargeValueFormatter implements ValueFormatter {

    private static String[] SUFFIX = new String[]{
            "", "k", "m", "b", "t"
    };
    private static final int MAX_LENGTH = 4;
    private DecimalFormat mFormat;
    private String mText = "";

    public LargeValueFormatter() {
        mFormat = new DecimalFormat("###E0");
    }

    /**
     * Creates a formatter that appends a specified text to the result string
     *
     * @param appendix a text that will be appended
     */
    public LargeValueFormatter(String appendix) {
        this();
        mText = appendix;
    }

    // ValueFormatter
   @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return makePretty(value) + mText;
    }


    // YAxisValueFormatter


    /**
     * Set an appendix text to be added at the end of the formatted value.
     *
     * @param appendix
     */
    public void setAppendix(String appendix) {
        this.mText = appendix;
    }

    /**
     * Set custom suffix to be appended after the values.
     * Default suffix: ["", "k", "m", "b", "t"]
     *
     * @param suff new suffix
     */
    public void setSuffix(String[] suff) {
        if (suff.length == 5) {
            SUFFIX = suff;
        }
    }

    /**
     * Formats each number properly. Special thanks to Roman Gromov
     * (https://github.com/romangromov) for this piece of code.
     */
    private String makePretty(double number) {

        String r = mFormat.format(number);

        r = r.replaceAll("E[0-9]", SUFFIX[Character.getNumericValue(r.charAt(r.length() - 1)) / 3]);

        while (r.length() > MAX_LENGTH || r.matches("[0-9]+\\.[a-z]")) {
            r = r.substring(0, r.length() - 2) + r.substring(r.length() - 1);
        }

        return r;
    }

    }

我不知道如何自定义x轴值如何才能做到这一点任何人都可以帮助我提前感谢!!

2 个答案:

答案 0 :(得分:0)

您可以尝试两种方法来解决问题 1)插入值时,您可以将值除以1000,000并将标签更改为6Cr。我有一个图表,其值从0到30,一个值是4000.在将值存储在数据库中时,我将其除以100,并在显示时更改标签以显示正确的值 示例SQL插入值:

 Float.valueOf(LEUCO_COUNT.getText().toString()) / 1000,

2)选项2是在显示之前将值除以1000,000,并修改你的babel以显示Cr。您可以通过API查找如何设置自定义标签

希望这有帮助。

答案 1 :(得分:0)

您需要用以下功能替换现有功能。它适合我。

private String makePretty(double number){

    double Cr = 10000000;
    double Lac = 100000;

    String result = mFormat.format(number);
    try {
        double LAmt = Double.parseDouble(result);
        String Append = "";
        if (LAmt > Cr) {//1Crore
            LAmt /= Cr;
            Append = "Cr";
        } else {
            LAmt /= Lac;
            Append = "L";
        }
        result = CommonUtils.GetFormattedString("" + LAmt) + Append;
    } catch (NumberFormatException e) {
        Log.d("", "");
    }
    return result;
}