Android开发:MPAndroidChart的MarkerView如何展示与X轴不同的日期格式?
MPAndroidChart 自定义MarkerView展示完整日期实现
现有问题
开发基于MPAndroidChart的温度折线图时,自定义了MarkerView组件展示选中点的日期、温度信息,参考效果如下:
需求是MarkerView上展示MM/dd格式的完整日期(例如03/05),但目前只能显示和X轴一致的仅日文本(例如只显示05)。
已实现的逻辑
- 数据初始化阶段准备了三类集合:
xValue:存储月/日格式的完整日期文本label:存储仅保留日部分的短日期文本entries:存储对应索引位置的温度数值
- 数据拆分组装代码:
ArrayList<String> xValue = new ArrayList<>(); // 存储月/日格式的日期 ArrayList<String> label = new ArrayList<>(); // 存储仅日格式的日期 List<Entry> entries = new ArrayList<>(); // 存储温度数值 // 遍历原始接口/本地数据拆分日期字段 String[] str = data.getTestDate().split("-"); String thisMonth = str[1]; // 提取月份部分 String DayOfMonth = str[2]; // 提取当月日部分 String testDay = thisMonth + "/" + DayOfMonth; // 拼接为月/日格式 // 温度有效时才加入折线数据列表 if (data.getTemperature() > 0) { Entry entry = new Entry(i, (float) data.getTemperature()); entries.add(entry); } label.add(DayOfMonth); // 短标签仅存入日部分文本 xValue.add(testDay); // 完整月/日格式存入xValue列表 // X轴绑定短label的格式化器,实现X轴只显示日部分 xAxis.setValueFormatter(new IndexAxisValueFormatter(label)); - 自定义MarkerView代码:
public class LineChartMarkView extends MarkerView{ private TextView tvDate; private TextView tvValue; private IAxisValueFormatter xAxisValueFormatter; public LineChartMarkView(Context context, IAxisValueFormatter xAxisValueFormatter) { super(context, R.layout.layout_markview); this.xAxisValueFormatter = xAxisValueFormatter; tvDate = findViewById(R.id.tv_date); tvValue = findViewById(R.id.tv_value); } @SuppressLint("SetTextI18n") @Override public void refreshContent(Entry e, Highlight highlight) { // 直接复用传入的X轴格式化器取文本,拿到的自然是短日期 tvDate.setText(xAxisValueFormatter.getFormattedValue(e.getX(), null)); tvValue.setText("" + e.getY() + "℃"); super.refreshContent(e, highlight); } }
问题根源
当前初始化MarkerView时,直接把X轴使用的、绑定了短日期列表label的格式化器传给了Marker,导致Marker取文本时拿到的就是X轴用的短日期,无法读取到xValue里的完整月/日文本。
实现方案
两种最简单的改法,不需要动原有X轴的显示逻辑:
- 方案1:给MarkerView单独传入绑定完整日期列表的格式化器
因为创建Entry时用的是和列表索引一一对应的自增序号i,只要在初始化Marker时,不再传入X轴用的短标签格式化器,改用xValue列表构造新的IndexAxisValueFormatter传入即可:// 原来的错误写法:把X轴的短标签格式化器传给了Marker // LineChartMarkView markView = new LineChartMarkView(context, new IndexAxisValueFormatter(label)); // 修改为:用完整日期列表构造格式化器传给Marker LineChartMarkView markView = new LineChartMarkView(context, new IndexAxisValueFormatter(xValue)); chart.setMarker(markView); - 方案2:让MarkerView直接持有完整日期列表,减少格式化器的依赖
把MarkerView的构造方法调整为接收完整日期列表,在刷新内容时通过Entry自带的X坐标值(就是之前存入的索引)直接取对应日期:public class LineChartMarkView extends MarkerView{ private TextView tvDate; private TextView tvValue; private List<String> fullDateList; public LineChartMarkView(Context context, List<String> fullDateList) { super(context, R.layout.layout_markview); this.fullDateList = fullDateList; tvDate = findViewById(R.id.tv_date); tvValue = findViewById(R.id.tv_value); } @SuppressLint("SetTextI18n") @Override public void refreshContent(Entry e, Highlight highlight) { // Entry的X值是整数索引,直接转成整型取列表值 int index = (int) e.getX(); String fullDate = fullDateList.get(index); tvDate.setText(fullDate); tvValue.setText(e.getY() + "℃"); super.refreshContent(e, highlight); } }
注意事项
- 三个集合
entries、label、xValue必须保持顺序、长度完全一致,否则会出现选中点和日期文本错位的问题。 - 如果后续有过滤无效温度数据的逻辑,记得三个集合同步移除无效项,不要只修改
entries的内容。
内容的提问来源于stack exchange,提问作者林姿妤
相关产品推荐
相关产品推荐

