Fix units issue of chart

This commit is contained in:
Alexey Kulish 2017-02-16 10:42:11 +03:00
parent 8e2c26591a
commit 61aaf7146e
4 changed files with 106 additions and 25 deletions

View file

@ -11,12 +11,15 @@ import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import com.github.mikephil.charting.charts.LineChart; 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.Legend;
import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet; 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.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.Utils;
@ -49,6 +52,7 @@ import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.MessageFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -63,6 +67,11 @@ import java.util.Stack;
import java.util.TimeZone; import java.util.TimeZone;
import static com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM; 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 class GPXUtilities {
public final static Log log = PlatformUtil.getLog(GPXUtilities.class); 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.setHardwareAccelerationEnabled(true);
mChart.setTouchEnabled(true); mChart.setTouchEnabled(true);
mChart.setDragEnabled(true); mChart.setDragEnabled(true);
@ -1307,8 +1321,8 @@ public class GPXUtilities {
XAxis xAxis = mChart.getXAxis(); XAxis xAxis = mChart.getXAxis();
xAxis.setDrawAxisLine(false); xAxis.setDrawAxisLine(false);
xAxis.setDrawGridLines(false); xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setPosition(BOTTOM); 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 yAxis = mChart.getAxisLeft();
yAxis.enableGridDashedLine(10f, 5f, 0f); yAxis.enableGridDashedLine(10f, 5f, 0f);
@ -1318,6 +1332,7 @@ public class GPXUtilities {
yAxis.setXOffset(16f); yAxis.setXOffset(16f);
yAxis.setYOffset(-6f); yAxis.setYOffset(-6f);
yAxis.setLabelCount(yLabelsCount); 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 legend = mChart.getLegend();
legend.setEnabled(false); legend.setEnabled(false);
@ -1325,9 +1340,87 @@ public class GPXUtilities {
mChart.getAxisRight().setEnabled(false); 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 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<Entry> values = new ArrayList<>(); ArrayList<Entry> values = new ArrayList<>();
List<Elevation> elevationData = analysis.elevationData; List<Elevation> elevationData = analysis.elevationData;
@ -1357,7 +1450,7 @@ public class GPXUtilities {
dataSet.setHighlightEnabled(true); dataSet.setHighlightEnabled(true);
dataSet.setDrawVerticalHighlightIndicator(true); dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false); 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) { if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above // fill drawable only supported on api level 18 and above

View file

@ -205,14 +205,12 @@ public class ShowRouteInfoDialogFragment extends DialogFragment {
private void buildHeader(View headerView) { private void buildHeader(View headerView) {
OsmandApplication app = getMyApplication(); 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); LineChart mChart = (LineChart) headerView.findViewById(R.id.chart);
GPXUtilities.setupGPXChart(mChart, useFeet, 4); GPXUtilities.setupGPXChart(app, mChart, 4);
GPXTrackAnalysis analysis = gpx.getAnalysis(0); GPXTrackAnalysis analysis = gpx.getAnalysis(0);
GPXUtilities.setGPXChartData(mChart, analysis, Utils.getSDKInt() >= 18 GPXUtilities.setGPXChartData(app, mChart, analysis, Utils.getSDKInt() >= 18
? R.drawable.line_chart_fade_blue : R.color.gpx_time_span_color, useFeet); ? R.drawable.line_chart_fade_blue : R.color.gpx_time_span_color);
((TextView) headerView.findViewById(R.id.average_text)) ((TextView) headerView.findViewById(R.id.average_text))
.setText(OsmAndFormatter.getFormattedAlt(analysis.avgElevation, app)); .setText(OsmAndFormatter.getFormattedAlt(analysis.avgElevation, app));

View file

@ -5,15 +5,12 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.Html; import android.text.Html;
@ -34,10 +31,7 @@ import android.widget.TextView;
import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.MarkerView; import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry; 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.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.MPPointF; import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.Utils;
@ -46,7 +40,6 @@ import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.Elevation;
import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper;
@ -58,7 +51,6 @@ import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.MetricsConstants;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.activities.TrackActivity;
@ -84,7 +76,6 @@ public class SelectedGPXFragment extends OsmAndListFragment {
protected SelectedGPXAdapter adapter; protected SelectedGPXAdapter adapter;
protected TrackActivity activity; protected TrackActivity activity;
private boolean updateEnable; private boolean updateEnable;
private MetricsConstants mc;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
@ -94,7 +85,6 @@ public class SelectedGPXFragment extends OsmAndListFragment {
final Collator collator = Collator.getInstance(); final Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY); collator.setStrength(Collator.SECONDARY);
app = (OsmandApplication) activity.getApplication(); app = (OsmandApplication) activity.getApplication();
mc = app.getSettings().METRIC_SYSTEM.get();
} }
public TrackActivity getMyActivity() { public TrackActivity getMyActivity() {
@ -459,6 +449,7 @@ public class SelectedGPXFragment extends OsmAndListFragment {
class SelectedGPXAdapter extends ArrayAdapter<GpxDisplayItem> { class SelectedGPXAdapter extends ArrayAdapter<GpxDisplayItem> {
public SelectedGPXAdapter(List<GpxDisplayItem> items) { public SelectedGPXAdapter(List<GpxDisplayItem> items) {
super(getActivity(), R.layout.gpx_item_list_item, 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) { public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView; View row = convertView;
LineChart mChart = null; 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 = (LineChart) row.findViewById(R.id.chart);
GPXUtilities.setupGPXChart(mChart, useFeet, 5); GPXUtilities.setupGPXChart(app, mChart, 5);
} }
GpxDisplayItem child = getItem(position); GpxDisplayItem child = getItem(position);
TextView label = (TextView) row.findViewById(R.id.name); TextView label = (TextView) row.findViewById(R.id.name);
@ -517,8 +507,8 @@ public class SelectedGPXFragment extends OsmAndListFragment {
if (mChart != null) { if (mChart != null) {
if (child.analysis != null && child.analysis.elevationData != null && child.analysis.isElevationSpecified() && (child.analysis.totalDistance > 0)) { if (child.analysis != null && child.analysis.elevationData != null && child.analysis.isElevationSpecified() && (child.analysis.totalDistance > 0)) {
GPXUtilities.setGPXChartData(mChart, child.analysis, Utils.getSDKInt() >= 18 GPXUtilities.setGPXChartData(app, mChart, child.analysis, Utils.getSDKInt() >= 18
? R.drawable.line_chart_fade_orange : R.color.osmand_orange, useFeet); ? R.drawable.line_chart_fade_orange : R.color.osmand_orange);
mChart.setVisibility(View.VISIBLE); mChart.setVisibility(View.VISIBLE);
} else { } else {

View file

@ -201,7 +201,7 @@ public class RouteCalculationResult {
} }
List<RouteSegmentResult> list = new ArrayList<RouteSegmentResult>(); List<RouteSegmentResult> list = new ArrayList<RouteSegmentResult>();
for (int i = cs; i < segments.size(); i++) { 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)); list.add(segments.get(i));
} }
} }