MPAndroidChart-如何最好地将X轴值设置为字符串/日期? [英] MPAndroidChart - How can I best set the X-axis values as strings/dates?
问题描述
这个 MPAndroid图表库让我有点迷茫.
我从此处开始了初学者示例,该示例建议创建一个数组/使用getValueX()和getValueY()方法的对象列表,然后将其添加为条目,如下所示:
I began the beginner example here, which recommends creating an array/list of objects with getValueX() and getValueY() methods, which you would then add as entries, like this:
List<Entry> entries = new ArrayList<Entry>(); // entry list
for(ValueAndDateObject data : valueAndDateArrayList){
// valueAndDateArrayList is the list of my own data
//(objects with a String X-value, and a double Y-value)
entries.add(new Entry(data.getValueX(), data.getValueY())); //error here
}
那里是一个错误,因为Entry仅采用(浮点数x,浮点数y).显然,我可以将double转换为浮点数,但是我需要x轴具有非浮点的日期.因此,我进行了更多搜索,发现了很多有关Github的问题,但是那里的解决方案似乎无法理解.多数指向类似于文档中的此.我遇到的问题是我不明白如何将此方法应用于Entry(float x,float y)问题.我找不到文档/解决方案说何时/何地适用于Entry().
There's an error there because Entry only takes (float x, float y). Obviously I can cast the double as a float, but I need the x-axis to have dates not floats. So I did some more searching and found a lot of Github issues about it, but the solutions there I can't seem to understand. Most point to something like this in the docs. The problem I'm having is I don't understand how this gets applied to the Entry(float x, float y) issue. I can't find when/where the docs/solutions say to apply to Entry().
因此,当我看有关创建格式化程序然后对其进行设置的示例时,我不知道entry.add(new Entry())东西的出现位置.该技术可以代替它吗?我会以某种方式通过吗?
So when I look at the example about creating the formatter and then setting it, I don't get where the entries.add(new Entry()) stuff comes in. Does that technique replace it? Do I somehow pass it in?
作为参考,这是我的完整方法,它是根据前面提到的初学者示例设计的.
For reference, here's my full method, designed after the beginner example mentioned previously.
public void updateUI(final ArrayList<ValueAndDateObject> valueAndDateArrayList){
List<Entry> entries = new ArrayList<Entry>();
for(ValueAndDateObject data : valueAndDateArrayList){
/** for the sake of the example, let's say there's only one
* ValueAndDataObject in the list and getValueX() returns "02-27-2016"
* and getValueY() returns 12,345.0
*/
entries.add(new Entry(data.getValueX(), data.getValueY())); //error obviously
}
// would I add the formatter somewhere in here? And what would I "add" it to?
LineDataSet dataSet = new LineDataSet(entries, "Label");
dataSet.setColor(Color.YELLOW);
dataSet.setValueTextColor(Color.BLACK);
LineData lineData = new LineData(dataSet);
lineChart.setData(lineData);
lineChart.invalidate();
}
据我所知,我将必须创建自己的格式化程序类,该类将基于格式化程序doc链接中的string []示例进行建模.我的电话挂在设置格式程序"部分.只是不确定如何更改/交互与Entry(float x,float y)问题.
From what I can tell, I'll have to make my own formatter class, which I'll model off of the string[] example in the formatter doc link. My hang up is on the "Setting the formatter" section. Just not sure about how that will change/interact with the Entry(float x, float y) issue.
推荐答案
在创建 Entry
对象时,您只需要将 Date
作为时间戳即可,例如:
You simply need to treat your Date
as a timestamp when creating the Entry
object, e.g.:
entries.add(new Entry(new Long(data.getDate().getTime()).floatValue(), new Double(data.getValueY()).floatValue()));
在上面,我们假设 data.getDate()
返回一个 java.util.Date
和 data.getValueY()
返回 double
.
In the above, we assume that data.getDate()
returns a java.util.Date
and that data.getValueY()
returns a double
.
然后,在您的 IAxisValueFormatter
中要做的就是将时间戳记表示形式转换回 Date
,然后转换为人类可读的格式,例如:
Then all you have to do in your IAxisValueFormatter
is to convert the timestamp representation back to a Date
and then into something human readable, like so:
public class DateValueFormatter implements IAxisValueFormatter {
@Override
public String getFormattedValue(float value, AxisBase axis) {
// Simple version. You should use a DateFormatter to specify how you want to textually represent your date.
return new Date(new Float(value).longValue()).toString();
}
// ...
}
更新:
设置值格式器:
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new DateValueFormatter());
更新:
用户'Yasir-Ghunaim'在该线程在github上.
User 'Yasir-Ghunaim' provides a very good step-by-step guide as a response in this thread on github.
这篇关于MPAndroidChart-如何最好地将X轴值设置为字符串/日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!