目錄
第8節(jié).Setting Colors(MPAndroidChart中文翻譯)
第9節(jié).Formatting Data Values (ValueFormatter)(MPAndroidChart中文翻譯)
第10節(jié)-Formatting Axis Values (AxisValueFormatter)(MPAndroidChart中文翻譯)
第11節(jié).General Settings & Styling(MPAndroidChart中文翻譯)
第12節(jié).Specific Settings & Styling(MPAndroidChart中文翻譯)
第13節(jié).Legend(MPAndroidChart中文翻譯)
第14節(jié).Dynamic & Realtime Data(MPAndroidChart中文翻譯)
第15節(jié). Modifying the Viewport(MPAndroidChart中文翻譯)
第16節(jié).Animations(MPAndroidChart中文翻譯)
第17節(jié). MarkerView (Popup View)(MPAndroidChart中文翻譯)
第18節(jié). The ChartData class(MPAndroidChart中文翻譯)
第19節(jié). ChartData subclasses(MPAndroidChart中文翻譯)
第20節(jié). The DataSet class (general DataSet styling)(MPAndroidChart中文翻譯)
第21節(jié). DataSet subclasses (specific DataSet styling)(MPAndroidChart中文翻譯)
第22節(jié). The ViewPortHandler(MPAndroidChart中文翻譯)
第23節(jié). Customizing the Fill-Line-Position (FillFormatter)(MPAndroidChart中文翻譯)
第24節(jié). Proguard(MPAndroidChart中文翻譯)
第25節(jié). Realm.io mobile database(MPAndroidChart中文翻譯)
第26節(jié). Creating your own (custom) DataSets(MPAndroidChart中文翻譯)
第27節(jié). Miscellaneous (more useful stuff)(MPAndroidChart中文翻譯)
在v3.0.0中介紹,該接口允許在繪制前設(shè)置XAxis和YAxis的自定義格式
Creating a Formatter
實現(xiàn)軸上的值自定義格式化都需要創(chuàng)建一個類實現(xiàn)IAxisValueFormattera接口,如下所示.這個formatter用于將軸的值始終格式化為保留1位小數(shù).
public class MyYAxisValueFormatter implements IAxisValueFormatter {
private DecimalFormat mFormat;
public MyAxisValueFormatter() {
// format values to 1 decimal digit
mFormat = new DecimalFormat("###,###,##0.0");
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// "value" represents the position of the label on the axis (x or y)
return mFormat.format(value) + " $";
}
/** this is only needed if numbers are returned, else return 0 */
@Override
public int getDecimalDigits() { return 1; }
}
下面的例子展示了如何將String數(shù)組中的數(shù)值繪制到軸上:
public class MyXAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues;
public MyXAxisValueFormatter(String[] values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// "value" represents the position of the label on the axis (x or y)
return mValues[(int) value];
}
/** this is only needed if numbers are returned, else return 0 */
@Override
public int getDecimalDigits() { return 0; }
}
Setting the Formatter
創(chuàng)建Formatter后,簡單的將它設(shè)置給選擇的軸上:
YAxis left = chart.getAxisLeft();
left.setValueFormatter(new MyYAxisValueFormatter());
String[] values = new String[] { ... };
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MyXAxisValueFormatter(values));
替換了從軸的最小值到最大值之間的默認值,軸將會顯示經(jīng)過格式化的特殊數(shù)據(jù).
Restricting Intervals
如果你使用基于數(shù)組索引的formatter(像上面說的那個),設(shè)置最小間距為1是有意義的:
axis.setGranularity(1f); // restrict interval to 1 (minimum)
這將會防止繪制重復的標簽(由于軸的間距<1).當縮放比例足夠大的時候,將會停止計算最小間距
Predefined Formatters
- LargeValueFormatter:用于格式化大于1,000的數(shù)值.將1,000轉(zhuǎn)換為1k,1,000,000轉(zhuǎn)換為1m(million),1,000,000,000轉(zhuǎn)換為1b(billion),1 trillion轉(zhuǎn)換為1t.
- PercentFormatter:用于顯示在一個十進制數(shù)后面增加"%"標記.尤其適用于PieChart. 50->50.0%
*StackedValueFormatter:專門用于重疊BarChart.它語序指定顯示所有的重疊的數(shù)值,還是只顯示最頂部的數(shù)值.
Example Formatters
- DayAxisValueFormatter:這個Formatter可以將提供的數(shù)值轉(zhuǎn)換為日期字符串,并根據(jù)縮放變換.
LegacyFormatters
發(fā)布v3.0.0之前,XAxis和YAxis有單獨的formatter,文檔可以在下載找到: