MPAndroidChart:饼图或自定义标签中的负值 [英] MPAndroidChart: negative values in pie chart or custom labels
问题描述
在我的Android应用程序中,我必须以饼图显示我的收入和支出金额.费用金额为负数.金额类型为float
.因此,可以使用MPAndroidChart库在饼图中显示负值和正值吗?
In my Android application I have to show my income and expense amounts in a piechart. The expense amounts will be negative. The amounts are of type float
. So is it possible to display both negative and positive values in a pie chart using MPAndroidChart library?
我尝试使用一些负和正整数值,结果令人难以置信.饼图的行为会像拉米纸牌或照明屏幕一样疯狂.
I have tried with some negative and positive integer values and the result is horible. The pie-chart will behave crazy like a rummy board or illumination screen.
如果我不能在饼图中使用负值,即使实际值是正数,是否也可以将标签显示为负数?
If I can't have negative values in a pie chart, is it possible to show the labels in negative by even though the actual values are positive?
推荐答案
您实际上是在问如何自定义 IValueFormatter
.
You are essentially asking how to customise the labels on an MPAndroidChart chart. This can be done easily by writing your own implementation of the interface IValueFormatter
.
IValueFormatter
有一种您需要实现的方法:
IValueFormatter
has one method that you need to implement:
getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler)`
您可以使用方法的参数将所需的标签构造为String.您想要在前面加减号-",因此只需执行以下操作:
You use the parameters of the method to construct the label you want as a String. You want a minus sign "-" in front so you simply do this:
return "-" + mFormat.format(value) + " %";
这是完整的实现:
public class NegativePercentFormatter implements IValueFormatter
protected DecimalFormat mFormat;
public NegativePercentFormatter() {
mFormat = new DecimalFormat("###,###,##0.0");
}
public NegativePercentFormatter(DecimalFormat format) {
this.mFormat = format;
}
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return "-" + mFormat.format(value) + " %";
}
}
这篇关于MPAndroidChart:饼图或自定义标签中的负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!