Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle time series in MPAndroidChart?

I want to add the following sort of data (can be any number of such pairs under 1000) to the newly introduced timeseries chart in MPAndroidChart library

Value :  50.0   at   1472112259
Value :  49.0   at   1472112294
Value :  50.0   at   1472112329
Value :  50.0   at   1472112360
Value :  50.0   at   1472112392

The following data will be fetched from the array. Right now, I guess there is some mess up with the timestamps. Here is the complete code: https://gist.github.com/utkarshns/e1723dcc57022fcd392bc3b127b6c898

UNIX timestamps will be parsed to required time format after I can successfully add values to the graph.

Currently, the problem I face is that the timestamps probably get clipped and values are overwritten which leads to a pretty messed up graph with really weird x-axis values.

Update: Screenshots: https://i.sstatic.net/pCmHu.jpg

like image 762
Utkarsh Narain Srivastava Avatar asked Dec 05 '25 07:12

Utkarsh Narain Srivastava


1 Answers

The problem is that Float values can't hold very big numbers and still be accurate, so you need a separate List with these timestamp values. BigDecimal should be ok for this purpose. Your distances must be in accordance to the time gaps between your events. Just iterate from the start date to end date keeping count of how many timestamps you have and add Entry with count from the timestamps you wish your value to be.

Long myValues[] = {1472112259L, 1472112294L, 1472112329L, 1472112360L, 1472112392L};// your values

   ArrayList<Entry> values = new ArrayList<>();// Entry List
   Long start = 1472112259L;//start
   Long end = 1472112392L;//end
   List<BigDecimal> mList = new ArrayList<>(); //Decimal list which holds timestamps
   int count = 0;

   for (Long i = start; i <= end; i++) {

       mList.add(new BigDecimal(i));
       if (myValues.equals(i)) {
           values.add(new Entry(count, 50));
       }
       count++;//always increment
   }

And your ValueFormatter should look like this:

AxisValueFormatter() {

    private FormattedStringCache.Generic<Long, Date> mFormattedStringCache = new FormattedStringCache.Generic<>(new SimpleDateFormat("HH:mm:ss"));
    @Override
    public String getFormattedValue ( float value, AxisBase axis){

            return mFormattedStringCache.getFormattedValue(new Date(mList.get((int)value).longValueExact()*1000), value);
    }

    @Override
    public int getDecimalDigits () {
        return 0;
    }
}

If you have any question or something is unclear I'll be happy to help.

like image 190
Vygintas B Avatar answered Dec 07 '25 21:12

Vygintas B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!