如何在饼图中隐藏零值 - MPChart android

时间:2015-10-05 03:48:23

标签: android mpandroidchart

有没有办法从MP安卓图表(饼图)中压缩零值文本

4 个答案:

答案 0 :(得分:8)

使用自定义格式化程序,如果值== 0.0f

,则返回空字符串
public class CustomPercentFormatter implements IValueFormatter {

    private DecimalFormat mFormat;

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

    public CustomPercentFormatter(DecimalFormat format) {
        this.mFormat = format;
    }

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

设置格式化程序:

lineData.setValueFormatter(new CustomPercentFormatter());

答案 1 :(得分:4)

那怎么样?

if(value > 0) dataSet.add(...)
else {
   // do nothing
}

或者编写自己的ValueFormatter来压制零。

答案 2 :(得分:2)

您可以在答案中声明为 Ali Zarei MPChart实施新格式化程序。但您可能仍需要已在DefaultValueFormatter

中实现的库的数字设置选项

要实现此目的,您只需创建一个新的格式化程序,然后从DefaultValueFormatterOverride getFormattedValue DefaultValueFormatter方法扩展它,并检查该值是否大于public class NonZeroChartValueFormatter extends DefaultValueFormatter { public NonZeroChartValueFormatter(int digits) { super(digits); } @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { if (value > 0) { return mFormat.format(value); } else { return ""; } } } 0或不。

class Program
{
    List<Employee> listOfEmp = new List<Employee>();
    List<Department> listOfDepart = new List<Department>();

    public Program()
    {
        listOfDepart = new List<Department>(){
            new Department { Id = 1, DeptName = "DEV" },
            new Department { Id = 2, DeptName = "QA" },
            new Department { Id = 3, DeptName = "BUILD" },
            new Department { Id = 4, DeptName = "SIT" }
        };


        listOfEmp = new List<Employee>(){
            new Employee { Empid = 1, Name = "Manikandan",DepartmentId=1 },
            new Employee { Empid = 2, Name = "Manoj" ,DepartmentId=1},
            new Employee { Empid = 3, Name = "Yokesh" ,DepartmentId=0},
            new Employee { Empid = 3, Name = "Purusotham",DepartmentId=0}
        };

    }
    static void Main(string[] args)
    {
        Program ob = new Program();
        ob.LeftJoin();
        Console.ReadLine();
    }

    private void LeftJoin()
    {
        listOfEmp.GroupJoin(listOfDepart.DefaultIfEmpty(), x => x.DepartmentId, y => y.Id, (x, y) => new { EmpId = x.Empid, EmpName = x.Name, Dpt = y.FirstOrDefault() != null ? y.FirstOrDefault().DeptName : null }).ToList().ForEach
            (z =>
            {
                Console.WriteLine("Empid:{0} EmpName:{1} Dept:{2}", z.EmpId, z.EmpName, z.Dpt);
            });
    }
}

class Employee
{
    public int Empid { get; set; }
    public string Name { get; set; }
    public int DepartmentId { get; set; }
}

class Department
{
    public int Id { get; set; }
    public string DeptName { get; set; }
}

答案 3 :(得分:0)

Kotlin 开发人员:

class MyCustomValueFormatter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
    return if (value == 0f) {
        ""
    } else {
        value.roundToInt().toString() + " %"
    }
  }
}

并像这样使用

pieData.setValueFormatter(MyCustomValueFormatter())