From 9ff13165c6a119f9efeb40ac42086f1698de76d7 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 2 Apr 2018 23:46:00 +0200 Subject: [PATCH] Added Volume charts in detail activity --- .../Activities/CurrencyDetailsActivity.java | 171 +++++++++++++----- .../coinfolio/Activities/HomeActivity.java | 12 +- .../CurrencyData/CurrencyDataChart.java | 15 +- .../CurrencyData/CurrencyDataRetriever.java | 4 +- .../main/res/color/home_color_bottombar.xml | 2 +- .../main/res/drawable/gradient_background.xml | 8 +- .../res/layout/activity_currency_details.xml | 12 +- .../res/layout/activity_currency_summary.xml | 3 +- .../res/layout/content_currency_summary.xml | 3 +- app/src/main/res/values/colors.xml | 4 +- app/src/main/res/values/dimens.xml | 2 + app/src/main/res/values/styles.xml | 20 +- 12 files changed, 185 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java index 6dede6a..1bf561b 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java @@ -14,7 +14,6 @@ import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.util.Log; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.MotionEvent; @@ -25,14 +24,16 @@ import android.widget.TextView; import android.widget.ViewFlipper; import com.daimajia.swipe.SwipeLayout; -import com.db.chart.model.LineSet; import com.db.chart.tooltip.Tooltip; +import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.LineChart; +import com.github.mikephil.charting.data.BarData; +import com.github.mikephil.charting.data.BarDataSet; +import com.github.mikephil.charting.data.BarEntry; 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.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.listener.OnChartValueSelectedListener; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; @@ -128,17 +129,18 @@ public class CurrencyDetailsActivity extends AppCompatActivity { if(currency.getHistoryMinutes().size() > 0) { - drawChart(DAY, 1); + drawPriceChart(DAY, 1); + drawVolumeChart(DAY, 1); } else { - TextView errorTextView = new TextView(this); + /*TextView errorTextView = new TextView(this); errorTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 750)); errorTextView.setText("Not enough data"); errorTextView.setTag("chart_layout"); errorTextView.setGravity(Gravity.CENTER); - chartLayout.addView(errorTextView, 0); + chartLayout.addView(errorTextView, 0);*/ } setTitle(" " + currency.getName()); @@ -182,6 +184,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { private void buttonEvent(View v) { v.setEnabled(false); + v.setElevation(convertDpToPx(8)); LinearLayout buttonLayout = (LinearLayout) v.getParent(); @@ -192,15 +195,22 @@ public class CurrencyDetailsActivity extends AppCompatActivity { if(button != v) { button.setEnabled(true); + button.setElevation(convertDpToPx(2)); } } chartEvent((Button) v); } + private float convertDpToPx(float dp) + { + return dp * this.getResources().getDisplayMetrics().density; + } + private void chartEvent(Button button) { - findViewById(R.id.chartView).setVisibility(View.GONE); + findViewById(R.id.chartPriceView).setVisibility(View.GONE); + findViewById(R.id.chartVolumeView).setVisibility(View.GONE); findViewById(R.id.progressLayoutChart).setVisibility(View.VISIBLE); String interval = button.getText().toString().substring(button.getText().toString().length()-2); @@ -208,13 +218,16 @@ public class CurrencyDetailsActivity extends AppCompatActivity { switch (interval) { case "1h": - drawChart(HOUR, 1); + drawPriceChart(HOUR, 1); + drawVolumeChart(HOUR, 1); break; case "3h": - drawChart(HOUR, 3); + drawPriceChart(HOUR, 3); + drawVolumeChart(HOUR, 3); break; case "1d": - drawChart(DAY, 1); + drawPriceChart(DAY, 1); + drawVolumeChart(DAY, 1); break; case "3d": currency.updateHistoryHours(this, new Currency.CurrencyCallBack() { @@ -223,7 +236,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawChart(CurrencyDetailsActivity.DAY, 3); + drawPriceChart(CurrencyDetailsActivity.DAY, 3); + drawVolumeChart(CurrencyDetailsActivity.DAY, 3); } }); } @@ -236,7 +250,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawChart(CurrencyDetailsActivity.WEEK, 1); + drawPriceChart(CurrencyDetailsActivity.WEEK, 1); + drawVolumeChart(CurrencyDetailsActivity.WEEK, 1); } }); } @@ -249,7 +264,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawChart(CurrencyDetailsActivity.MONTH, 1); + drawPriceChart(CurrencyDetailsActivity.MONTH, 1); + drawVolumeChart(CurrencyDetailsActivity.MONTH, 1); } }); } @@ -262,7 +278,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawChart(CurrencyDetailsActivity.MONTH, 3); + drawPriceChart(CurrencyDetailsActivity.MONTH, 3); + drawVolumeChart(CurrencyDetailsActivity.MONTH, 3); } }); } @@ -275,7 +292,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawChart(CurrencyDetailsActivity.MONTH, 6); + drawPriceChart(CurrencyDetailsActivity.MONTH, 6); + drawVolumeChart(CurrencyDetailsActivity.MONTH, 6); } }); } @@ -288,7 +306,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawChart(CurrencyDetailsActivity.YEAR, 1); + drawPriceChart(CurrencyDetailsActivity.YEAR, 1); + drawVolumeChart(CurrencyDetailsActivity.YEAR, 1); } }); } @@ -297,9 +316,34 @@ public class CurrencyDetailsActivity extends AppCompatActivity { } } - private void drawChart(int timeUnit, int amout) + private void drawVolumeChart(int timeUnit, int amout) { - final LineChart lineChart = findViewById(R.id.chartView); + final BarChart barChart = findViewById(R.id.chartVolumeView); + + barChart.setDrawGridBackground(false); + barChart.setDrawBorders(false); + barChart.setDrawMarkers(true); + barChart.setDoubleTapToZoomEnabled(true); + barChart.setPinchZoom(true); + barChart.setScaleEnabled(false); + barChart.setDragEnabled(true); + barChart.getDescription().setEnabled(false); + barChart.getAxisLeft().setEnabled(false); + barChart.getAxisRight().setEnabled(false); + barChart.getLegend().setEnabled(false); + barChart.getXAxis().setEnabled(false); + barChart.setViewPortOffsets(0, 0, 0, 0); + barChart.setFitBars(true); + + barChart.setData(generateVolumeChartSet(timeUnit, amout)); + barChart.invalidate(); + + findViewById(R.id.chartVolumeView).setVisibility(View.VISIBLE); + } + + private void drawPriceChart(int timeUnit, int amout) + { + final LineChart lineChart = findViewById(R.id.chartPriceView); lineChart.setDrawGridBackground(false); lineChart.setDrawBorders(false); @@ -315,7 +359,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { lineChart.getXAxis().setEnabled(false); lineChart.setViewPortOffsets(0, 0, 0, 0); - lineChart.setData(generateChartSet(timeUnit, amout)); + lineChart.setData(generatePriceChartSet(timeUnit, amout)); lineChart.getAxisLeft().setAxisMinValue(lineChart.getData().getYMin()); lineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { @@ -346,11 +390,73 @@ public class CurrencyDetailsActivity extends AppCompatActivity { updateFluctuation(lineChart.getData().getDataSets().get(0).getEntryForIndex(0).getY(), lineChart.getData().getDataSets().get(0).getEntryForIndex(lineChart.getData().getDataSets().get(0).getEntryCount() - 1).getY()); - findViewById(R.id.chartView).setVisibility(View.VISIBLE); + findViewById(R.id.chartPriceView).setVisibility(View.VISIBLE); findViewById(R.id.progressLayoutChart).setVisibility(View.GONE); } - private LineData generateChartSet(int timeUnit, int amount) + private BarData generateVolumeChartSet(int timeUnit, int amount) + { + BarDataSet dataSet; + List dataChartList = new ArrayList<>(); + ArrayList values = new ArrayList<>(); + + switch (timeUnit) + { + case HOUR: + dataChartList = currency.getHistoryMinutes().subList(currency.getHistoryMinutes().size()-(60*amount), currency.getHistoryMinutes().size()); + break; + case DAY: + if(amount == 1) + { + dataChartList = currency.getHistoryMinutes(); + } + else + { + dataChartList = currency.getHistoryHours().subList(currency.getHistoryHours().size()-(24*amount), currency.getHistoryHours().size()); + } + break; + case WEEK: + dataChartList = currency.getHistoryHours().subList(currency.getHistoryHours().size()-168, currency.getHistoryHours().size()); + break; + case MONTH: + switch (amount) + { + case 1: + dataChartList = currency.getHistoryHours(); + break; + case 3: + dataChartList = currency.getHistoryDays().subList(currency.getHistoryDays().size()-93, currency.getHistoryDays().size()); + break; + case 6: + dataChartList = currency.getHistoryDays().subList(currency.getHistoryDays().size()-186, currency.getHistoryDays().size()); + break; + } + break; + case YEAR: + dataChartList = currency.getHistoryDays(); + break; + } + + int offset = (int) Math.floor(dataChartList.size() / 50); + + int loopNumber = 0; + for(int i = 0; i < dataChartList.size(); i += offset) + { + values.add(new BarEntry(loopNumber, (float) dataChartList.get(i).getVolumeTo())); + loopNumber++; + } + + dataSet = new BarDataSet(values, "Volume"); + dataSet.setDrawIcons(false); + dataSet.setColor(Color.GRAY); + dataSet.setDrawValues(false); + dataSet.setHighlightEnabled(true); + dataSet.setHighLightColor(currency.getChartColor()); + + return new BarData(dataSet); + } + + private LineData generatePriceChartSet(int timeUnit, int amount) { LineDataSet dataSet; List dataChartList = new ArrayList<>(); @@ -583,28 +689,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { ((TextView) findViewById(R.id.txtViewPercentage)).setText(percentageFluctuation + "%"); } - private void updateFluctuation(LineData lineData) - { - ILineDataSet dataSet = lineData.getDataSets().get(0); - - float fluctuation = dataSet.getEntryForIndex(dataSet.getEntryCount() - 1).getY() - dataSet.getEntryForIndex(0).getY(); - float percentageFluctuation = (float) (fluctuation / dataSet.getEntryForIndex(0).getY() * 100); - - if(percentageFluctuation < 0) - { - ((TextView) findViewById(R.id.txtViewPercentage)).setTextColor(getResources().getColor(R.color.red)); - } - else - { - ((TextView) findViewById(R.id.txtViewPercentage)).setTextColor(getResources().getColor(R.color.green)); - } - - ((TextView) findViewById(R.id.txtViewPriceStart)).setText("$" + dataSet.getEntryForIndex(0).getY()); - ((TextView) findViewById(R.id.txtViewPriceNow)).setText("$" + dataSet.getEntryForIndex(dataSet.getEntryCount() - 1).getY()); - ((TextView) findViewById(R.id.txtViewPercentage)).setText(percentageFluctuation + "%"); - } - - /*private LineSet generateChartSet(int timeUnit, int amount) + /*private LineSet generatePriceChartSet(int timeUnit, int amount) { List dataChartList = new ArrayList<>(); LineSet lineSet = new LineSet(); diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java index 8de9607..53f604b 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; +import android.graphics.Outline; import android.graphics.Typeface; import android.os.AsyncTask; import android.os.Bundle; @@ -34,6 +35,7 @@ import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.view.ViewOutlineProvider; import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; @@ -139,9 +141,11 @@ public class HomeActivity extends AppCompatActivity { /**Interface setup**/ //Setup main interface - requestWindowFeature(Window.FEATURE_NO_TITLE); - getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); + //requestWindowFeature(Window.FEATURE_NO_TITLE); + //getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); + getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + setContentView(R.layout.activity_currency_summary); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); @@ -293,7 +297,7 @@ public class HomeActivity extends AppCompatActivity { spaceNavigationView.addSpaceItem(new SpaceItem("Market Cap.", R.drawable.ic_pie_chart_black_24dp)); spaceNavigationView.setSpaceBackgroundColor(getResources().getColor(R.color.colorPrimary)); spaceNavigationView.setCentreButtonIcon(R.drawable.ic_view_list_white_24dp); - spaceNavigationView.setCentreButtonColor(getResources().getColor(R.color.colorAccent)); + spaceNavigationView.setCentreButtonColor(getResources().getColor(R.color.colorPrimary)); spaceNavigationView.setCentreButtonIconColorFilterEnabled(false); spaceNavigationView.changeCurrentItem(-1); @@ -390,7 +394,7 @@ public class HomeActivity extends AppCompatActivity { } }); - ((LinearLayout) findViewById(R.id.linearLayoutWatchlist)).addView(view); + //((LinearLayout) findViewById(R.id.linearLayoutWatchlist)).addView(view); Intent intent = getIntent(); diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataChart.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataChart.java index 31cc654..8fea2be 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataChart.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataChart.java @@ -14,14 +14,18 @@ public class CurrencyDataChart implements Parcelable { double high; double low; double open; + double volumeFrom; + double volumeTo; - public CurrencyDataChart(long timestamp, double close, double high, double low, double open) + public CurrencyDataChart(long timestamp, double close, double high, double low, double open, double volumeFrom, double volumeTo) { this.timestamp = timestamp; this.close = close; this.high = high; this.low = low; this.open = open; + this.volumeFrom = volumeFrom; + this.volumeTo = volumeTo; } public double getOpen() @@ -34,6 +38,11 @@ public class CurrencyDataChart implements Parcelable { return close; } + public double getVolumeTo() + { + return volumeTo; + } + public long getTimestamp() { return timestamp; @@ -51,6 +60,8 @@ public class CurrencyDataChart implements Parcelable { dest.writeDouble(this.high); dest.writeDouble(this.low); dest.writeDouble(this.open); + dest.writeDouble(this.volumeFrom); + dest.writeDouble(this.volumeTo); } protected CurrencyDataChart(Parcel in) { @@ -59,6 +70,8 @@ public class CurrencyDataChart implements Parcelable { this.high = in.readDouble(); this.low = in.readDouble(); this.open = in.readDouble(); + this.volumeFrom = in.readDouble(); + this.volumeTo = in.readDouble(); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java index b7e59ab..95a3aee 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java @@ -104,8 +104,10 @@ public class CurrencyDataRetriever { double high = Double.parseDouble(jsonObject.getString("high")); double low = Double.parseDouble(jsonObject.getString("low")); double open = Double.parseDouble(jsonObject.getString("open")); + double volumeFrom = Double.parseDouble(jsonObject.getString("volumefrom")); + double volumeTo = Double.parseDouble(jsonObject.getString("volumeto")); - dataChart.add(new CurrencyDataChart(timestamp, close, high, low, open)); + dataChart.add(new CurrencyDataChart(timestamp, close, high, low, open, volumeFrom, volumeTo)); } catch (JSONException e) { Log.d(context.getResources().getString(R.string.debug_volley), "API Request error: " + e + " index: " + i); diff --git a/app/src/main/res/color/home_color_bottombar.xml b/app/src/main/res/color/home_color_bottombar.xml index b8cae12..5fafac5 100644 --- a/app/src/main/res/color/home_color_bottombar.xml +++ b/app/src/main/res/color/home_color_bottombar.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/gradient_background.xml b/app/src/main/res/drawable/gradient_background.xml index 2dd82b0..24074e2 100644 --- a/app/src/main/res/drawable/gradient_background.xml +++ b/app/src/main/res/drawable/gradient_background.xml @@ -1,9 +1,7 @@ + android:startColor="@color/colorPrimary" + android:endColor="@color/colorPrimaryDark" + android:angle="0"/> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 7d3fa03..92892ff 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -21,7 +21,7 @@ @@ -37,11 +37,17 @@ + + + android:elevation="10dp"> + android:layout_marginBottom="50dp"> - #000046 - #111124 + #004e92 + #000428 #1CB5E0 #FF000000 #FF222222 diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index b1805b4..5d3f91c 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -15,4 +15,6 @@ 60dp 125dp 250dp + + 1dp diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 610e24d..b3d370f 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,25 +1,28 @@ -