Using Dates With The Graphview Library
Solution 1:
This is the correct way to do this
You just have to use the unix timestamp (seconds from 1.01.1970) as x value.
Then you can set a custom label formatter and convert the unix timestamp to a String:
final java.text.DateFormatdateTimeFormatter= DateFormat.getTimeFormat(mActivity);
LineGraphViewgraphView=newLineGraphView(mActivity, entry.getValue()) {
@Overrideprotected String formatLabel(double value, boolean isValueX) {
if (isValueX) {
// transform number to timereturn dateTimeFormatter.format(newDate((long) value*1000));
} else {
returnsuper.formatLabel(value, isValueX);
}
}
};
Solution 2:
GraphView is a great library to use, i find it the easiest as well. The first step in doing this would be to add a String Variable in the GraphViewData Class within GraphView.java. Like So:
staticpublicclassGraphViewData {
publicfinaldouble valueX;
publicfinaldouble valueY;
publicfinal String valueDate;
publicGraphViewData(double valueX, double valueY,String valueDate) {
super();
this.valueX = valueX;
this.valueY = valueY;
this.valueDate = valueDate;
}
}
When you create your GraphViewData object when creating a GraphView Graph, you will need to add the date data in string form (along with the X and Y).
Lets say you have 80 data points in your graph (index 0 - 79). There is a method within GraphView that is responsible for generating and returning the horizontal labels, i believe its called generateHorLabels. Instead of just returning the X Value (0-79), Use the X value to get the String from the GraphData object.
In the code you have now, it should have the following in a for loop
labels[i] = formatLabel(min + ((max-min)*i/numLabels), true);
instead of the above, you could do something like this.
Double temp = Double.valueOf(formatLabel(min + ((max-min)*i/numLabels), true));
int rounded =(int)Math.round(temp);
labels[i] = values[rounded].valueDate;
Hope this helped!
Solution 3:
Here's the updated answer from jjoe64, with the x-values obtained from Date#getTime()
finalDateFormatdateTimeFormatter= DateFormat.getDateTimeInstance();
graphView = newLineGraphView(context, "Chart");
graphView.setCustomLabelFormatter(newCustomLabelFormatter()
{
@Overridepublic String formatLabel(double value, boolean isValueX)
{
if (isValueX)
{
return dateTimeFormatter.format(newDate((long) value));
}
returnnull; // let graphview generate Y-axis label for us
}
});
Solution 4:
I created the horizontal labels at the same time I was setting the values:
publicvoidinitializeLineGraphView() {
// Get last weeks entries from the DB
ArrayList<Entry> entries = DbManager.getInstance().getLastWeeksEntries(newDate());
String[] hLabels = newString[entries.size()];
GraphView.GraphViewData[] graphViewData = newGraphView.GraphViewData[entries.size()];
for(inti=0; i < entries.size(); i++) {
Entryentry= entries.get(i);
intpain= entry.getPain();
graphViewData[i] = newGraphView.GraphViewData(i, pain);
// Generate the horizontal labelsSimpleDateFormatsdf=newSimpleDateFormat("EEE");
StringdayOfWeek= sdf.format(entry.getDate());
hLabels[i] = dayOfWeek;
}
mSeries = newGraphViewSeries("", null, graphViewData);
GraphViewgraphView=newLineGraphView(getActivity(), "");
graphView.addSeries(mSeries);
graphView.setHorizontalLabels(hLabels);
graphView.setVerticalLabels(newString[] {"10", "5", "0"});
mLineGraphView = graphView;
}
Post a Comment for "Using Dates With The Graphview Library"