How to format values inside MPAndroidChart?

前端 未结 2 1869
有刺的猬
有刺的猬 2020-12-09 08:55

2 Questions about the MPAndroidChart library. All my yvalues are integers, but displayed as Decimals. How can I get them displayed as integers (without the digits)? How can

相关标签:
2条回答
  • 2020-12-09 09:12

    Have a look at the IValueFormatter interface provided by the library. With that interface, you can completely customize what gets displayed on the chart based on your own logic.

    Usage:

    chart.setValueFormatter(new YourValueFormatter());
    YLabels yl = chart.getYLabels();
    yl.setFormatter(new YourValueFormatter());
    

    UPDATE (for versions 2.0.0+ of this [library][2]):

    Now, a ValueFormatter can be set for each DataSet separately, or the same ValueFormatter can be set for the whole data object containig all DataSets. Furthermore, the YLabels class is now called YAxis.

    Example:

    // set for whole data object (individual DataSets also possible)
    LineData data = new LineData(...);
    data.setValueFormatter(new YourValueFormatter());
    
    // YLabels are now called YAxis
    YAxis yAxis = mChart.getAxisLeft(); // get the left or right axis
    yAxis.setValueFormatter(new YourAxisValueFormatter());
    

    UPDATE (for versions 3.0.0+ of this [library][2]):

    The interfaces for formatting have been renamed and extended in their functionality. Now, the IAxisValueFormatter can be used to format values of both XAxis and YAxis. The IValueFormatter interface is used to customize chart values.

    Link to the ValueFormatter documentation.

    0 讨论(0)
  • 2020-12-09 09:17

    If all you want is to change the number of decimal places on the values, the DefaultValueFormatter will suffice.

    pieDataSet.setDefaultValueFormatter(new DefaultValueFormatter(digits = 1))
    //where digits is the number of decimal places
    

    The example above will format 88.65 as 88.7

    0 讨论(0)
提交回复
热议问题