我正在尝试在单个视图中组合多个折线图,但无法解决不同数据类型的问题。我尝试使用最小-最大规格化来缩放来自不同数据集的值,但是当我想在自定义标记视图中显示选定的值时,我无法将其转换回初始格式。我已尝试对所有数据集使用不同的ValueFormatters,但未完成任何事情。
规范化代码:
private List<Entry> normalizeEntry(List<Entry> entries, int min, int max) {
for (int i = 0; i < entries.size(); i++) {
float lastValue = entries.get(i).getY();
float newValue = normalize(lastValue, min, max, 0, 100);
entries.set(i, new Entry(i, newValue));
}
return entries;
}
我能够通过将与标准化之前的值对应的参数添加到Entry对象的构造函数来解决我的问题。
before:
new Entry(xValue, normalizedValue);
after:
new Entry(xValue, normalizedValue, initialValueWithOutputFormat);
示例:
entries.add(new Entry(i, normalizedValues.get(i), String.format("%.2f m", inputValues.get(i))));
然后,我可以像下面的代码一样轻松地更新自定义标记:
public class CustomMarkerView extends MarkerView {
private TextView tvContent;
public CustomMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// this markerview only displays a textview
tvContent = findViewById(R.id.tvContent);
}
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText(String.valueOf(e.getData()));
super.refreshContent(e, highlight);
}
@Override
public MPPointF getOffset() {
return new MPPointF(-(getWidth() / 2), -getHeight());
}
}