Luc*_*ano 9 charts datetime android android-graphview
我正在使用GraphView库(参见:https://github.com/jjoe64/GraphView或http://www.jjoe64.com/p/graphview-library.html)
但我想使用日期/时间作为X-as.有谁知道一个简单的方法来完成这个或任何人都可以推动我朝着正确的方向?
app*_*ter 12
这是执行此操作的正确方法
您只需使用unix时间戳(从1.01.1970开始的秒数)作为x值.
然后,您可以设置自定义标签格式化程序并将unix时间戳转换为String:
final java.text.DateFormat dateTimeFormatter = DateFormat.getTimeFormat(mActivity);
LineGraphView graphView = new LineGraphView(mActivity, entry.getValue()) {
@Override
protected String formatLabel(double value, boolean isValueX) {
if (isValueX) {
// transform number to time
return dateTimeFormatter.format(new Date((long) value*1000));
} else {
return super.formatLabel(value, isValueX);
}
}
};
Run Code Online (Sandbox Code Playgroud)
GraphView 是一个很棒的库,我发现它也是最简单的。执行此操作的第一步是在 GraphView.java 内的 GraphViewData 类中添加一个字符串变量。像这样:
static public class GraphViewData {
public final double valueX;
public final double valueY;
public final String valueDate;
public GraphViewData(double valueX, double valueY,String valueDate) {
super();
this.valueX = valueX;
this.valueY = valueY;
this.valueDate = valueDate;
}
}
Run Code Online (Sandbox Code Playgroud)
当您在创建 GraphView 图表时创建 GraphViewData 对象时,您需要添加字符串形式的日期数据(以及 X 和 Y)。
假设您的图表中有 80 个数据点(索引 0 - 79)。GraphView 中有一个方法负责生成和返回水平标签,我相信它称为generateHorLabels。使用 X 值从 GraphData 对象获取字符串,而不是仅返回 X 值 (0-79)。
在您现在的代码中,for循环中应该包含以下内容
labels[i] = formatLabel(min + ((max-min)*i/numLabels), true);
Run Code Online (Sandbox Code Playgroud)
除了上述之外,您还可以执行类似的操作。
Double temp = Double.valueOf(formatLabel(min + ((max-min)*i/numLabels), true));
int rounded =(int)Math.round(temp);
labels[i] = values[rounded].valueDate;
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!
| 归档时间: |
|
| 查看次数: |
12216 次 |
| 最近记录: |