I'm just a bit lost with this MPAndroid chart library .
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
}
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().
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();
}
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.
You simply need to treat your Date
as a timestamp when creating the Entry
object, eg:
entries.add(new Entry(new Long(data.getDate().getTime()).floatValue(), new Double(data.getValueY()).floatValue()));
In the above, we assume that data.getDate()
returns a java.util.Date
and that data.getValueY()
returns a double
.
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();
}
// ...
}
UPDATE:
Setting the value formatter:
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new DateValueFormatter());
UPDATE:
User 'Yasir-Ghunaim' provides a very good step-by-step guide as a response in this thread on github .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.