From 61aaf7146e50b983831da7e2c1c496a402e2fbf3 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Thu, 16 Feb 2017 10:42:11 +0300 Subject: [PATCH] Fix units issue of chart --- OsmAnd/src/net/osmand/plus/GPXUtilities.java | 103 +++++++++++++++++- .../ShowRouteInfoDialogFragment.java | 8 +- .../plus/myplaces/SelectedGPXFragment.java | 18 +-- .../plus/routing/RouteCalculationResult.java | 2 +- 4 files changed, 106 insertions(+), 25 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index 52c1f48f80..59903e3ea9 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -11,12 +11,15 @@ import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import com.github.mikephil.charting.charts.LineChart; +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.DefaultAxisValueFormatter; +import com.github.mikephil.charting.formatter.IAxisValueFormatter; import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.utils.Utils; @@ -49,6 +52,7 @@ import java.io.StringWriter; import java.io.Writer; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.text.MessageFormat; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -63,6 +67,11 @@ import java.util.Stack; import java.util.TimeZone; import static com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM; +import static net.osmand.plus.OsmAndFormatter.FEET_IN_ONE_METER; +import static net.osmand.plus.OsmAndFormatter.METERS_IN_KILOMETER; +import static net.osmand.plus.OsmAndFormatter.METERS_IN_ONE_MILE; +import static net.osmand.plus.OsmAndFormatter.METERS_IN_ONE_NAUTICALMILE; +import static net.osmand.plus.OsmAndFormatter.YARDS_IN_ONE_METER; public class GPXUtilities { public final static Log log = PlatformUtil.getLog(GPXUtilities.class); @@ -1281,7 +1290,12 @@ public class GPXUtilities { } } - public static void setupGPXChart(LineChart mChart, boolean useFeet, int yLabelsCount) { + public static void setupGPXChart(OsmandApplication ctx, LineChart mChart, int yLabelsCount) { + OsmandSettings settings = ctx.getSettings(); + OsmandSettings.MetricsConstants mc = settings.METRIC_SYSTEM.get(); + boolean useFeet = (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) || (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS); + boolean light = settings.isLightContent(); + //mChart.setHardwareAccelerationEnabled(true); mChart.setTouchEnabled(true); mChart.setDragEnabled(true); @@ -1307,8 +1321,8 @@ public class GPXUtilities { XAxis xAxis = mChart.getXAxis(); xAxis.setDrawAxisLine(false); xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(false); xAxis.setPosition(BOTTOM); + xAxis.setTextColor(light ? mChart.getResources().getColor(R.color.secondary_text_light) : mChart.getResources().getColor(R.color.secondary_text_dark)); YAxis yAxis = mChart.getAxisLeft(); yAxis.enableGridDashedLine(10f, 5f, 0f); @@ -1318,6 +1332,7 @@ public class GPXUtilities { yAxis.setXOffset(16f); yAxis.setYOffset(-6f); yAxis.setLabelCount(yLabelsCount); + yAxis.setTextColor(light ? mChart.getResources().getColor(R.color.secondary_text_light) : mChart.getResources().getColor(R.color.secondary_text_dark)); Legend legend = mChart.getLegend(); legend.setEnabled(false); @@ -1325,9 +1340,87 @@ public class GPXUtilities { mChart.getAxisRight().setEnabled(false); } - public static void setGPXChartData(LineChart mChart, GPXTrackAnalysis analysis, int fillResourceId, boolean useFeet) { + public static void setGPXChartData(OsmandApplication ctx, LineChart mChart, GPXTrackAnalysis analysis, int fillResourceId) { + OsmandSettings settings = ctx.getSettings(); + OsmandSettings.MetricsConstants mc = settings.METRIC_SYSTEM.get(); + boolean useFeet = (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) || (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS); + boolean light = settings.isLightContent(); final float convEle = useFeet ? 3.28084f : 1.0f; - final float divX = analysis.totalDistance > 1000 ? 1000f : 1f; + final float meters = analysis.totalDistance; + float divX; + + String format1 = "{0,number,0.#} "; + String format2 = "{0,number,0.##} "; + String fmt = null; + int mainUnitStr; + float mainUnitInMeters; + if (mc == OsmandSettings.MetricsConstants.KILOMETERS_AND_METERS) { + mainUnitStr = R.string.km; + mainUnitInMeters = METERS_IN_KILOMETER; + } else if (mc == OsmandSettings.MetricsConstants.NAUTICAL_MILES) { + mainUnitStr = R.string.nm; + mainUnitInMeters = METERS_IN_ONE_NAUTICALMILE; + } else { + mainUnitStr = R.string.mile; + mainUnitInMeters = METERS_IN_ONE_MILE; + } + if (meters > 9.99f * mainUnitInMeters) { + fmt = format1; + } + if (meters >= 100 * mainUnitInMeters || + meters > 9.99f * mainUnitInMeters || + meters > 0.999f * mainUnitInMeters || + mc == OsmandSettings.MetricsConstants.MILES_AND_FEET && meters > 0.249f * mainUnitInMeters || + mc == OsmandSettings.MetricsConstants.MILES_AND_METERS && meters > 0.249f * mainUnitInMeters || + mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS && meters > 0.249f * mainUnitInMeters || + mc == OsmandSettings.MetricsConstants.NAUTICAL_MILES && meters > 0.99f * mainUnitInMeters) { + + divX = mainUnitInMeters; + if (fmt == null) { + fmt = format2; + } + } else { + fmt = null; + if (mc == OsmandSettings.MetricsConstants.KILOMETERS_AND_METERS || mc == OsmandSettings.MetricsConstants.MILES_AND_METERS) { + divX = 1f; + mainUnitStr = R.string.m; + } else if (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) { + divX = 1f / FEET_IN_ONE_METER; + mainUnitStr = R.string.foot; + } else if (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS) { + divX = 1f / YARDS_IN_ONE_METER; + mainUnitStr = R.string.yard; + } else { + divX = 1f; + mainUnitStr = R.string.m; + } + } + + final String mainUnitX = ctx.getString(mainUnitStr); + final String formatX = fmt; + + XAxis xAxis = mChart.getXAxis(); + xAxis.setValueFormatter(new IAxisValueFormatter() { + + @Override + public String getFormattedValue(float value, AxisBase axis) { + if (formatX != null) { + return MessageFormat.format(formatX + mainUnitX, value); + } else { + return (int)value + " " + mainUnitX; + } + } + }); + + final String mainUnitY = useFeet ? ctx.getString(R.string.foot) : ctx.getString(R.string.m); + YAxis yAxis = mChart.getAxisLeft(); + yAxis.setValueFormatter(new IAxisValueFormatter() { + + @Override + public String getFormattedValue(float value, AxisBase axis) { + return (int)value + " " + mainUnitY; + } + }); ArrayList values = new ArrayList<>(); List elevationData = analysis.elevationData; @@ -1357,7 +1450,7 @@ public class GPXUtilities { dataSet.setHighlightEnabled(true); dataSet.setDrawVerticalHighlightIndicator(true); dataSet.setDrawHorizontalHighlightIndicator(false); - dataSet.setHighLightColor(Color.BLACK); + dataSet.setHighLightColor(light ? mChart.getResources().getColor(R.color.secondary_text_light) : mChart.getResources().getColor(R.color.secondary_text_dark)); if (Utils.getSDKInt() >= 18) { // fill drawable only supported on api level 18 and above diff --git a/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoDialogFragment.java b/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoDialogFragment.java index 3643a38493..2c8f993ff4 100644 --- a/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoDialogFragment.java @@ -205,14 +205,12 @@ public class ShowRouteInfoDialogFragment extends DialogFragment { private void buildHeader(View headerView) { OsmandApplication app = getMyApplication(); - OsmandSettings.MetricsConstants mc = app.getSettings().METRIC_SYSTEM.get(); - final boolean useFeet = (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) || (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS); LineChart mChart = (LineChart) headerView.findViewById(R.id.chart); - GPXUtilities.setupGPXChart(mChart, useFeet, 4); + GPXUtilities.setupGPXChart(app, mChart, 4); GPXTrackAnalysis analysis = gpx.getAnalysis(0); - GPXUtilities.setGPXChartData(mChart, analysis, Utils.getSDKInt() >= 18 - ? R.drawable.line_chart_fade_blue : R.color.gpx_time_span_color, useFeet); + GPXUtilities.setGPXChartData(app, mChart, analysis, Utils.getSDKInt() >= 18 + ? R.drawable.line_chart_fade_blue : R.color.gpx_time_span_color); ((TextView) headerView.findViewById(R.id.average_text)) .setText(OsmAndFormatter.getFormattedAlt(analysis.avgElevation, app)); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java index 4122f869c2..92f3431fb5 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/SelectedGPXFragment.java @@ -5,15 +5,12 @@ import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.graphics.Color; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.v4.content.ContextCompat; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AlertDialog; import android.text.Html; @@ -34,10 +31,7 @@ import android.widget.TextView; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.components.MarkerView; 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.utils.MPPointF; import com.github.mikephil.charting.utils.Utils; @@ -46,7 +40,6 @@ import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GPXUtilities; -import net.osmand.plus.GPXUtilities.Elevation; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper; @@ -58,7 +51,6 @@ import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; -import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TrackActivity; @@ -84,7 +76,6 @@ public class SelectedGPXFragment extends OsmAndListFragment { protected SelectedGPXAdapter adapter; protected TrackActivity activity; private boolean updateEnable; - private MetricsConstants mc; @Override public void onAttach(Activity activity) { @@ -94,7 +85,6 @@ public class SelectedGPXFragment extends OsmAndListFragment { final Collator collator = Collator.getInstance(); collator.setStrength(Collator.SECONDARY); app = (OsmandApplication) activity.getApplication(); - mc = app.getSettings().METRIC_SYSTEM.get(); } public TrackActivity getMyActivity() { @@ -459,6 +449,7 @@ public class SelectedGPXFragment extends OsmAndListFragment { class SelectedGPXAdapter extends ArrayAdapter { + public SelectedGPXAdapter(List items) { super(getActivity(), R.layout.gpx_item_list_item, items); } @@ -467,13 +458,12 @@ public class SelectedGPXFragment extends OsmAndListFragment { public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; LineChart mChart = null; - final boolean useFeet = (mc == MetricsConstants.MILES_AND_FEET) || (mc == MetricsConstants.MILES_AND_YARDS); if (row == null) { LayoutInflater inflater = getMyActivity().getLayoutInflater(); row = inflater.inflate(R.layout.gpx_item_list_item, parent, false); mChart = (LineChart) row.findViewById(R.id.chart); - GPXUtilities.setupGPXChart(mChart, useFeet, 5); + GPXUtilities.setupGPXChart(app, mChart, 5); } GpxDisplayItem child = getItem(position); TextView label = (TextView) row.findViewById(R.id.name); @@ -517,8 +507,8 @@ public class SelectedGPXFragment extends OsmAndListFragment { if (mChart != null) { if (child.analysis != null && child.analysis.elevationData != null && child.analysis.isElevationSpecified() && (child.analysis.totalDistance > 0)) { - GPXUtilities.setGPXChartData(mChart, child.analysis, Utils.getSDKInt() >= 18 - ? R.drawable.line_chart_fade_orange : R.color.osmand_orange, useFeet); + GPXUtilities.setGPXChartData(app, mChart, child.analysis, Utils.getSDKInt() >= 18 + ? R.drawable.line_chart_fade_orange : R.color.osmand_orange); mChart.setVisibility(View.VISIBLE); } else { diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java b/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java index 6de57cfbed..2bd0b1c8f9 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java @@ -201,7 +201,7 @@ public class RouteCalculationResult { } List list = new ArrayList(); for (int i = cs; i < segments.size(); i++) { - if (i == 0 || segments.get(i - 1) != segments.get(i)) { + if (i == cs || segments.get(i - 1) != segments.get(i)) { list.add(segments.get(i)); } }