0

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();
            }
        });
    }
  • Do you solve your problem ? I exactly have the issue of first element(datapoint) no displaying.Also I test for near y values and far y values. No any result.Is this a bug? Only way is adding a dummy datapoint as first element ({10,0}) .Of course 10 is smaller than lowest x value. – maniaq May 20 '20 at 11:22
  • Yes. Fixed it using the code in the comment below. – Githin Joseph May 27 '20 at 04:43
  • Ok I will test it. Also I found another reason for not displaying : If you use .appendData method the first data point maybe disappeared – maniaq May 27 '20 at 10:54
  • No . If you have a barGraph , the method of setDrawDataPoints does not exist ! In your opinion in bargraphs what is the solution ? – maniaq May 28 '20 at 08:39
  • I haven't tried it on bar graphs. May be it's a bug with the library. I think it is better to switch to other alternatives https://stackoverflow.com/questions/26467376/android-charting-libraries . – Githin Joseph May 28 '20 at 16:06

1 Answers1

0

This line of code fixed the issue.

series.setDrawDataPoints(true);

Here, series is an object of LineGraphSeries.