Skip to content

Commit

Permalink
Graph implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBogueRat committed Mar 29, 2017
1 parent 45d2d9f commit e1db420
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 17 deletions.
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ dependencies {
compile('com.crashlytics.sdk.android:crashlytics:2.6.6@aar') {
transitive = true;
}
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support:design:25.3.0'
// Adding Glide dependency to test a new Image Gallery
compile 'com.github.bumptech.glide:glide:3.7.0'
// Required for segmented control
compile 'info.hoang8f:android-segmented:1.0.6'
// Adding Charting for DCP
compile 'com.github.PhilJay:MPAndroidChart:v3.0.0'
// Adding support for Realm listview and gridview adapters
// compile 'eu.davidea:flexible-adapter:5.0.0-rc1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'info.hoang8f:android-segmented:1.0.6'
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
compile 'io.realm:android-adapters:1.3.0'
compile 'com.github.thorbenprimke:realm-recyclerview:0.9.25'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
// compile 'eu.davidea:flexible-adapter:5.0.0-rc1'
}
14 changes: 12 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@
android:label="@string/title_activity_points"
android:parentActivityName=".DCP.Projects"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"
android:theme="@style/CustomActionBarTheme.NoActionBar">
android:theme="@style/CustomActionBarTheme.NoActionBar"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.bogueratcreations.eaftoolkit.DCP.Projects" />
Expand All @@ -218,6 +218,16 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.bogueratcreations.eaftoolkit.DCP.Points" />
</activity>
<activity
android:name=".DCP.PointsChart"
android:label="@string/title_activity_points_chart"
android:parentActivityName=".DCP.Points"
android:screenOrientation="portrait"
android:theme="@style/CustomActionBarTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.bogueratcreations.eaftoolkit.DCP.Points" />
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bogueratcreations.eaftoolkit.DCP.model.Point;
Expand All @@ -39,7 +35,6 @@
import java.util.Date;

import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;

public class Points extends AppCompatActivity {
Expand Down Expand Up @@ -191,9 +186,10 @@ public void onClick(View view) {
realm.commitTransaction();
revertView();
} else {
// TODO: Future implementation of GRAPH function
Snackbar.make(view, "Graphing this set of points will be implemented once all other functionality is implemented.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
// Pass the selected Point and navigate to Reading.class
Intent intent = new Intent(view.getContext(), PointsChart.class);
intent.putExtra("projId", passedProjectID);
startActivityForResult(intent, 1);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package com.bogueratcreations.eaftoolkit.DCP;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bogueratcreations.eaftoolkit.DCP.model.Reading;
import com.bogueratcreations.eaftoolkit.R;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;

import io.realm.Realm;

import com.bogueratcreations.eaftoolkit.DCP.model.Point;
import com.bogueratcreations.eaftoolkit.DCP.model.Project;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class PointsChart extends AppCompatActivity {

long passedProjectID;

private Realm realm;
Project passedProject;

LineChart chart;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_points_chart);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

passedProjectID = getIntent().getLongExtra("projId",-1);
realm = Realm.getDefaultInstance();
passedProject = realm.where(Project.class)
.equalTo("id",passedProjectID)
.findFirst();

// Customize the Chart
chart = (LineChart) findViewById(R.id.chartProject);
chart.setKeepPositionOnRotation(true);
chart.animateY(1000, Easing.EasingOption.EaseInOutQuad);
chart.setHighlightPerTapEnabled(false);
chart.setHighlightPerDragEnabled(false);
chart.setNoDataText("No data to display.");
chart.getDescription().setText(passedProject.getProjName());
Legend legend = chart.getLegend();
legend.setWordWrapEnabled(true);
XAxis xAxis = chart.getXAxis();
xAxis.setAxisMinimum(1f);
xAxis.setAxisMaximum(101f);
xAxis.setLabelCount(8);
xAxis.setDrawLabels(true);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
DecimalFormat mFormat;
mFormat = new DecimalFormat("##0.#"); // use one decimal
if (value <= 0) {
return "0";
} else {
return mFormat.format(unScaleCbr(value));
}
}
});
YAxis yAxis = chart.getAxisLeft();
yAxis.setInverted(true);
yAxis.setAxisMaximum(1020f);
yAxis.setAxisMinimum(0f);
YAxis yAxis1 = chart.getAxisRight();
yAxis1.setInverted(true);
yAxis1.setAxisMaximum(1020f);
yAxis1.setAxisMinimum(0f);
yAxis1.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
DecimalFormat mFormat;
mFormat = new DecimalFormat("##0"); // use one decimal
return mFormat.format(value/25.4);
}
});

