[关闭]
@zifeng328573112 2021-03-17T11:14:44.000000Z 字数 7454 阅读 1201

MPAndroidChart技术-数据格式器 ValueFormatter(五)

一、ValueFormatter 接口

1. 简介

2. 源码

  1. public interface ValueFormatter {
  2. /**
  3. * Called when a value (from labels inside the chart) is formatted
  4. * before being drawn. For performance reasons, avoid excessive
  5. * calculationsand memory allocations inside this method.
  6. *
  7. * @param value the value to be formatted
  8. * @param entry the entry the value belongs to - in
  9. * e.g. BarChart, this is of class BarEntry
  10. * @param dataSetIndex the index of the DataSet the entry in
  11. * focus belongs to
  12. * @param viewPortHandler provides information about the current
  13. * chart state (scale, translation, ...)
  14. * @return the formatted label ready for being drawn
  15. */
  16. String getFormattedValue(float value, Entry entry, int dataSetIndex,
  17. ViewPortHandler viewPortHandler);
  18. }

3. 自定义数据格式例子

  1. public interface ValueFormatter {
  2. /**
  3. * Called when a value (from labels inside the chart) is formatted
  4. * before being drawn. For performance reasons, avoid excessive calculations
  5. * and memory allocations inside this method.
  6. *
  7. * @param value the value to be formatted
  8. * @param entry the entry the value belongs to - in e.g. BarChart,
  9. * this is of class BarEntry
  10. * @param dataSetIndex the index of the DataSet the entry in focus belongs to
  11. * @param viewPortHandler provides information about the current
  12. * chart state (scale, translation, ...)
  13. * @return the formatted label ready for being drawn
  14. */
  15. String getFormattedValue(float value, Entry entry, int dataSetIndex,
  16. ViewPortHandler viewPortHandler);
  17. }

然后,设置格式为ChartData或DataSet对象:

  1. // usage on whole data object
  2. lineData.setValueFormatter(new MyValueFormatter());
  3. // usage on individual dataset object
  4. lineDataSet.setValueFormatter(new MyValueFormatter());

4. 预定义的自定义格式

1) LargeValueFormatter

可用于格式化 大于”1.000” 的值。 它会被转变,比如

  1. public class LargeValueFormatter implements ValueFormatter, YAxisValueFormatter {
  2. private static String[] SUFFIX = new String[]{
  3. "", "k", "m", "b", "t"
  4. };
  5. private static final int MAX_LENGTH = 4;
  6. private DecimalFormat mFormat;
  7. private String mText = "";
  8. public LargeValueFormatter() {
  9. mFormat = new DecimalFormat("###E0");
  10. }
  11. /**
  12. * Creates a formatter that appends a specified text to the result string
  13. *
  14. * @param appendix a text that will be appended
  15. */
  16. public LargeValueFormatter(String appendix) {
  17. this();
  18. mText = appendix;
  19. }
  20. // ValueFormatter
  21. @Override
  22. public String getFormattedValue(float value, Entry entry, int dataSetIndex,
  23. ViewPortHandler viewPortHandler) {
  24. return makePretty(value) + mText;
  25. }
  26. // YAxisValueFormatter
  27. @Override
  28. public String getFormattedValue(float value, YAxis yAxis) {
  29. return makePretty(value) + mText;
  30. }
  31. /**
  32. * Set an appendix text to be added at the end of the formatted value.
  33. *
  34. * @param appendix
  35. */
  36. public void setAppendix(String appendix) {
  37. this.mText = appendix;
  38. }
  39. /**
  40. * Set custom suffix to be appended after the values.
  41. * Default suffix: ["", "k", "m", "b", "t"]
  42. *
  43. * @param suff new suffix
  44. */
  45. public void setSuffix(String[] suff) {
  46. if (suff.length == 5) {
  47. SUFFIX = suff;
  48. }
  49. }
  50. /**
  51. * Formats each number properly. Special thanks to Roman Gromov
  52. * (https://github.com/romangromov) for this piece of code.
  53. */
  54. private String makePretty(double number) {
  55. String r = mFormat.format(number);
  56. r = r.replaceAll("E[0-9]",
  57. SUFFIX[Character.getNumericValue(r.charAt(r.length() - 1)) / 3]);
  58. while (r.length() > MAX_LENGTH || r.matches("[0-9]+\\.[a-z]")) {
  59. r = r.substring(0, r.length() - 2) + r.substring(r.length() - 1);
  60. }
  61. return r;
  62. }
  63. }

2) PercentFormatter

