Search in sources :

Example 1 with OnChartGestureListener

use of com.github.mikephil.charting.listener.OnChartGestureListener in project CryptoBuddy by Patchett.

the class GraphFragment method setUpChart.

public void setUpChart() {
    XAxis xAxis = lineChart.getXAxis();
    xAxis.setDrawAxisLine(true);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
    xAxis.setAvoidFirstLastClipping(true);
    lineChart.getAxisLeft().setEnabled(true);
    lineChart.getAxisLeft().setDrawGridLines(false);
    lineChart.getXAxis().setDrawGridLines(false);
    lineChart.getAxisRight().setEnabled(false);
    lineChart.getLegend().setEnabled(false);
    lineChart.setDoubleTapToZoomEnabled(false);
    lineChart.setScaleEnabled(false);
    lineChart.getDescription().setEnabled(false);
    lineChart.setContentDescription("");
    lineChart.setNoDataText(getString(R.string.noChartDataString));
    lineChart.setNoDataTextColor(R.color.darkRed);
    lineChart.setOnChartValueSelectedListener(this);
    lineChart.setOnChartGestureListener(new OnChartGestureListener() {

        @Override
        public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
            YAxis yAxis = lineChart.getAxisLeft();
            // Allow scrolling in the right and left margins
            if (me.getX() > yAxis.getLongestLabel().length() * yAxis.getTextSize() && me.getX() < displayWidth - lineChart.getViewPortHandler().offsetRight()) {
                viewPager.setPagingEnabled(false);
                nestedScrollView.setScrollingEnabled(false);
            }
        }

        @Override
        public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
            viewPager.setPagingEnabled(true);
            nestedScrollView.setScrollingEnabled(true);
        }

        @Override
        public void onChartLongPressed(MotionEvent me) {
        }

        @Override
        public void onChartDoubleTapped(MotionEvent me) {
        }

        @Override
        public void onChartSingleTapped(MotionEvent me) {
        }

        @Override
        public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
        }

        @Override
        public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
        }

        @Override
        public void onChartTranslate(MotionEvent me, float dX, float dY) {
        }
    });
}
Also used : OnChartGestureListener(com.github.mikephil.charting.listener.OnChartGestureListener) ChartTouchListener(com.github.mikephil.charting.listener.ChartTouchListener) XAxis(com.github.mikephil.charting.components.XAxis) MotionEvent(android.view.MotionEvent) YAxis(com.github.mikephil.charting.components.YAxis)

Example 2 with OnChartGestureListener

use of com.github.mikephil.charting.listener.OnChartGestureListener in project Osmand by osmandapp.

the class TrackDetailsMenu method updateView.

