Search in sources :

Example 1 with DateAsXAxisLabelFormatter

use of com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter in project Thesis by bajnax.

the class LeConnectedDeviceActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_le_connected_device);
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        isServiceRunning = savedInstanceState.getBoolean(CONNECTION_STATE);
        hasReceivedServices = savedInstanceState.getBoolean(RECEIVED_SERVICES);
    } else {
        isServiceRunning = false;
        hasReceivedServices = false;
    }
    deviceStatus = (TextView) findViewById(R.id.deviceStatus);
    // spinner, its neighbouring textView, graph and connectivity to the SaMi cloud
    // won't be shown if another device is connected
    spinner = (ProgressBar) findViewById(R.id.spinner);
    lookUpText = (TextView) findViewById(R.id.lookUpTextView);
    spinner.setVisibility(View.GONE);
    lookUpText.setVisibility(View.GONE);
    toolBar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolBar);
    // setting up the graph's vertical labels
    sensorsGraph = (GraphView) findViewById(R.id.graph);
    sensorsGraph.setTitle("Current sensor\'s data");
    sensorsGraph.setTitleColor(R.color.colorPrimaryDark);
    sensorsGraph.getGridLabelRenderer().setVerticalAxisTitle("Value");
    sensorsGraph.getGridLabelRenderer().setHorizontalAxisTitle("Time");
    // enabling horizontal zooming and scrolling
    sensorsGraph.getViewport().setScalable(true);
    sensorsGraph.getGridLabelRenderer().setLabelVerticalWidth(50);
    sensorsGraph.getGridLabelRenderer().setLabelHorizontalHeight(50);
    sensorsGraph.getViewport().setYAxisBoundsManual(true);
    sensorsGraph.getViewport().setMinY(0);
    sensorsGraph.getViewport().setMaxY(40);
    // TODO: make the date labels on the X axis to be shown properly
    // set date label formatter
    SimpleDateFormat mDateFormatter = new SimpleDateFormat("MM-dd HH:mm:ss");
    sensorsGraph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(LeConnectedDeviceActivity.this, mDateFormatter));
    // only 2 because of the space
    sensorsGraph.getGridLabelRenderer().setNumHorizontalLabels(2);
    Calendar calendar = Calendar.getInstance();
    long t1 = calendar.getTimeInMillis();
    long t2 = calendar.getTimeInMillis() + 15000;
    sensorsGraph.getViewport().setXAxisBoundsManual(true);
    sensorsGraph.getViewport().setMinX((double) t1);
    sensorsGraph.getViewport().setMaxX(((double) t2));
    // as we use dates as labels, the human rounding to nice readable numbers
    // is not necessary
    sensorsGraph.getGridLabelRenderer().setHumanRounding(false);
    temperatureSeries = new PointsGraphSeries<>();
    sensorsGraph.addSeries(temperatureSeries);
    expListView = (ExpandableListView) findViewById(R.id.expandableListView);
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager != null)
        mBluetoothAdapter = bluetoothManager.getAdapter();
    else
        finish();
    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    deviceAddress = intent.getStringExtra("deviceAddress");
    if (deviceAddress == null) {
        finish();
    }
    // setting up the progress bar, in case the BLE Shield is connected
    if (deviceAddress.equals(GattAttributesSample.DEVICE_ADDRESS)) {
        Drawable progressDrawable = spinner.getIndeterminateDrawable().mutate();
        progressDrawable.setColorFilter(getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
        spinner.setProgressDrawable(progressDrawable);
        spinner.setVisibility(View.VISIBLE);
        lookUpText.setVisibility(View.VISIBLE);
    }
    // Starting service if it is not running yet and binding to it afterwards
    Intent gattServiceIntent = new Intent(getApplicationContext(), BluetoothLowEnergyService.class);
    // the service will be created only once
    getApplicationContext().startService(gattServiceIntent);
    // service clients are able to bind to it at any time
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    registerReceiver(mGattUpdateReceiver, GattUpdateIntentFilter());
    /* if (mBluetoothLEService != null && !isServiceRunning) {
            final boolean result = mBluetoothLEService.connect(deviceAddress);
            Toast.makeText(LeConnectedDeviceActivity.this,
                    "Connect request result: " + result, Toast.LENGTH_SHORT).show();
        }*/
    expListView.setVisibility(View.GONE);
    sensorsGraph.setVisibility(View.GONE);
    final SensorsDataViewModel sensorsDataViewModel = ViewModelProviders.of(this).get(SensorsDataViewModel.class);
    sensorsDataViewModel.getTemperatures().observe(this, new Observer<List<Temperature>>() {

        @Override
        public void onChanged(@Nullable final List<Temperature> temperatures) {
            if (temperatures == null) {
                Toast.makeText(LeConnectedDeviceActivity.this, "No data received", Toast.LENGTH_SHORT).show();
            } else if (temperatures.size() > 0) {
                if (temperatures.size() == 1) {
                    // set manual x bounds to have nice steps
                    // sensorsGraph.getViewport().setXAxisBoundsManual(true);
                    sensorsGraph.getViewport().setMinX((double) temperatures.get(0).getTimestamp());
                    sensorsGraph.getViewport().setMaxX((double) sensorsGraph.getViewport().getMinX(false) + 15000);
                    Log.d(TAG, "Initial timestamp, MinLabelX: " + mDateFormatter.format((double) temperatures.get(0).getTimestamp()));
                    Log.d(TAG, "Final timestamp, MaxLabelX: " + mDateFormatter.format(((double) sensorsGraph.getViewport().getMinX(false) + 15000)));
                }
                /*//scales programmatically
                    sensorsGraph.getViewport().setMaxX((double)temperatures.get(temperatures.size()-1).getTimestamp());*/
                displayTemperature(temperatures.get(temperatures.size() - 1));
            }
        }
    });
}
Also used : Temperature(com.savonia.thesis.db.entity.Temperature) BluetoothManager(android.bluetooth.BluetoothManager) Calendar(java.util.Calendar) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) List(java.util.List) ArrayList(java.util.ArrayList) DateAsXAxisLabelFormatter(com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DateAsXAxisLabelFormatter

use of com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter in project fitness-app by seemoo-lab.

the class InfoArrayAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = null;
    int type = getItemViewType(position);
    // Inflate the layout according to the view type
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (type == InfoListItem.TEXT_VIEW) {
        // Inflate the layout with image
        v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView text = (TextView) v.findViewById(android.R.id.text1);
        InfoListItem infoListItem = itemList.get(position).getItem();
        Information info = (Information) infoListItem.getItem();
        text.setText(Html.fromHtml(info.toString()), TextView.BufferType.SPANNABLE);
    } else {
        v = inflater.inflate(R.layout.listitem_dumpgraph, parent, false);
        InfoListItem infoListItem = itemList.get(position);
        InfoGraphDataPoints dgDataPoints = (InfoGraphDataPoints) infoListItem.getItem();
        DataPoint[] dataPoints = dgDataPoints.getDatapoints();
        GraphView graph = (GraphView) v.findViewById(R.id.graph);
        LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dataPoints);
        series.setDrawDataPoints(true);
        graph.addSeries(series);
        // set date label formatter
        graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(ctx));
        if (dataPoints.length == 5) {
            // max 3 because of the space
            graph.getGridLabelRenderer().setNumHorizontalLabels(3);
        } else if (dataPoints.length > 5) {
            graph.getGridLabelRenderer().setNumHorizontalLabels(2);
        } else {
            graph.getGridLabelRenderer().setNumHorizontalLabels(dataPoints.length);
        }
        // set manual x bounds to have nice steps
        graph.getViewport().setMinX(dataPoints[0].getX());
        graph.getViewport().setMaxX(dataPoints[dataPoints.length - 1].getX());
        graph.getViewport().setXAxisBoundsManual(true);
        // set manual y bounds to have nice steps
        graph.getViewport().setMinY(0);
        graph.getViewport().setYAxisBoundsManual(true);
        // as we use dates as labels, the human rounding to nice readable numbers
        // is not necessary
        graph.getGridLabelRenderer().setHumanRounding(false);
    }
    return v;
}
Also used : Information(seemoo.fitbit.information.Information) TextView(android.widget.TextView) View(android.view.View) GraphView(com.jjoe64.graphview.GraphView) GraphView(com.jjoe64.graphview.GraphView) DataPoint(com.jjoe64.graphview.series.DataPoint) LineGraphSeries(com.jjoe64.graphview.series.LineGraphSeries) DataPoint(com.jjoe64.graphview.series.DataPoint) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) DateAsXAxisLabelFormatter(com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter)

Aggregations

DateAsXAxisLabelFormatter (com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter)2 BluetoothManager (android.bluetooth.BluetoothManager)1 Intent (android.content.Intent)1 Drawable (android.graphics.drawable.Drawable)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 TextView (android.widget.TextView)1 GraphView (com.jjoe64.graphview.GraphView)1 DataPoint (com.jjoe64.graphview.series.DataPoint)1 LineGraphSeries (com.jjoe64.graphview.series.LineGraphSeries)1 Temperature (com.savonia.thesis.db.entity.Temperature)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 List (java.util.List)1 Information (seemoo.fitbit.information.Information)1