2016-05-13 9 views
0

Ich habe ein horizontales Balkendiagramm mit X-Werten. Im Moment kann ich nur einen BarEntry mit einem Schwimmer Wert wie folgt erstellen:MPAndroidChart: Wie BarEntry als String formatiert?

BarEntry barEntry = new BarEntry((float)iAmountInCents/100, ++ index,item.getKey()); 
valueSet1.add(barEntry); 

ich auch einen benutzerdefinierten Formatter hinzugefügt:

public class MyYAxisValueFormatter implements YAxisValueFormatter { 

     private DecimalFormat mFormat; 

     public MyYAxisValueFormatter() { 
      mFormat = new DecimalFormat("###,###,##0.00"); 
     } 

     @Override 
     public String getFormattedValue(float value, YAxis yAxis) { 
      // write your logic here 
      // access the YAxis object to get more information 
      return "₹. " + mFormat.format(value) ; 
     } 
    } 

YAxis leftAxis = mChart.getAxisLeft(); 
     leftAxis.setValueFormatter(new MyYAxisValueFormatter()); 

     YAxis rightAxis = mChart.getAxisRight(); 
     rightAxis.setValueFormatter(new MyYAxisValueFormatter()); 

Aber das GetFormattedValue() -Methode wird NIE genannt, die Ergebnisse in so etwas auf einem Balken in der Tabelle: 0,400. Ich möchte, dass dieser Wert im Balkendiagramm wie "$ .40" aussieht. Wie kann ich das erreichen?

enter image description here

Antwort

0

Sie setzen die Formatierungs an einer falschen Stelle. Sie setzen den Formatierer auf YAxis. Stattdessen müssen Sie den Formatierer für das BarDataSet-Objekt festlegen, das Sie dem Diagramm zuweisen.

eine Formatter-Klasse Erstellen von ValueFormatter erstreckt

public class MyValueFormatter implements ValueFormatter { 

    private DecimalFormat mFormat; 

    public MyValueFormatter() { 
     mFormat = new DecimalFormat("###,###,###,##0.0"); 
    } 

    @Override 
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { 
     return mFormat.format(value) + " $"; 
    } 
} 

Sie haben die Formatierer setzen, wenn Sie die auf die Tabelle Datensatz zuweisen mit

BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1"); 
set1.setValueFormatter(new MyValueFormatter());