MPAndroidChart 教程:数据格式器 ValueFormatter(五),拿下我人生中第7个Offer

MPAndroidChart 教程:动画 Animations(十)

MPAndroidChart 教程:MarkerView(十一)

MPAndroidChart 教程:ChartData类,ChartData子类, DataSet类,DataSet子类(十二)

时间仓促,难免有错误,有的话希望大家在评论中指出,谢谢。

源码:范例代码在线查看或下载

一、ValueFormatter 接口


1. 简介

  • Interface that allows custom formatting of all values inside the chart before they are being drawn to the screen. Simply create your own formatting class and let it implement ValueFormatter. Then override the getFormattedValue(…) method and return whatever you want.

ValueFormatter 是一个接口,在被绘制到屏幕之前允许自定义图表内所有的值的格式。方法很简单,创建自己的格式类并让它实现 ValueFormatter 接口,然后覆盖 getFormattedValue(...) 方法返回你想要的。

2. 源码

public interface ValueFormatter {

/**

  • Called when a value (from labels inside the chart) is formatted

  • before being drawn. For performance reasons, avoid excessive

  • calculationsand memory allocations inside this method.

  • @param value the value to be formatted

  • @param entry the entry the value belongs to - in

  • e.g. BarChart, this is of class BarEntry
    
  • @param dataSetIndex the index of the DataSet the entry in

  • focus belongs to
    
  • @param viewPortHandler provides information about the current

  • chart state (scale, translation, ...)
    
  • @return the formatted label ready for being drawn

*/

String getFormattedValue(float value, Entry entry, int dataSetIndex,

ViewPortHandler viewPortHandler);

}

3. 自定义数据格式例子

public interface ValueFormatter {

/**

  • Called when a value (from labels inside the chart) is formatted

  • before being drawn. For performance reasons, avoid excessive calculations

  • and memory allocations inside this method.

  • @param value the value to be formatted

  • @param entry the entry the value belongs to - in e.g. BarChart,

  • this is of class BarEntry
    
  • @param dataSetIndex the index of the DataSet the entry in focus belongs to

  • @param viewPortHandler provides information about the current

  • chart state (scale, translation, ...)
    
  • @return the formatted label ready for being drawn

*/

String getFormattedValue(float value, Entry entry, int dataSetIndex,

ViewPortHandler viewPortHandler);

}

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

// usage on whole data object

lineData.setValueFormatter(new MyValueFormatter());

// usage on individual dataset object

lineDataSet.setValueFormatter(new MyValueFormatter());

4. 预定义的自定义格式

1) LargeValueFormatter

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

  • “1.000”变成”1k”

  • “1.000.000”变成”1m”(million)

  • “1.000.000.000”将“1b”(billion)

它不支持带有小数的数字,如“1.000,5”数字。LargeValueFormatter 源码如下:

public class LargeValueFormatter implements ValueFormatter, YAxisValueFormatter {

private static String[] SUFFIX = new String[]{

“”, “k”, “m”, “b”, “t”

};

private static final int MAX_LENGTH = 4;

private DecimalFormat mFormat;

private String mText = “”;

public LargeValueFormatter() {

mFormat = new DecimalFormat(“###E0”);

}

/**

  • Creates a formatter that appends a specified text to the result string

  • @param appendix a text that will be appended

*/

public LargeValueFormatter(String appendix) {

this();

mText = appendix;

}

// ValueFormatter

@Override

public String getFormattedValue(float value, Entry entry, int dataSetIndex,

ViewPortHandler viewPortHandler) {

return makePretty(value) + mText;

}

// YAxisValueFormatter

@Override

public String getFormattedValue(float value, YAxis yAxis) {

return makePretty(value) + mText;

}

/**

  • Set an appendix text to be added at the end of the formatted value.

  • @param appendix

*/

public void setAppendix(String appendix) {

this.mText = appendix;

}

/**

  • Set custom suffix to be appended after the values.

  • Default suffix: [“”, “k”, “m”, “b”, “t”]

  • @param suff new suffix

*/

public void setSuffix(String[] suff) {

if (suff.length == 5) {

SUFFIX = suff;

}

}

/**

  • Formats each number properly. Special thanks to Roman Gromov

  • (https://github.com/romangromov) for this piece of code.

*/

private String makePretty(double number) {

String r = mFormat.format(number);

r = r.replaceAll(“E[0-9]”,

SUFFIX[Character.getNumericValue(r.charAt(r.length() - 1)) / 3]);

while (r.length() > MAX_LENGTH || r.matches(“[0-9]+\.[a-z]”)) {

r = r.substring(0, r.length() - 2) + r.substring(r.length() - 1);

}

return r;

}

}

2) PercentFormatter

Used for displaying a “%” sign after each value with 1 decimal digit .

对于 PieChart 来说非常有用。PercentFormatter 源码如下:

public class PercentFormatter implements ValueFormatter, YAxisValueFormatter {

protected DecimalFormat mFormat;

public PercentFormatter() {

mFormat = new DecimalFormat(“###,###,##0.0”);

}

/**

  • Allow a custom decimalformat

  • @param format

*/

public PercentFormatter(DecimalFormat format) {

this.mFormat = format;

}

// ValueFormatter

@Override

public String getFormattedValue(float value, Entry entry, int dataSetIndex,

ViewPortHandler viewPortHandler) {

return mFormat.format(value) + " %";

}

// YAxisValueFormatter

@Override

public String getFormattedValue(float value, YAxis yAxis) {

return mFormat.format(value) + " %";

}

}

二、XAxisValueFormatter 接口


1. 概述

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

  • XAxisValueFormatter 接口可用于创建定制格式化器类,在它们绘制到屏幕之前 动态地调整 x-values

  • 对于使用 XValueFormatter 只需创建一个新的类,让它实现接口,通过 getXValue(...) 方法返回任何你想要显示的 X 轴标签。 50 - > 50.0%

2. 自定义格式示例

public class MyCustomXAxisValueFormatter implements XAxisValueFormatter {

@Override

public String getXValue(String original, int index, ViewPortHandler viewPortHandler) {

// original is the original value to use, x-index is the index in your x-values array

// implement your logic here …

return …;

}

}

  • 然后,为 X 轴设置格式器:

// usage on XAxis, get axis instance:

XAxis xAxis = chart.getXAxis();

// set the formatter

xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());

3. 预定义了的 XAxisValueFormatters

  • XAxisValueFormatters 源码:

package com.github.mikephil.charting.formatter;

import com.github.mikephil.charting.utils.ViewPortHandler;

/**

  • Default formatter class for adjusting x-values before drawing them.

  • This simply returns the original value unmodified.

*/

public class DefaultXAxisValueFormatter implements XAxisValueFormatter {

@Override

public String getXValue(String original, int index,
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

上面分享的字节跳动公司2020年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

就先写到这,码字不易,写的很片面不好之处敬请指出,如果觉得有参考价值的朋友也可以关注一下我

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

上面分享的字节跳动公司2020年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

就先写到这,码字不易,写的很片面不好之处敬请指出,如果觉得有参考价值的朋友也可以关注一下我

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

[外链图片转存中…(img-sjGfhf5e-1712786839810)]

[外链图片转存中…(img-HBsvZEJG-1712786839810)]

[外链图片转存中…(img-AyQUyVGk-1712786839811)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值