package com.ghostwan.dtake.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.util.Log;
import com.ghostwan.dtake.DayFormatter;
import com.ghostwan.dtake.R;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.*;
import com.orm.SugarDb;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@EFragment(R.layout.fragment_stats)
public class StatsFragment extends Fragment {

    private static final String TAG = "StatsFragment";
    @ViewById
    HorizontalBarChart chart;

    @AfterViews
    protected void initViews(){

        SQLiteDatabase database = SugarDb.getInstance().getDB();

        List<BarEntry> entryList = new ArrayList<>();

        Cursor cursor = database.rawQuery("select count(*), take_date " +
                "from take " +
                "group by date(take_date/1000, 'unixepoch', 'localtime') " +
                "order by take_date", null);

        Log.d(TAG, "Data count: "+cursor.getCount());
        int i = 0;
        Long[] dates = new Long[cursor.getCount()];
        while (cursor.moveToNext()){
            int count = cursor.getInt(0);
            long datetime = cursor.getLong(1);
            entryList.add(new BarEntry(i, count));
            dates[i] = datetime;
            i++;
        }
        cursor.close();
        BarDataSet dataSet = new BarDataSet(entryList, "Pills"); // add entries to dataset
        BarData lineData = new BarData(dataSet);

        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new DayFormatter(chart, dates));
        chart.setVisibleXRangeMaximum(4);
        chart.setData(lineData);
        chart.invalidate(); // refresh



    }
    @Override
    public void onStart() {
        super.onStart();

    }
}

@@ -31,16 +31,48 @@
 gment;%0A%0A
+import android.content.Context;%0A
 import a
@@ -161,56 +161,192 @@
 oid.
-support.v4.app.Fragment;%0Aimport android.util.Log
+os.Bundle;%0Aimport android.support.annotation.Nullable;%0Aimport android.support.v4.app.Fragment;%0Aimport android.util.Log;%0Aimport android.view.View;%0Aimport com.ghostwan.dtake.DTakeService
 ;%0Aim
@@ -573,16 +573,73 @@
 data.*;%0A
+import com.github.mikephil.charting.utils.ColorTemplate;%0A
 import c
@@ -757,24 +757,76 @@
 .EFragment;%0A
+import org.androidannotations.annotations.Receiver;%0A
 import org.a
@@ -898,31 +898,8 @@
 st;%0A
-import java.util.Date;%0A
 impo
@@ -1060,25 +1060,20 @@
 t%22;%0A
+%0A
     
-@ViewById%0A   
+private
  Hor
@@ -1142,18 +1142,356 @@
 tViews()
+ 
 %7B%0A
+        updateChart();%0A    %7D%0A%0A    @Override%0A    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) %7B%0A        super.onViewCreated(view, savedInstanceState);%0A        chart = (HorizontalBarChart)view.findViewById(R.id.chart);%0A    %7D%0A%0A    @Receiver(actions = DTakeService.ACTION_COUNT)%0A    protected void updateChart()%7B
 %0A       
@@ -1602,16 +1602,77 @@
 ist%3C%3E();
+%0A        List%3CBarEntry%3E entryListCurrent = new ArrayList%3C%3E();
 %0A%0A      
@@ -1926,17 +1926,19 @@
 count: %22
-+
+ + 
 cursor.g
@@ -2055,16 +2055,17 @@
 oNext())
+ 
 %7B%0A      
@@ -2147,16 +2147,63 @@
 ong(1);%0A
+            if(i+1 != cursor.getCount()) %7B%0A    
         
@@ -2241,24 +2241,133 @@
 i, count));%0A
+            %7D%0A            else %7B%0A                entryListCurrent.add(new BarEntry(i, count));%0A            %7D%0A
             
@@ -2541,48 +2541,257 @@
     
-BarData lineData = new BarData(dataSet);
+dataSet.setValueTextSize(11);%0A%0A        BarDataSet dataSetCurrent = new BarDataSet(entryListCurrent, %22Current%22); // add entries to dataset%0A        dataSetCurrent.setColors(ColorTemplate.VORDIPLOM_COLORS);%0A        dataSetCurrent.setValueTextSize(12);%0A
 %0A%0A  
@@ -2914,63 +2914,49 @@
 .set
-VisibleXRangeMaximum(4);%0A        chart.setData(lineData
+Data(new BarData(dataSet, dataSetCurrent)
 );%0A 
@@ -2997,89 +2997,14 @@
 esh%0A
-%0A%0A%0A
     %7D%0A
-    @Override%0A    public void onStart() %7B%0A        super.onStart();%0A%0A    %7D
+%0A
 %0A%7D%0A

Update Chart when database change. Add current BarDataSet