Reputation: 1313
To do: Plot time series line chart using the library, where x-axis time-stamps are not equidistant.
Sample Data:
1467886121: 325
1467886153: 326
1467886185: 325
1467886248: 326
1467886280: 326
1467886311: 326
1467886343: 327
1467886375: 327
1467886406: 327
1467886438: 328
1467886529: 328
1467886561: 327
1467886593: 327
1467886625: 326
1467886659: 327
1467886692: 326
1467886725: 326
Note: LHS values are UNIX timestamps and RHS values are data points at corresponding timestamps. The timestamps aren't equidistant and so they should be spaced according to the time difference between the two. Recently, MPAndroidChart introduced this functionality [github: link]
Over here they seem to be adding new values separated by one hour. I want to add values according to the timegap in UNIX timestamps.
How do I go about that?
Upvotes: 8
Views: 7886
Reputation: 1313
TR4Android's answer is absolutely fine except for the fact the library doesn't support long datatype for the timestamps.
So, I have gone with the following approach:
Sample Data: 1467886121: 325, 1467886153: 326, 1467886185: 325, 1467886248: 326
Taking timestamp_1 to be zero or the base. The following timestamps become base timestamp(first timestamp) - itself.
So, the data now becomes like so,
Processed Sample Data: 0: 325, 32: 326, 64: 325, 127: 326
This data can now be passed on to the charting library and the drawn graph has its time-series nature preserved.
Upvotes: 4
Reputation: 3246
Time series are supported in the latest version of the library, at the time of writing this would be v3.0.0-beta1
. Make sure you import that version in your build.gradle
file.
It is pretty straightforward to construct your entries, as each entry now consists of a x-value and a corresponding y-value, just like your data (how you retrieve your x- and y-values depends on how your input data is structured):
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < length; i++) {
long x = ... // 1467886121, 1467886153, ...
int y = ... // 325, 326, ...
entries.add(new Entry(x, y));
}
LineDataSet dataSet = new LineDataSet(entries, "Time series");
LineData data = new LineData(dataSet);
mLineChart.setData(data);
Since you are creating a time series, you'll probably also want to format the axis labels to be dates, you can find the sample's DayAxisValueFormatter
on GitHub. You'll need to adjust this to fit your needs and then add it to your x-axis:
mLineChart.getXAxis().setValueFormatter(new DayAxisValueFormatter(mLineChart));
Upvotes: 5