From 094d4ebf2a0546ff418b76052a250e72a7ec3a75 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Thu, 9 Mar 2017 11:24:51 +0300 Subject: [PATCH] Added interpolation of Y value when two charts visible --- OsmAnd-java/src/net/osmand/util/MapUtils.java | 16 ++++++++++- .../net/osmand/plus/helpers/GpxUiHelper.java | 28 +++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/util/MapUtils.java b/OsmAnd-java/src/net/osmand/util/MapUtils.java index b71e03f31b..6046e985c7 100644 --- a/OsmAnd-java/src/net/osmand/util/MapUtils.java +++ b/OsmAnd-java/src/net/osmand/util/MapUtils.java @@ -586,7 +586,21 @@ public class MapUtils { return rect; } - + public static float getInterpolatedY(float x1, float y1, float x2, float y2, float x) { + + float a = y1 - y2; + float b = x2 - x1; + + float d = -a * b; + if (d != 0) { + float c1 = y2 * x1 - x2 * y1; + float c2 = x * (y2 - y1); + + return (a * (c1 - c2)) / d; + } else { + return y1; + } + } } diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index ebff0a5bf0..280f39d7f9 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -41,6 +41,7 @@ import com.github.mikephil.charting.components.MarkerView; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.ChartData; +import com.github.mikephil.charting.data.DataSet; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.formatter.IAxisValueFormatter; @@ -79,6 +80,7 @@ import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.render.RenderingRuleProperty; import net.osmand.render.RenderingRulesStorage; import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; import java.io.File; import java.text.DateFormat; @@ -1606,24 +1608,24 @@ public class GpxUiHelper { break; } if (altSetIndex != -1) { - Entry eAlt = chartData.getEntryForHighlight(new Highlight(e.getX(), Float.NaN, altSetIndex)); - ((TextView) textAltView.findViewById(R.id.text_alt_value)).setText(Integer.toString((int) eAlt.getY()) + " "); + float y = getInterpolatedY(altSetIndex == 0 ? dataSet1 : dataSet2, e); + ((TextView) textAltView.findViewById(R.id.text_alt_value)).setText(Integer.toString((int) y) + " "); ((TextView) textAltView.findViewById(R.id.text_alt_units)).setText((altSetIndex == 0 ? dataSet1.units : dataSet2.units)); textAltView.setVisibility(VISIBLE); } else { textAltView.setVisibility(GONE); } if (spdSetIndex != -1) { - Entry eSpd = chartData.getEntryForHighlight(new Highlight(e.getX(), Float.NaN, spdSetIndex)); - ((TextView) textSpdView.findViewById(R.id.text_spd_value)).setText(Integer.toString((int) eSpd.getY()) + " "); + float y = getInterpolatedY(spdSetIndex == 0 ? dataSet1 : dataSet2, e); + ((TextView) textSpdView.findViewById(R.id.text_spd_value)).setText(Integer.toString((int) y) + " "); ((TextView) textSpdView.findViewById(R.id.text_spd_units)).setText(spdSetIndex == 0 ? dataSet1.units : dataSet2.units); textSpdView.setVisibility(VISIBLE); } else { textSpdView.setVisibility(GONE); } if (slpSetIndex != -1) { - Entry eSlp = chartData.getEntryForHighlight(new Highlight(e.getX(), Float.NaN, slpSetIndex)); - ((TextView) textSlpView.findViewById(R.id.text_slp_value)).setText(Integer.toString((int) eSlp.getY()) + " "); + float y = getInterpolatedY(slpSetIndex == 0 ? dataSet1 : dataSet2, e); + ((TextView) textSlpView.findViewById(R.id.text_slp_value)).setText(Integer.toString((int) y) + " "); textSlpView.setVisibility(VISIBLE); } else { textSlpView.setVisibility(GONE); @@ -1638,6 +1640,20 @@ public class GpxUiHelper { super.refreshContent(e, highlight); } + private float getInterpolatedY(OrderedLineDataSet ds, Entry e) { + if (ds.getEntryIndex(e) == -1) { + Entry upEntry = ds.getEntryForXValue(e.getX(), Float.NaN, DataSet.Rounding.UP); + Entry downEntry = upEntry; + int upIndex = ds.getEntryIndex(upEntry); + if (upIndex > 0) { + downEntry = ds.getEntryForIndex(upIndex - 1); + } + return MapUtils.getInterpolatedY(downEntry.getX(), downEntry.getY(), upEntry.getX(), upEntry.getY(), e.getX()); + } else { + return e.getY(); + } + } + @Override public MPPointF getOffset() { if (getChartView().getData().getDataSetCount() == 2) {