private void updateView(final View parentView) {
    GPXTrackAnalysis analysis = gpxItem.analysis;
    if (analysis == null || gpxItem.chartTypes == null) {
        parentView.setVisibility(View.GONE);
        if (analysis != null && analysis.isBoundsCalculated()) {
            mapActivity.getMapView().fitRectToMap(analysis.left, analysis.right, analysis.top, analysis.bottom, 0, 0, 0);
        }
        return;
    }
    final LineChart chart = (LineChart) parentView.findViewById(R.id.chart);
    chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

        @Override
        public void onValueSelected(Entry e, Highlight h) {
            refreshChart(chart, false);
        }

        @Override
        public void onNothingSelected() {
        }
    });
    chart.setOnChartGestureListener(new OnChartGestureListener() {

        boolean hasTranslated = false;

        float highlightDrawX = -1;

        @Override
        public void onChartGestureStart(MotionEvent me, ChartGesture lastPerformedGesture) {
            hasTranslated = false;
            if (chart.getHighlighted() != null && chart.getHighlighted().length > 0) {
                highlightDrawX = chart.getHighlighted()[0].getDrawX();
            } else {
                highlightDrawX = -1;
            }
        }

        @Override
        public void onChartGestureEnd(MotionEvent me, ChartGesture lastPerformedGesture) {
            if ((lastPerformedGesture == ChartGesture.DRAG && hasTranslated) || lastPerformedGesture == ChartGesture.X_ZOOM || lastPerformedGesture == ChartGesture.Y_ZOOM || lastPerformedGesture == ChartGesture.PINCH_ZOOM || lastPerformedGesture == ChartGesture.DOUBLE_TAP || lastPerformedGesture == ChartGesture.ROTATE) {
                gpxItem.chartMatrix = new Matrix(chart.getViewPortHandler().getMatrixTouch());
                refreshChart(chart, true);
            }
        }

        @Override
        public void onChartLongPressed(MotionEvent me) {
        }

        @Override
        public void onChartDoubleTapped(MotionEvent me) {
        }

        @Override
        public void onChartSingleTapped(MotionEvent me) {
        }

        @Override
        public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
        }

        @Override
        public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
        }

        @Override
        public void onChartTranslate(MotionEvent me, float dX, float dY) {
            hasTranslated = true;
            if (highlightDrawX != -1) {
                Highlight h = chart.getHighlightByTouchPoint(highlightDrawX, 0f);
                if (h != null) {
                    chart.highlightValue(h);
                    refreshChart(chart, false);
                }
            }
        }
    });
    final OsmandApplication app = mapActivity.getMyApplication();
    final IconsCache ic = app.getIconsCache();
    GpxUiHelper.setupGPXChart(app, chart, 4);
    List<ILineDataSet> dataSets = new ArrayList<>();
    if (gpxItem.chartTypes != null && gpxItem.chartTypes.length > 0) {
        for (int i = 0; i < gpxItem.chartTypes.length; i++) {
            OrderedLineDataSet dataSet = null;
            switch(gpxItem.chartTypes[i]) {
                case ALTITUDE:
                    dataSet = GpxUiHelper.createGPXElevationDataSet(app, chart, analysis, gpxItem.chartAxisType, false, true);
                    break;
                case SPEED:
                    dataSet = GpxUiHelper.createGPXSpeedDataSet(app, chart, analysis, gpxItem.chartAxisType, gpxItem.chartTypes.length > 1, true);
                    break;
                case SLOPE:
                    dataSet = GpxUiHelper.createGPXSlopeDataSet(app, chart, analysis, gpxItem.chartAxisType, null, gpxItem.chartTypes.length > 1, true);
                    break;
            }
            if (dataSet != null) {
                dataSets.add(dataSet);
            }
        }
    }
    Collections.sort(dataSets, new Comparator<ILineDataSet>() {

        @Override
        public int compare(ILineDataSet ds1, ILineDataSet ds2) {
            OrderedLineDataSet dataSet1 = (OrderedLineDataSet) ds1;
            OrderedLineDataSet dataSet2 = (OrderedLineDataSet) ds2;
            return dataSet1.getPriority() > dataSet2.getPriority() ? -1 : (dataSet1.getPriority() == dataSet2.getPriority() ? 0 : 1);
        }
    });
    chart.setData(new LineData(dataSets));
    updateChart(chart);
    View yAxis = parentView.findViewById(R.id.y_axis);
    ImageView yAxisIcon = (ImageView) parentView.findViewById(R.id.y_axis_icon);
    TextView yAxisTitle = (TextView) parentView.findViewById(R.id.y_axis_title);
    View yAxisArrow = parentView.findViewById(R.id.y_axis_arrow);
    final List<GPXDataSetType[]> availableTypes = new ArrayList<>();
    boolean hasSlopeChart = false;
    if (analysis.hasElevationData) {
        availableTypes.add(new GPXDataSetType[] { GPXDataSetType.ALTITUDE });
        if (gpxItem.chartAxisType != GPXDataSetAxisType.TIME) {
            availableTypes.add(new GPXDataSetType[] { GPXDataSetType.SLOPE });
        }
    }
    if (analysis.hasSpeedData) {
        availableTypes.add(new GPXDataSetType[] { GPXDataSetType.SPEED });
    }
    if (analysis.hasElevationData && gpxItem.chartAxisType != GPXDataSetAxisType.TIME) {
        availableTypes.add(new GPXDataSetType[] { GPXDataSetType.ALTITUDE, GPXDataSetType.SLOPE });
    }
    if (analysis.hasElevationData && analysis.hasSpeedData) {
        availableTypes.add(new GPXDataSetType[] { GPXDataSetType.ALTITUDE, GPXDataSetType.SPEED });
    }
    for (GPXDataSetType t : gpxItem.chartTypes) {
        if (t == GPXDataSetType.SLOPE) {
            hasSlopeChart = true;
            break;
        }
    }
    yAxisIcon.setImageDrawable(GPXDataSetType.getImageDrawable(app, gpxItem.chartTypes));
    yAxisTitle.setText(GPXDataSetType.getName(app, gpxItem.chartTypes));
    if (availableTypes.size() > 0) {
        yAxis.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final PopupMenu optionsMenu = new PopupMenu(mapActivity, v);
                DirectionsDialogs.setupPopUpMenuIcon(optionsMenu);
                for (final GPXDataSetType[] types : availableTypes) {
                    MenuItem menuItem = optionsMenu.getMenu().add(GPXDataSetType.getName(app, types)).setIcon(GPXDataSetType.getImageDrawable(app, types));
                    menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem mItem) {
                            gpxItem.chartTypes = types;
                            update();
                            return true;
                        }
                    });
                }
                optionsMenu.show();
            }
        });
        yAxisArrow.setVisibility(View.VISIBLE);
    } else {
        yAxis.setOnClickListener(null);
        yAxis.setBackgroundResource(0);
        yAxisArrow.setVisibility(View.GONE);
    }
    View xAxis = parentView.findViewById(R.id.x_axis);
    ImageView xAxisIcon = (ImageView) parentView.findViewById(R.id.x_axis_icon);
    TextView xAxisTitle = (TextView) parentView.findViewById(R.id.x_axis_title);
    View xAxisArrow = parentView.findViewById(R.id.x_axis_arrow);
    if (gpxItem.chartAxisType == GPXDataSetAxisType.TIME) {
        xAxisIcon.setImageDrawable(ic.getThemedIcon(R.drawable.ic_action_time));
        xAxisTitle.setText(app.getString(R.string.shared_string_time));
    } else {
        xAxisIcon.setImageDrawable(ic.getThemedIcon(R.drawable.ic_action_marker_dark));
        xAxisTitle.setText(app.getString(R.string.distance));
    }
    if (analysis.isTimeSpecified() && !hasSlopeChart) {
        xAxis.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final PopupMenu optionsMenu = new PopupMenu(mapActivity, v);
                DirectionsDialogs.setupPopUpMenuIcon(optionsMenu);
                final GPXDataSetAxisType type;
                if (gpxItem.chartAxisType == GPXDataSetAxisType.TIME) {
                    type = GPXDataSetAxisType.DISTANCE;
                } else {
                    type = GPXDataSetAxisType.TIME;
                }
                MenuItem menuItem = optionsMenu.getMenu().add(type.getStringId()).setIcon(type.getImageDrawable(app));
                menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem mItem) {
                        gpxItem.chartAxisType = type;
                        gpxItem.chartHighlightPos = -1;
                        gpxItem.chartMatrix = null;
                        update();
                        return true;
                    }
                });
                optionsMenu.show();
            }
        });
        xAxisArrow.setVisibility(View.VISIBLE);
    } else {
        xAxis.setOnClickListener(null);
        xAxis.setBackgroundResource(0);
        xAxisArrow.setVisibility(View.GONE);
    }
    refreshChart(chart, true);
}
Also used : OnChartValueSelectedListener(com.github.mikephil.charting.listener.OnChartValueSelectedListener) Highlight(com.github.mikephil.charting.highlight.Highlight) OsmandApplication(net.osmand.plus.OsmandApplication) ArrayList(java.util.ArrayList) ChartGesture(com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture) Entry(com.github.mikephil.charting.data.Entry) Matrix(android.graphics.Matrix) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) GPXDataSetType(net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType) TextView(android.widget.TextView) ImageView(android.widget.ImageView) OnChartGestureListener(com.github.mikephil.charting.listener.OnChartGestureListener) GPXTrackAnalysis(net.osmand.plus.GPXUtilities.GPXTrackAnalysis) MenuItem(android.view.MenuItem) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) MotionEvent(android.view.MotionEvent) LineData(com.github.mikephil.charting.data.LineData) GPXDataSetAxisType(net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType) OrderedLineDataSet(net.osmand.plus.helpers.GpxUiHelper.OrderedLineDataSet) IconsCache(net.osmand.plus.IconsCache) LineChart(com.github.mikephil.charting.charts.LineChart) PopupMenu(android.support.v7.widget.PopupMenu)

