Added interpolation of Y value when two charts visible

This commit is contained in:
Alexey Kulish 2017-03-09 11:24:51 +03:00
parent 0e812d08f2
commit 094d4ebf2a
2 changed files with 37 additions and 7 deletions

View file

@ -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;
}
}
}

View file

@ -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) {