Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeseries chart in MPAndroidChart

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?

like image 945
Utkarsh Narain Srivastava Avatar asked Aug 17 '16 08:08

Utkarsh Narain Srivastava


1 Answers

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));
like image 170
TR4Android Avatar answered Sep 20 '22 17:09

TR4Android