Example 3 with OnChartGestureListener

use of com.github.mikephil.charting.listener.OnChartGestureListener in project Osmand by osmandapp.

the class ShowRouteInfoDialogFragment method buildHeader.

private void buildHeader(View headerView) {
    OsmandApplication app = getMyApplication();
    final LineChart mChart = (LineChart) headerView.findViewById(R.id.chart);
    GpxUiHelper.setupGPXChart(app, mChart, 4);
    mChart.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            listView.requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });
    GPXTrackAnalysis analysis = gpx.getAnalysis(0);
    if (analysis.hasElevationData) {
        List<ILineDataSet> dataSets = new ArrayList<>();
        elevationDataSet = GpxUiHelper.createGPXElevationDataSet(app, mChart, analysis, GPXDataSetAxisType.DISTANCE, false, true);
        if (elevationDataSet != null) {
            dataSets.add(elevationDataSet);
        }
        slopeDataSet = GpxUiHelper.createGPXSlopeDataSet(app, mChart, analysis, GPXDataSetAxisType.DISTANCE, elevationDataSet.getValues(), true, true);
        if (slopeDataSet != null) {
            dataSets.add(slopeDataSet);
        }
        LineData data = new LineData(dataSets);
        mChart.setData(data);
        mChart.setOnChartGestureListener(new OnChartGestureListener() {

            float highlightDrawX = -1;

            @Override
            public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
                if (mChart.getHighlighted() != null && mChart.getHighlighted().length > 0) {
                    highlightDrawX = mChart.getHighlighted()[0].getDrawX();
                } else {
                    highlightDrawX = -1;
                }
            }

            @Override
            public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
                gpxItem.chartMatrix = new Matrix(mChart.getViewPortHandler().getMatrixTouch());
                Highlight[] highlights = mChart.getHighlighted();
                if (highlights != null && highlights.length > 0) {
                    gpxItem.chartHighlightPos = highlights[0].getX();
                } else {
                    gpxItem.chartHighlightPos = -1;
                }
            }

            @Override
            public void onChartLongPressed(MotionEvent me) {
            }

            @Override
            public void onChartDoubleTapped(MotionEvent me) {
            }

            @Override
            public void onChartSingleTapped(MotionEvent me) {
            }

            @Override
            public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
            }

            @Override
            public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
            }

            @Override
            public void onChartTranslate(MotionEvent me, float dX, float dY) {
                if (highlightDrawX != -1) {
                    Highlight h = mChart.getHighlightByTouchPoint(highlightDrawX, 0f);
                    if (h != null) {
                        mChart.highlightValue(h);
                    }
                }
            }
        });
        mChart.setVisibility(View.VISIBLE);
    } else {
        elevationDataSet = null;
        slopeDataSet = null;
        mChart.setVisibility(View.GONE);
    }
    ((TextView) headerView.findViewById(R.id.average_text)).setText(OsmAndFormatter.getFormattedAlt(analysis.avgElevation, app));
    String min = OsmAndFormatter.getFormattedAlt(analysis.minElevation, app);
    String max = OsmAndFormatter.getFormattedAlt(analysis.maxElevation, app);
    ((TextView) headerView.findViewById(R.id.range_text)).setText(min + " - " + max);
    String asc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationUp, app);
    String desc = OsmAndFormatter.getFormattedAlt(analysis.diffElevationDown, app);
    ((TextView) headerView.findViewById(R.id.descent_text)).setText(desc);
    ((TextView) headerView.findViewById(R.id.ascent_text)).setText(asc);
    ((ImageView) headerView.findViewById(R.id.average_icon)).setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_altitude_average));
    ((ImageView) headerView.findViewById(R.id.range_icon)).setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_altitude_average));
    ((ImageView) headerView.findViewById(R.id.descent_icon)).setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_altitude_descent));
    ((ImageView) headerView.findViewById(R.id.ascent_icon)).setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_altitude_ascent));
    headerView.findViewById(R.id.details_view).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openDetails();
        }
    });
}
Also used : Highlight(com.github.mikephil.charting.highlight.Highlight) OsmandApplication(net.osmand.plus.OsmandApplication) OnChartGestureListener(com.github.mikephil.charting.listener.OnChartGestureListener) GPXTrackAnalysis(net.osmand.plus.GPXUtilities.GPXTrackAnalysis) ArrayList(java.util.ArrayList) ChartTouchListener(com.github.mikephil.charting.listener.ChartTouchListener) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) MotionEvent(android.view.MotionEvent) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) Matrix(android.graphics.Matrix) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LineChart(com.github.mikephil.charting.charts.LineChart)

Aggregations

MotionEvent (android.view.MotionEvent)3 OnChartGestureListener (com.github.mikephil.charting.listener.OnChartGestureListener)3 Matrix (android.graphics.Matrix)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 LineChart (com.github.mikephil.charting.charts.LineChart)2 LineData (com.github.mikephil.charting.data.LineData)2 Highlight (com.github.mikephil.charting.highlight.Highlight)2 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)2 ChartTouchListener (com.github.mikephil.charting.listener.ChartTouchListener)2 ArrayList (java.util.ArrayList)2 GPXTrackAnalysis (net.osmand.plus.GPXUtilities.GPXTrackAnalysis)2 OsmandApplication (net.osmand.plus.OsmandApplication)2 PopupMenu (android.support.v7.widget.PopupMenu)1 MenuItem (android.view.MenuItem)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 XAxis (com.github.mikephil.charting.components.XAxis)1 YAxis (com.github.mikephil.charting.components.YAxis)1