use of net.osmand.plus.GPXUtilities.Speed in project Osmand by osmandapp.
the class GpxUiHelper method createGPXSpeedDataSet.
public static OrderedLineDataSet createGPXSpeedDataSet(OsmandApplication ctx, LineChart mChart, GPXTrackAnalysis analysis, GPXDataSetAxisType axisType, boolean useRightAxis, boolean drawFilled) {
OsmandSettings settings = ctx.getSettings();
boolean light = settings.isLightContent();
float divX;
XAxis xAxis = mChart.getXAxis();
if (axisType == GPXDataSetAxisType.TIME && analysis.isTimeSpecified()) {
divX = setupXAxisTime(xAxis, analysis.timeSpan);
} else {
divX = setupXAxisDistance(ctx, xAxis, analysis.totalDistance);
}
OsmandSettings.SpeedConstants sps = settings.SPEED_SYSTEM.get();
float mulSpeed = Float.NaN;
float divSpeed = Float.NaN;
final String mainUnitY = sps.toShortString(ctx);
if (sps == OsmandSettings.SpeedConstants.KILOMETERS_PER_HOUR) {
mulSpeed = 3.6f;
} else if (sps == OsmandSettings.SpeedConstants.MILES_PER_HOUR) {
mulSpeed = 3.6f * METERS_IN_KILOMETER / METERS_IN_ONE_MILE;
} else if (sps == OsmandSettings.SpeedConstants.NAUTICALMILES_PER_HOUR) {
mulSpeed = 3.6f * METERS_IN_KILOMETER / METERS_IN_ONE_NAUTICALMILE;
} else if (sps == OsmandSettings.SpeedConstants.MINUTES_PER_KILOMETER) {
divSpeed = METERS_IN_KILOMETER / 60;
} else if (sps == OsmandSettings.SpeedConstants.MINUTES_PER_MILE) {
divSpeed = METERS_IN_ONE_MILE / 60;
} else {
mulSpeed = 1f;
}
YAxis yAxis;
if (useRightAxis) {
yAxis = mChart.getAxisRight();
yAxis.setEnabled(true);
} else {
yAxis = mChart.getAxisLeft();
}
if (analysis.hasSpeedInTrack()) {
yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange_label));
yAxis.setGridColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange_grid));
} else {
yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_red_label));
yAxis.setGridColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_red_grid));
}
yAxis.setAxisMinimum(0f);
ArrayList<Entry> values = new ArrayList<>();
List<Speed> speedData = analysis.speedData;
float nextX = 0;
float nextY;
float x;
for (Speed s : speedData) {
x = axisType == GPXDataSetAxisType.TIME ? s.time : s.distance;
if (x > 0) {
if (axisType == GPXDataSetAxisType.TIME && x > 60) {
values.add(new Entry(nextX + 1, 0));
values.add(new Entry(nextX + x - 1, 0));
}
nextX += x / divX;
if (Float.isNaN(divSpeed)) {
nextY = s.speed * mulSpeed;
} else {
nextY = divSpeed / s.speed;
}
if (nextY < 0 || Float.isInfinite(nextY)) {
nextY = 0;
}
values.add(new Entry(nextX, nextY));
}
}
OrderedLineDataSet dataSet = new OrderedLineDataSet(values, "", GPXDataSetType.SPEED, axisType);
String format = null;
if (dataSet.getYMax() < 3) {
format = "{0,number,0.#} ";
}
final String formatY = format;
yAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
if (!Algorithms.isEmpty(formatY)) {
return MessageFormat.format(formatY + mainUnitY, value);
} else {
return (int) value + " " + mainUnitY;
}
}
});
if (Float.isNaN(divSpeed)) {
dataSet.priority = analysis.avgSpeed * mulSpeed;
} else {
dataSet.priority = divSpeed / analysis.avgSpeed;
}
dataSet.divX = divX;
if (Float.isNaN(divSpeed)) {
dataSet.mulY = mulSpeed;
dataSet.divY = Float.NaN;
} else {
dataSet.divY = divSpeed;
dataSet.mulY = Float.NaN;
}
dataSet.units = mainUnitY;
if (analysis.hasSpeedInTrack()) {
dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange));
} else {
dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_red));
}
dataSet.setLineWidth(1f);
if (drawFilled) {
dataSet.setFillAlpha(128);
if (analysis.hasSpeedInTrack()) {
dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange));
} else {
dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_red));
}
dataSet.setDrawFilled(true);
} else {
dataSet.setDrawFilled(false);
}
dataSet.setDrawValues(false);
dataSet.setValueTextSize(9f);
dataSet.setFormLineWidth(1f);
dataSet.setFormSize(15.f);
dataSet.setDrawCircles(false);
dataSet.setDrawCircleHole(false);
dataSet.setHighlightEnabled(true);
dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false);
dataSet.setHighLightColor(light ? mChart.getResources().getColor(R.color.secondary_text_light) : mChart.getResources().getColor(R.color.secondary_text_dark));
if (useRightAxis) {
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
}
return dataSet;
}
Aggregations