Used for displaying a “%” sign after each value with 1 decimal digit .
对于 PieChart 来说非常有用。PercentFormatter 源码如下:

  1. public class PercentFormatter implements ValueFormatter, YAxisValueFormatter {
  2. protected DecimalFormat mFormat;
  3. public PercentFormatter() {
  4. mFormat = new DecimalFormat("###,###,##0.0");
  5. }
  6. /**
  7. * Allow a custom decimalformat
  8. *
  9. * @param format
  10. */
  11. public PercentFormatter(DecimalFormat format) {
  12. this.mFormat = format;
  13. }
  14. // ValueFormatter
  15. @Override
  16. public String getFormattedValue(float value, Entry entry, int dataSetIndex,
  17. ViewPortHandler viewPortHandler) {
  18. return mFormat.format(value) + " %";
  19. }
  20. // YAxisValueFormatter
  21. @Override
  22. public String getFormattedValue(float value, YAxis yAxis) {
  23. return mFormat.format(value) + " %";
  24. }
  25. }
  26. ···

二、XAxisValueFormatter 接口

1. 概述

v2.1.4 后才有了这个接口。

2. 自定义格式示例

  1. public class MyCustomXAxisValueFormatter implements XAxisValueFormatter {
  2. @Override
  3. public String getXValue(String original, int index, ViewPortHandler viewPortHandler) {
  4. // original is the original value to use, x-index is the index in your x-values array
  5. // implement your logic here ...
  6. return ...;
  7. }
  8. }
  1. // usage on XAxis, get axis instance:
  2. XAxis xAxis = chart.getXAxis();
  3. // set the formatter
  4. xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());

3. 预定义了的 XAxisValueFormatters

  1. package com.github.mikephil.charting.formatter;
  2. import com.github.mikephil.charting.utils.ViewPortHandler;
  3. /**
  4. * Default formatter class for adjusting x-values before drawing them.
  5. * This simply returns the original value unmodified.
  6. */
  7. public class DefaultXAxisValueFormatter implements XAxisValueFormatter {
  8. @Override
  9. public String getXValue(String original, int index,
  10. ViewPortHandler viewPortHandler) {
  11. return original; // just return original, no adjustments
  12. }
  13. }

4. 自定义 XAxisValueFormatters 范例

  1. package com.xxmassdeveloper.mpchartexample.custom;
  2. import com.github.mikephil.charting.utils.ViewPortHandler;
  3. import com.github.mikephil.charting.formatter.XAxisValueFormatter;
  4. public class MyCustomXAxisValueFormatter implements XAxisValueFormatter {
  5. public MyCustomXAxisValueFormatter() {
  6. // maybe do something here or provide parameters in constructor
  7. }
  8. @Override
  9. public String getXValue(String original, int index, ViewPortHandler viewPortHandler) {
  10. /* Log.i("TRANS", "x: " + viewPortHandler.getTransX()
  11. + ", y: " + viewPortHandler.getTransY()); */
  12. // e.g. adjust the x-axis values depending on scale / zoom level
  13. if (viewPortHandler.getScaleX() > 5)
  14. return "4";
  15. else if (viewPortHandler.getScaleX() > 3)
  16. return "3";
  17. else if (viewPortHandler.getScaleX() > 1)
  18. return "2";
  19. else
  20. return original;
  21. }
  22. }

三、YAxisValueFormatter 接口

1. 概述

v2.1.4 后才有了这个接口。

2. 自定义格式示例

  1. public class MyYAxisValueFormatter implements YAxisValueFormatter {
  2. private DecimalFormat mFormat;
  3. public MyYAxisValueFormatter () {
  4. mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
  5. }
  6. @Override
  7. public String getFormattedValue(float value, YAxis yAxis) {
  8. // write your logic here
  9. // access the YAxis object to get more information
  10. return mFormat.format(value) + " $"; // e.g. append a dollar-sign
  11. }
  12. }

然后,为 YAxis 对象设置格式器:

  1. // get an instance of the YAxis (e.g. left axis)
  2. YAxis leftAxis = chart.getAxisLeft();
  3. leftAxis.setValueFormatter(new MyYAxisValueFormatter());

3. 预定义的自定义格式

LargeValueFormatterPercentFormatter .

四、三种格式器的实例效果图分析

1. ValueFormatter

2. XValueFormatter

3. YValueFormatter

  1. public class MyYAxisValueFormatter implements YAxisValueFormatter {
  2. private DecimalFormat mFormat;
  3. public MyYAxisValueFormatter() {
  4. mFormat = new DecimalFormat("###,###,###,##0.0");
  5. }
  6. @Override
  7. public String getFormattedValue(float value, YAxis yAxis) {
  8. return mFormat.format(value) + " $";
  9. }
  10. }

img39img40

  1. YAxis leftAxis = chart.getAxisLeft();
  2. leftAxis.setTextSize(24f);
  3. leftAxis.setTextColor(Color.GREEN);
  4. // 给Y轴的标签文本设置数据各格式器,上面的左图未设置格式器
  5. leftAxis.setValueFormatter(new MyYAxisValueFormatter());

注意:leftAxissetValueFormatter 参数类型只能是 YAxisFormatter !
img41

五、 其他章节索引

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注