From fd02b44dd833e2e71ca7e76501e81c6cdc6e0aed Mon Sep 17 00:00:00 2001 From: crimean Date: Sat, 13 Apr 2019 15:10:47 +0300 Subject: [PATCH] Select legend title on chart click - route details --- .../net/osmand/plus/helpers/GpxUiHelper.java | 1 + .../RouteDetailsFragment.java | 10 +-- .../cards/RouteInfoCard.java | 69 ++++++++++++++----- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index d00134410a..04bc14c9bd 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -1300,6 +1300,7 @@ public class GpxUiHelper { entries.add(new BarEntry(0, stacks)); BarDataSet barDataSet = new BarDataSet(entries, ""); barDataSet.setColors(colors); + barDataSet.setHighLightColor(ContextCompat.getColor(app, R.color.color_white)); BarData dataSet = new BarData(barDataSet); dataSet.setDrawValues(false); dataSet.setBarWidth(1); diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java index ac6052ad96..b64a63924e 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java @@ -317,19 +317,19 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT GPXUtilities.GPXTrackAnalysis analysis = gpx.getAnalysis(0); RouteInfoCard routeClassCard = new RouteInfoCard(mapActivity, routeStatistics.getRouteClassStatistic(), analysis); - createRouteCard(cardsContainer, routeClassCard); + addRouteCard(cardsContainer, routeClassCard); RouteInfoCard routeSurfaceCard = new RouteInfoCard(mapActivity, routeStatistics.getRouteSurfaceStatistic(), analysis); - createRouteCard(cardsContainer, routeSurfaceCard); + addRouteCard(cardsContainer, routeSurfaceCard); if (slopeDataSet != null) { List inclines = createInclinesAndAdd100MetersWith0Incline(slopeDataSet.getValues()); RouteInfoCard routeSteepnessCard = new RouteInfoCard(mapActivity, routeStatistics.getRouteSteepnessStatistic(inclines), analysis); - createRouteCard(cardsContainer, routeSteepnessCard); + addRouteCard(cardsContainer, routeSteepnessCard); } RouteInfoCard routeSmoothnessCard = new RouteInfoCard(mapActivity, routeStatistics.getRouteSmoothnessStatistic(), analysis); - createRouteCard(cardsContainer, routeSmoothnessCard); + addRouteCard(cardsContainer, routeSmoothnessCard); } } } @@ -347,7 +347,7 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT buildRowDivider(cardsContainer, false); } - private void createRouteCard(LinearLayout cardsContainer, RouteInfoCard routeInfoCard) { + private void addRouteCard(LinearLayout cardsContainer, RouteInfoCard routeInfoCard) { OsmandApplication app = requireMyApplication(); menuCards.add(routeInfoCard); routeInfoCard.setListener(this); diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteInfoCard.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteInfoCard.java index 5eb47e79aa..0348bcd956 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteInfoCard.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/RouteInfoCard.java @@ -16,8 +16,11 @@ import android.widget.TextView; import com.github.mikephil.charting.charts.HorizontalBarChart; import com.github.mikephil.charting.data.BarData; +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.highlight.Highlight; +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -25,7 +28,6 @@ import net.osmand.plus.activities.SettingsNavigationActivity; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.router.RouteStatistics.Boundaries; import net.osmand.router.RouteStatistics.RouteSegmentAttribute; -import net.osmand.router.RouteStatistics.StatisticType; import net.osmand.router.RouteStatistics.Statistics; import net.osmand.util.Algorithms; @@ -42,11 +44,12 @@ public class RouteInfoCard extends BaseCard { private static final int MINIMUM_CONTRAST_RATIO = 3; private Statistics routeStatistics; - private GPXUtilities.GPXTrackAnalysis analysis; + private GPXTrackAnalysis analysis; + private String selectedPropertyName; private boolean showLegend; - public RouteInfoCard(MapActivity mapActivity, Statistics routeStatistics, GPXUtilities.GPXTrackAnalysis analysis) { + public RouteInfoCard(MapActivity mapActivity, Statistics routeStatistics, GPXTrackAnalysis analysis) { super(mapActivity); this.routeStatistics = routeStatistics; this.analysis = analysis; @@ -59,12 +62,41 @@ public class RouteInfoCard extends BaseCard { @Override protected void updateContent() { + updateContent(routeStatistics); + } + + private void updateContent(final Statistics routeStatistics) { updateHeader(); final HorizontalBarChart chart = (HorizontalBarChart) view.findViewById(R.id.chart); GpxUiHelper.setupHorizontalGPXChart(app, chart, 5, 9, 24, true, nightMode); BarData barData = GpxUiHelper.buildStatisticChart(app, chart, routeStatistics, analysis, true, nightMode); chart.setData(barData); - final LinearLayout container = (LinearLayout) view.findViewById(R.id.route_items); + chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { + @Override + public void onValueSelected(Entry e, Highlight h) { + List> elems = routeStatistics.getElements(); + int i = h.getStackIndex(); + if (elems.size() > i) { + selectedPropertyName = elems.get(i).getPropertyName(); + LinearLayout container = (LinearLayout) view.findViewById(R.id.route_items); + if (!showLegend) { + showLegend = true; + } + container.removeAllViews(); + attachLegend(container, routeStatistics); + setLayoutNeeded(); + } + } + + @Override + public void onNothingSelected() { + LinearLayout container = (LinearLayout) view.findViewById(R.id.route_items); + showLegend = false; + container.removeAllViews(); + setLayoutNeeded(); + } + }); + LinearLayout container = (LinearLayout) view.findViewById(R.id.route_items); container.removeAllViews(); if (showLegend) { attachLegend(container, routeStatistics); @@ -92,16 +124,17 @@ public class RouteInfoCard extends BaseCard { } private String getInfoType() { - if (routeStatistics.getStatisticType() == StatisticType.CLASS) { - return app.getString(R.string.road_types); - } else if (routeStatistics.getStatisticType() == StatisticType.STEEPNESS) { - return app.getString(R.string.route_steepness_stat_container); - } else if (routeStatistics.getStatisticType() == StatisticType.SMOOTHNESS) { - return app.getString(R.string.route_smoothness_stat_container); - } else if (routeStatistics.getStatisticType() == StatisticType.SURFACE) { - return app.getString(R.string.route_surface_stat_container); - } else { - return ""; + switch (routeStatistics.getStatisticType()) { + case CLASS: + return app.getString(R.string.road_types); + case STEEPNESS: + return app.getString(R.string.route_steepness_stat_container); + case SMOOTHNESS: + return app.getString(R.string.route_smoothness_stat_container); + case SURFACE: + return app.getString(R.string.route_surface_stat_container); + default: + return ""; } } @@ -124,7 +157,7 @@ public class RouteInfoCard extends BaseCard { } String propertyName = segment.getPropertyName(); String name = SettingsNavigationActivity.getStringPropertyName(app, propertyName, propertyName.replaceAll("_", " ")); - Spannable text = getSpanLegend(name, segment); + Spannable text = getSpanLegend(name, segment, segment.getPropertyName().equals(selectedPropertyName)); TextView legend = (TextView) view.findViewById(R.id.legend_text); legend.setText(text); @@ -156,12 +189,12 @@ public class RouteInfoCard extends BaseCard { }); } - private Spannable getSpanLegend(String title, RouteSegmentAttribute segment) { + private Spannable getSpanLegend(String title, RouteSegmentAttribute segment, boolean selected) { String formattedDistance = OsmAndFormatter.getFormattedDistance(segment.getDistance(), getMyApplication()); title = Algorithms.capitalizeFirstLetter(title); SpannableStringBuilder spannable = new SpannableStringBuilder(title); spannable.append(": "); - int startIndex = spannable.length(); + int startIndex = selected ? -0 : spannable.length(); spannable.append(formattedDistance); spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);