Remove zero axis from gpx chart. Cleanup code.

This commit is contained in:
Alexey Kulish 2017-02-12 18:55:08 +03:00
parent 524901f1b5
commit f4bcee4315
3 changed files with 96 additions and 228 deletions

View file

@ -52,6 +52,7 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/description" android:id="@+id/description"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -67,12 +68,5 @@
android:layout_height="150dp" android:layout_height="150dp"
android:layout_gravity="center_vertical"/> android:layout_gravity="center_vertical"/>
<!--
<net.osmand.plus.myplaces.ElevationView
android:id="@+id/elevation"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_gravity="center_vertical" />
-->
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View file

@ -1,115 +0,0 @@
package net.osmand.plus.myplaces;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.R;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings.MetricsConstants;
import java.util.List;
/**
* Created by maw on 02.09.15., original credit goes to gibbsnich
* Developed further by Hardy 2016-11
*/
public class ElevationView extends ImageView {
double maxElevation, minElevation;
float xDistance;
List<GPXUtilities.Elevation> elevationList;
private OsmandApplication app;
private MetricsConstants mc;
public ElevationView(Context ctx, AttributeSet as) {
super(ctx, as);
app = (OsmandApplication) ctx.getApplicationContext();
mc = app.getSettings().METRIC_SYSTEM.get();
}
public void onDraw(Canvas canvas) {
final float screenScale = getResources().getDisplayMetrics().density;
final boolean useFeet = (mc == MetricsConstants.MILES_AND_FEET) || (mc == MetricsConstants.MILES_AND_YARDS);
final String unit = useFeet ? app.getString(R.string.foot) : app.getString(R.string.m);
int stepBase = useFeet ? 200 : 100;
final float convEle = useFeet ? 3.28084f : 1.0f;
final int maxBase = ((int)Math.floor((maxElevation * convEle / stepBase) + 1)) * stepBase;
final int minBase = (int)Math.floor((minElevation * convEle / stepBase)) * stepBase;
final float yDistance = maxBase - minBase;
if ((!useFeet && (yDistance <= 100)) || (useFeet && (yDistance <= 200))) { //Draw 50m/100ft markers for smallest scale
stepBase = stepBase/2;
}
final float xPer = (float)canvas.getWidth() / xDistance;
final float yPer = (float)canvas.getHeight() / yDistance;
final float canvasRight = (float)canvas.getWidth() - 1f;
final float canvasBottom = (float)canvas.getHeight() - 1f;
// This y transform apparently needed to assure top and bottom lines show up on all devices
final float yOffset = 2f;
final float ySlope = ((float)canvas.getHeight() - 2f * yOffset) / (float)canvas.getHeight();
Paint barPaint = new Paint();
barPaint.setColor(getResources().getColor(R.color.dialog_inactive_text_color_dark));
barPaint.setTextSize((int)(16f * screenScale + 0.5f));
barPaint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD));
float yTextLast = 9999f;
for (int i = minBase; i <= maxBase ; i += stepBase) {
float y = yOffset + ySlope * (canvasBottom - yPer * (float)(i - minBase));
canvas.drawLine(0, y, canvasRight, y, barPaint);
if ((yTextLast - y) >= (int)(32f * screenScale + 0.5f)) { // Overlap prevention
canvas.drawText(String.valueOf(i) + " " + unit, (int)(8f * screenScale + 0.5f), y - (int)(2f * screenScale + 0.5f), barPaint);
yTextLast = y;
}
}
float lastX = 0, lastY = 0;
float xDistSum = 0;
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.gpx_altitude_asc));
paint.setStrokeWidth((int)(2f * screenScale + 0.5f));
boolean first = true;
if (elevationList != null) {
for (GPXUtilities.Elevation elevation : elevationList) {
xDistSum += elevation.distance;
float nextX = xPer * xDistSum;
float nextY = yOffset + ySlope * (canvasBottom - yPer * (float)(elevation.elevation * convEle - minBase));
if (first) {
first = false;
} else {
canvas.drawLine(lastX, lastY, nextX, nextY, paint);
}
lastX = nextX;
lastY = nextY;
}
}
}
public void setElevationData(List<GPXUtilities.Elevation> elevationData) {
elevationList = elevationData;
}
public void setMaxElevation(double max) {
maxElevation = max;
}
public void setMinElevation(double min) {
minElevation = min;
}
public void setTotalDistance(float dist) {
xDistance = dist;
}
}

