AndroidPlot:我怎么能更改域的数据? [英] AndroidPlot: How i can change the domain data?
问题描述
我实际使用AndroidPlot库使用一个简单的图表在我的Android项目,但我不知道我可以改变域区域的值。
I use actually the AndroidPlot library to use a simple chart in my android project but I don't know how i can change the values of domain zone.
更具体的,这一行:
mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#"));
在网站上说,我可以使用另一种格式,其真正的,但如果我使用的,例如:
In the web site said that i can use another formats and its true, but if I use for example:
SimpleDateFormat("dd-MM-yyyy")
在显示的图表31-12-1969在所有的域值。
In the chart appears "31-12-1969" in all domain values.
有人知道我可以改变日期吗?或使用其他格式(如String)?
Somebody know how I can change that date? or use another format (like String)?
推荐答案
晚的答案,但也许会是其他人是有用的。
Late answer, but maybe will be useful to other people.
我也有一个困难时期这个问题,尤其是因为我的Java初学者。
我实现了一个自定义的格式。似乎有点丑陋的解决方案,但它的工作原理。想法是这样的:
I also had a hard time with this issue, especially since I am Java beginner. I implemented a custom formatter. Seems a bit ugly solution but it works. Idea is the following:
- 只需要Y轴的值,并留下X轴为索引到一个数组(如Androidplot教程表明,使用ArrayFormat.Y_VALS_ONLY使用,// Y_VALS_ONLY意味着使用元素索引作为x值)
- 每次数据的变化,你需要传递给绘图一个新格式化为X轴的新数据
下面是一些code片段。
Here are some code snippets.
首先是转换数组索引到一个自定义标签的String类:
First is the class which transforms array index to a custom label String:
public class MyIndexFormat extends Format {
public String[] Labels = null;
@Override
public StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos) {
// try turning value to index because it comes from indexes
// but if is too far from index, ignore it - it is a tick between indexes
float fl = ((Number)obj).floatValue();
int index = Math.round(fl);
if(Labels == null || Labels.length <= index ||
Math.abs(fl - index) > 0.1)
return new StringBuffer("");
return new StringBuffer(Labels[index]);
}
这里是我如何将其连接到剧情:
And here is how I attach it to the Plot:
MyIndexFormat mif = new MyIndexFormat ();
mif.Labels = // TODO: fill the array with your custom labels
// attach index->string formatter to the plot instance
pricesPlot.getGraphWidget().setDomainValueFormat(mif);
这招作品也进行动态更新。
This trick works also for dynamic updates.
注意:Androidplot似乎与画横线的问题,因此,如果您的数据具有相同的Y值,你可能会得到奇怪的结果,我已经问了在这个问题上的帮助。
NOTICE: Androidplot seems to have problems with drawing horizontal lines, so if your data has the same Y values, you might get strange results, I already asked for help on this issue.
这篇关于AndroidPlot:我怎么能更改域的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!