androidandroid-studioandroid-graphview

GraphView is not displaying first Datapoint on tap


These code is supposed to draw a graph and then on tap, it should display the corresponding datapoint as toast notification. Everything works fine, except the first datapoint is not displayed on tap (x=1,y=amount). It just displays from (x=2,y=amount).

public void DrawGraph(int term, double amount, Float P, Float r, GraphView graph){

        graph.setVisibility(View.VISIBLE);
        graph.getViewport().setScrollable(true);
        graph.getViewport().setScrollableY(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(term+1);
        graph.getViewport().setMinY(0);
        graph.getViewport().setMaxY(Math.round(amount)*1.15);
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setXAxisBoundsManual(true);

        graph.removeAllSeries();
        LineGraphSeries<DataPoint> series = new LineGraphSeries<>();

        for (int i=1;i<=term;i++){
            int x=i;
            long y=Math.round(P * java.lang.Math.pow(1 + (r) / 100, i));
            series.appendData(new DataPoint(x,y),true,term);
        }

        graph.addSeries(series);
        series.setOnDataPointTapListener(new OnDataPointTapListener() {
            @Override
            public void onTap(Series series, DataPointInterface dataPoint) {
                Toast.makeText(SinglePremium.this,"After "+(int)dataPoint.getX()+" years amount becomes "+(long)dataPoint.getY(),Toast.LENGTH_LONG).show();
            }
        });
    }

Solution

  • This line of code fixed the issue.

    series.setDrawDataPoints(true);
    

    Here, series is an object of LineGraphSeries.