View file

@ -471,9 +471,46 @@ public class SelectedGPXFragment extends OsmAndListFragment {
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView; View row = convertView;
LineChart mChart = null;
final boolean useFeet = (mc == MetricsConstants.MILES_AND_FEET) || (mc == MetricsConstants.MILES_AND_YARDS);
if (row == null) { if (row == null) {
LayoutInflater inflater = getMyActivity().getLayoutInflater(); LayoutInflater inflater = getMyActivity().getLayoutInflater();
row = inflater.inflate(R.layout.gpx_item_list_item, parent, false); row = inflater.inflate(R.layout.gpx_item_list_item, parent, false);
mChart = (LineChart) row.findViewById(R.id.chart);
//mChart.setHardwareAccelerationEnabled(true);
mChart.setTouchEnabled(true);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setPinchZoom(true);
mChart.setScaleYEnabled(false);
mChart.setAutoScaleMinMaxEnabled(true);
mChart.setDrawBorders(false);
mChart.getDescription().setEnabled(false);
mChart.setMaxVisibleValueCount(10);
// create a custom MarkerView (extend MarkerView) and specify the layout
// to use for it
MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.chart_marker_view, useFeet);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
mChart.setDrawMarkers(true);
XAxis xAxis = mChart.getXAxis();
xAxis.setDrawAxisLine(false);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setPosition(BOTTOM);
YAxis yAxis = mChart.getAxisLeft();
yAxis.enableGridDashedLine(10f, 5f, 0f);
yAxis.setGridColor(ActivityCompat.getColor(getActivity(), R.color.divider_color));
yAxis.setDrawAxisLine(false);
Legend legend = mChart.getLegend();
legend.setEnabled(false);
mChart.getAxisRight().setEnabled(false);
} }
GpxDisplayItem child = getItem(position); GpxDisplayItem child = getItem(position);
TextView label = (TextView) row.findViewById(R.id.name); TextView label = (TextView) row.findViewById(R.id.name);
@ -514,116 +551,68 @@ public class SelectedGPXFragment extends OsmAndListFragment {
description.setVisibility(View.GONE); description.setVisibility(View.GONE);
} }
final LineChart mChart = (LineChart) row.findViewById(R.id.chart); if (mChart != null) {
//mChart.setHardwareAccelerationEnabled(true); if (child.analysis != null && child.analysis.elevationData != null && child.analysis.isElevationSpecified() && (child.analysis.totalDistance > 0)) {
mChart.setTouchEnabled(true);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setPinchZoom(true);
mChart.setScaleYEnabled(false);
mChart.setAutoScaleMinMaxEnabled(true);
mChart.setDrawBorders(false);
mChart.getDescription().setEnabled(false);
mChart.setMaxVisibleValueCount(10);
final boolean useFeet = (mc == MetricsConstants.MILES_AND_FEET) || (mc == MetricsConstants.MILES_AND_YARDS); if (child.analysis.minElevation >= 0) {
// create a custom MarkerView (extend MarkerView) and specify the layout //mChart.getAxisLeft().setAxisMinimum(0f);
// to use for it
MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.chart_marker_view, useFeet);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
mChart.setDrawMarkers(true);
XAxis xAxis = mChart.getXAxis();
xAxis.setDrawAxisLine(false);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setPosition(BOTTOM);
YAxis yAxis = mChart.getAxisLeft();
yAxis.enableGridDashedLine(10f, 5f, 0f);
yAxis.setGridColor(ActivityCompat.getColor(getActivity(), R.color.divider_color));
yAxis.setDrawAxisLine(false);
Legend legend = mChart.getLegend();
legend.setEnabled(false);
mChart.getAxisRight().setEnabled(false);
if (child.analysis != null && child.analysis.elevationData != null && child.analysis.isElevationSpecified() && (child.analysis.totalDistance > 0)) {
if (child.analysis.minElevation >= 0) {
yAxis.setAxisMinimum(0f);
}
final float convEle = useFeet ? 3.28084f : 1.0f;
final float divX = child.analysis.totalDistance > 1000 ? 1000f : 1f;
ArrayList<Entry> values = new ArrayList<>();
List<Elevation> elevationData = child.analysis.elevationData;
float nextX = 0;
float nextY;
for (Elevation e : elevationData) {
if (e.distance > 0) {
nextX += (float) e.distance / divX;
nextY = (float) (e.elevation * convEle);
values.add(new Entry(nextX, nextY));
} }
final float convEle = useFeet ? 3.28084f : 1.0f;
final float divX = child.analysis.totalDistance > 1000 ? 1000f : 1f;
ArrayList<Entry> values = new ArrayList<>();
List<Elevation> elevationData = child.analysis.elevationData;
float nextX = 0;
float nextY;
for (Elevation e : elevationData) {
if (e.distance > 0) {
nextX += (float) e.distance / divX;
nextY = (float) (e.elevation * convEle);
values.add(new Entry(nextX, nextY));
}
}
LineDataSet dataSet = new LineDataSet(values, "");
dataSet.setColor(Color.BLACK);
dataSet.setDrawValues(false);
dataSet.setLineWidth(0f);
dataSet.setValueTextSize(9f);
dataSet.setDrawFilled(true);
dataSet.setFormLineWidth(1f);
dataSet.setFormSize(15.f);
dataSet.setDrawCircles(false);
dataSet.setDrawCircleHole(false);
dataSet.setHighlightEnabled(true);
dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false);
dataSet.setHighLightColor(Color.BLACK);
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_chart_fade_orange);
dataSet.setFillDrawable(drawable);
} else {
dataSet.setFillColor(ContextCompat.getColor(getActivity(), R.color.osmand_orange));
}
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(dataSet); // add the datasets
// create a data object with the datasets
LineData data = new LineData(dataSets);
// set data
mChart.setData(data);
mChart.setVisibility(View.VISIBLE);
} else {
mChart.setVisibility(View.GONE);
} }
LineDataSet dataSet = new LineDataSet(values, "");
dataSet.setColor(Color.BLACK);
dataSet.setDrawValues(false);
//set1.setCircleColor(Color.BLACK);
dataSet.setLineWidth(0f);
dataSet.setValueTextSize(9f);
dataSet.setDrawFilled(true);
dataSet.setFormLineWidth(1f);
dataSet.setFormSize(15.f);
dataSet.setDrawCircles(false);
dataSet.setDrawCircleHole(false);
dataSet.setHighlightEnabled(true); // allow highlighting for DataSet
// set this to false to disable the drawing of highlight indicator (lines)
dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false);
dataSet.setHighLightColor(Color.BLACK); // color for highlight indicator
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_chart_fade_orange);
dataSet.setFillDrawable(drawable);
}
else {
dataSet.setFillColor(ContextCompat.getColor(getActivity(), R.color.osmand_orange));
}
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(dataSet); // add the datasets
// create a data object with the datasets
LineData data = new LineData(dataSets);
// set data
mChart.setData(data);
mChart.setVisibility(View.VISIBLE);
} else {
mChart.setVisibility(View.GONE);
} }
/*
ElevationView elevationImg = (ElevationView) row.findViewById(R.id.elevation);
if (child.analysis != null && child.analysis.elevationData != null && child.analysis.isElevationSpecified() && (child.analysis.totalDistance > 0)) {
elevationImg.setElevationData(child.analysis.elevationData);
elevationImg.setMaxElevation(child.analysis.maxElevation);
elevationImg.setMinElevation(child.analysis.minElevation);
elevationImg.setTotalDistance(child.analysis.totalDistance); //Use raw data for graph, not channel detection noise filter (facilitates visual double check)
elevationImg.setVisibility(View.VISIBLE);
} else {
elevationImg.setVisibility(View.GONE);
}
*/
return row; return row;
} }