我之前发布了一个关于在 GraphView 库中使用 CustomFormatLabeler 将时间显示为 x 标签的问题(https://stackoverflow.com/questions/21567853/using-customlabelformatter-to-display-time-in-x-axis) . 我仍然找不到解决方案,所以我尝试编辑 GraphView 库。我尝试了这里建议的解决方案:Using dates with the Graphview library
我修改了:
public GraphViewData(double valueX, double valueY)
正如通过添加第三个输入变量 (String valueDate) 和一个名为 getTime() 的方法所建议的那样,该方法返回此字符串值。然后我修改了generateHorlabels,如下图:
private String[] generateHorlabels(float graphwidth) {
int numLabels = getGraphViewStyle().getNumHorizontalLabels()-1;
if (numLabels < 0) {
numLabels = (int) (graphwidth/(horLabelTextWidth*2));
}
String[] labels = new String[numLabels+1];
double min = getMinX(false);
double max = getMaxX(false);
for (int i=0; i<=numLabels; i++) {
Double temp = min + ((max-min)*i/numLabels);
int rounded =(int)Math.round(temp)-1;
if(rounded < 0){
labels[i] = " ";
}else{
if(graphSeries.size() > 0){
GraphViewDataInterface[] values = graphSeries.get(0).values;
if(values.length > rounded){
labels[i] = values[rounded].getTime();
}else{
labels[i] = " ";
}
}
}
}
return labels;
}
我不得不从四舍五入的变量中减去 1,因为我超出了边界错误。这比自定义格式标签器效果更好,因为水平标签和实时之间没有延迟。然而,在大约 600 个数据点之后,
rounded
变得大于长度
values
我得到了越界错误。有没有人尝试修改 GraphView 库以成功显示时间?我对java和android编程很陌生,所以一些建议会很棒。谢谢阅读。