Switch to MPAndroidChart library for home activity charts

This commit is contained in:
Tanguy Herbron 2018-03-15 16:22:31 +01:00
parent 2422618400
commit da984671f8
2 changed files with 48 additions and 52 deletions

View File

@ -14,6 +14,10 @@ import com.db.chart.model.ChartSet;
import com.db.chart.model.LineSet; import com.db.chart.model.LineSet;
import com.db.chart.renderer.AxisRenderer; import com.db.chart.renderer.AxisRenderer;
import com.db.chart.view.LineChartView; import com.db.chart.view.LineChartView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.nauk.coinfolio.Activities.CurrencyDetailsActivity; import com.nauk.coinfolio.Activities.CurrencyDetailsActivity;
import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency;
import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart;
@ -21,6 +25,7 @@ import com.nauk.coinfolio.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -90,17 +95,24 @@ public class HomeLayoutGenerator {
if(currency.getHistoryMinutes() != null) if(currency.getHistoryMinutes() != null)
{ {
List<Double> borders = getAxisBorders(currency); List<Double> borders = getAxisBorders(currency);
LineChartView chartView = view.findViewById(R.id.LineChartView); LineChart lineChart = view.findViewById(R.id.LineChartView);
chartView.setAxisBorderValues(borders.get(0).floatValue(), borders.get(1).floatValue()) lineChart.setDrawGridBackground(false);
.setYLabels(AxisRenderer.LabelPosition.NONE) lineChart.setDrawBorders(false);
.setYAxis(false) lineChart.setDrawMarkers(false);
.setXAxis(false) lineChart.setDoubleTapToZoomEnabled(false);
.setVisibility(View.VISIBLE); lineChart.setPinchZoom(false);
lineChart.setScaleEnabled(false);
lineChart.setDragEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.getAxisLeft().setEnabled(false);
lineChart.getAxisRight().setEnabled(false);
lineChart.getLegend().setEnabled(false);
lineChart.getXAxis().setEnabled(false);
lineChart.setViewPortOffsets(0, 0, 0, 0);
lineChart.setData(generateData(currency));
chartView.addData(generateChartSet(currency)); lineChart.setOnClickListener(new View.OnClickListener() {
chartView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class); Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class);
@ -142,7 +154,7 @@ public class HomeLayoutGenerator {
if(currency.getHistoryMinutes() != null) if(currency.getHistoryMinutes() != null)
{ {
view.findViewById(R.id.LineChartView).setVisibility(View.VISIBLE); view.findViewById(R.id.LineChartView).setVisibility(View.VISIBLE);
((LineChartView) view.findViewById(R.id.LineChartView)).show(); ((LineChart) view.findViewById(R.id.LineChartView)).invalidate();
view.findViewById(R.id.errorTextView).setVisibility(View.GONE); view.findViewById(R.id.errorTextView).setVisibility(View.GONE);
} }
else else
@ -197,52 +209,30 @@ public class HomeLayoutGenerator {
} }
} }
private ChartSet generateChartSet(Currency currency) private LineData generateData(Currency currency)
{ {
LineDataSet dataSet;
List<CurrencyDataChart> dataChartList = currency.getHistoryMinutes(); List<CurrencyDataChart> dataChartList = currency.getHistoryMinutes();
LineSet lineSet = new LineSet(); ArrayList<Entry> values = new ArrayList<>();
int counter = 0;
Calendar calendar = Calendar.getInstance(Locale.FRANCE);
String hour;
String minute;
for(int i = 0; i < dataChartList.size(); i+=10) for(int i = 0; i < dataChartList.size(); i+=10)
{ {
if(counter == 30) values.add(new Entry(i, (float) dataChartList.get(i).getOpen()));
{
calendar.setTimeInMillis(dataChartList.get(i).getTimestamp()*1000);
hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
minute = String.valueOf(calendar.get(Calendar.MINUTE));
if(hour.length() < 2)
{
hour = "0" + hour;
} }
if(minute.length() < 2) dataSet = new LineDataSet(values, "History");
{ dataSet.setDrawIcons(false);
minute = "0" + minute; dataSet.setColor(currency.getChartColor());
} dataSet.setLineWidth(1);
dataSet.setDrawFilled(true);
dataSet.setFillColor(getColorWithAplha(currency.getChartColor(), 0.5f));
dataSet.setFormLineWidth(1);
dataSet.setFormSize(15);
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
dataSet.setHighlightEnabled(false);
lineSet.addPoint(hour + ":" + minute, (float) dataChartList.get(i).getOpen()); return new LineData(dataSet);
counter = 0;
}
else
{
counter++;
lineSet.addPoint("", (float) dataChartList.get(i).getOpen());
}
}
lineSet.setSmooth(true);
lineSet.setThickness(4);
lineSet.setFill(getColorWithAplha(currency.getChartColor(), 0.5f));
lineSet.setColor(currency.getChartColor());
return lineSet;
} }
private int getColorWithAplha(int color, float ratio) private int getColorWithAplha(int color, float ratio)
@ -262,11 +252,11 @@ public class HomeLayoutGenerator {
if(abs(number) > 1) if(abs(number) > 1)
{ {
str = String.format("%.2f", number); str = String.format( Locale.UK, "%.2f", number);
} }
else else
{ {
str = String.format("%.4f", number); str = String.format( Locale.UK, "%.4f", number);
} }
return str; return str;

View File

@ -150,10 +150,16 @@
android:focusable="true" android:focusable="true"
android:visibility="gone"> android:visibility="gone">
<com.db.chart.view.LineChartView <!--<com.db.chart.view.LineChartView
android:id="@+id/LineChartView" android:id="@+id/LineChartView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/cardViewChartSize" android:layout_height="@dimen/cardViewChartSize"
android:visibility="gone" />-->
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/LineChartView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:visibility="gone"/> android:visibility="gone"/>
<TextView <TextView