2016-05-30 247 views
0

它可能是愚蠢的问题,我正在使用水平mpandroid条形图有x轴数字如6,000,000所以x轴正在崩溃,所以需要做的是将数字转换成像印度卢比6 cr我该如何做到目前为止我什么我尝试是:如何在水平mpandroidchart中自定义x轴值?

这是水平条形图我在哪里设定值:

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

这是值格式化:

/** 
* 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 <[email protected]> 
*/ 
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轴值如何我可以这样做吗?任何人提前帮助我,谢谢!

回答

0

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

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

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

希望这会有所帮助。

0

你需要用下面的函数替换现有的函数,它为我工作。

私人字符串makePretty(双号){

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