// Retrieve applicable readings from the realm...Now to use them in chart.
Long[] pointIds = new Long[passedProject.getPoints().size()];
int counter = 0;
// TODO: Get me some better colors!
int[] colors = new int[]{
Color.RED,
Color.BLUE,
Color.GREEN
};
// use the interface ILineDataSet
String seriesTitle[] = new String[passedProject.getPoints().size()];
List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();

for (Point p : passedProject.getPoints()) {
// Reinitialize the series for each point.
List<Entry> series = new ArrayList<Entry>();
String title = p.getPointNum();
for (Reading r : p.getReadings().sort("readingNum")) {
// Store x,y values for this series.
double cbrMax = r.getCbr();
if (cbrMax > 100.0) {cbrMax = 100.0;}
Log.d("EAF", "r.getCbr(): " + cbrMax + " scaleCbr(cbrMax: " + scaleCbr(cbrMax));
Entry entry = new Entry( scaleCbr(cbrMax),(float)(r.getTotalDepth()));
series.add(entry);
}
// Find out if there are any readings and force an empty one to prevent crash.
if (p.getReadings().size() > 0) {
LineDataSet seriesDataSet = new LineDataSet(series, title);
seriesDataSet.setColor(colors[counter]);
seriesDataSet.setCircleColor(colors[counter]);
dataSets.add(seriesDataSet);
if (counter == 2) {
counter = 0;
} else {
counter++;
}
}
}

LineData data = new LineData(dataSets);
data.setDrawValues(false);
chart.setData(data);
chart.invalidate();

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fileName = passedProject.getProjName() + "_" + (int)(Math.random() * 100);
chart.saveToGallery(fileName, 100);
Toast.makeText(getBaseContext(), "Saved this graph to your Gallery", Toast.LENGTH_LONG).show();
}
});
// Will crash if I do, removing for consistency...
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public float scaleCbr(double cbr) {

return (float)(Math.log(cbr) / Math.log(100)) * 100;
}
public float unScaleCbr(double cbr) {
double calcVal = Math.pow(10,(cbr/50));
return (float)(calcVal);
}

@Override
public void onBackPressed() {
// TODO: Update the lowestCBR for Point here or in onItemSwipedDismiss
Intent intent = new Intent();
intent.putExtra("projId", passedProjectID);
setResult(RESULT_OK, intent);
finish();
}

@Override
protected void onDestroy() {
super.onDestroy();
if(realm != null) {
realm.close();
realm = null;
}
}
}
34 changes: 34 additions & 0 deletions app/src/main/res/layout/activity_points_chart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.bogueratcreations.eaftoolkit.DCP.PointsChart">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/CustomActionBarTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/BRCgreen"
app:popupTheme="@style/CustomActionBarTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_points_chart" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_gallery" />

</android.support.design.widget.CoordinatorLayout>
53 changes: 53 additions & 0 deletions app/src/main/res/layout/content_points_chart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bogueratcreations.eaftoolkit.DCP.PointsChart"
tools:showIn="@layout/activity_points_chart">

<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chartProject"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/tvYlabelL"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvXlabel"
app:layout_constraintVertical_bias="0.0"
android:layout_width="312dp"
android:layout_height="473dp" />

<TextView
android:id="@+id/tvXlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="CBR"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />

<TextView
android:id="@+id/tvYlabelL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="D\nE\nP\nT\nH\n\nmm"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
<string name="title_activity_readings">Readings</string>
<string name="title_activity_points_add">PointsAdd</string>
<string name="select_create_project">Select / Create Project</string>
<string name="title_activity_points_chart">Project CBRs Graph</string>
</resources>

0 comments on commit e1db420

Please sign in to comment.