MPandroidchart - 我想在用户向上或向下或向右或向左滚动时添加更多点

时间:2017-02-28 08:12:46

标签: android graph mpandroidchart cartesian-coordinates

最初图表从-10到10但我希望当用户向右滚动时,评估值并且图形“增长”。是否有任何听众。 我的意思是当用户滚动到右边然后我添加更多的点作为函数,x的值总是为y。当用户向右或向左向下滚动时,图表会自行构建。

1 个答案:

答案 0 :(得分:3)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linechart = (LineChart) findViewById(R.id.chart);
        new Random(0).nextInt();
        linechart.setHighlightPerDragEnabled(true);
        linechart.setScaleEnabled(true);
        linechart.setPinchZoom(true);
        linechart.setDrawGridBackground(false);
        linechart.getDescription().setEnabled(false);
        linechart.setOnChartGestureListener(new GestureListener());

        LineData lineData = new LineData();
        lineData.setValueTextColor(Color.WHITE);
        linechart.setData(lineData);

        Legend legend = linechart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextColor(Color.WHITE);

        XAxis xaxis = linechart.getXAxis();
        xaxis.setDrawGridLines(true);
        xaxis.setTextColor(Color.WHITE);
        xaxis.setTextSize(15f);
        xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        YAxis yaxis = linechart.getAxisLeft();
        yaxis.setTextColor(Color.WHITE);
        yaxis.setTextSize(15f);
        yaxis = linechart.getAxisRight();
        yaxis.setTextColor(Color.WHITE);
        yaxis.setTextSize(15f);
        linechart.invalidate();
    }

    private void addEntry(int number) {
        data = linechart.getData();

        if (data != null)
        {
            ILineDataSet set = data.getDataSetByIndex(0);

            if (set == null) {
                set = createSet();
                data.addDataSet(set);
            }
            data.addEntry(new Entry(data.getDataSetByIndex(0).getEntryCount(), number), 0);
            data.notifyDataChanged();
            linechart.notifyDataSetChanged();

        }
    }

    private LineDataSet createSet() {

        LineDataSet set = new LineDataSet(null, "");
        set.setCubicIntensity(0.0f);
        set.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set.setColor(ColorTemplate.getHoloBlue());
        set.setCircleColor(ColorTemplate.getHoloBlue());
        set.setDrawValues(false);
        set.setLineWidth(2f);
        set.setCircleSize(4f);
        set.setFillAlpha(65);
        set.disableDashedLine();
        return set;

    }
    class GestureListener implements OnChartGestureListener {

        @Override
        public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

            addEntry(//add your entries here);
        }

        @Override
        public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
            linechart.moveViewToX(data.getEntryCount());

        }

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

        }
    }

试试这个