首页 > 解决方案 > 进行网络调用后如何使用 GraphView 绘制图形?

问题描述

我正在使用GraphView,(参考this)来绘制图表。

下面是我正在使用 进行网络调用的活动Volley,我在其中获取信息以绘制图表。目前,我正在使用测试数据来绘制图表。寻找我的评论“这里它正在工作”“这是我需要调用 DoTheGraphs(),但不工作的地方”。在活动中。

每当我DoTheGraphs()在解析后调用JSON(第二条评论),GraphView并没有绘制数据并且我的手机变得无响应而没有抛出任何错误。任何帮助将不胜感激。

ReportDetailsActivity.java

public class ReportDetailsActivity extends Activity {

        private ArrayList<Hemoglobin> hemoglobins;
        private GraphView graph;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_report_details);


            graph =  findViewById(R.id.graph);

    DoingNetworkStuff(Endpoints.BASE_URL+Endpoints.GET_HEMOGLOBIN_READINGS);
//Here It is working
            DoTheGraphs();


        }

        private void DoingNetworkStuff(String url) {
            final String TAG = "ReportsActivity_DoingNetworkStuff";
            final ProgressDialog pDialog = new ProgressDialog(this);
            pDialog.setMessage("Loading...");
            pDialog.show();
            StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                hemoglobins = new ArrayList<>();
                                JSONArray jsonResponse = new JSONObject(response).getJSONArray("info");

                                for (int i = 0; i < jsonResponse.length(); i++) {
                                    JSONObject obj = jsonResponse.getJSONObject(i);
                                    String date = obj.optString("lappointmentdate");
                                    String value = obj.optString("lhemoglobinreading");

                                    hemoglobins.add(new Hemoglobin(date,value));
                                }

//This is where I need to call DoTheGraphs(), but not working.



                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            pDialog.dismiss();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            // hide the progress dialog
                            pDialog.hide();
                            pDialog.setMessage("Check your internet connection Technical Difficulties!");
                            pDialog.show();
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                public void run() {
                                    pDialog.dismiss();
                                }
                            }, 5000);
                        }
                    }

            ) {
                @Override
                protected Map<String, String> getParams() {

                    Map<String, String> params = new HashMap<>();
                    // the POST parameters:
                    params.put("patientid", "VIU1111111111433");
                    params.put("patientdependantid", "");
                    return params;
                }
            };
            Volley.newRequestQueue(this).add(postRequest);
        }


        private void DoTheGraphs() {

            Date d1 = null;
            Date d2 = null;
            Date d3 = null;
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            try {
                d1 = formatter.parse("03/09/1995");
                d2 = formatter.parse("04/09/1995");
                d3 = formatter.parse("05/09/1995");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date[] date = {d1,d2,d3};
            DataPoint[] dataPoints = new DataPoint[3];
            for(int i=0;i<3;i++){
                dataPoints[i]=new DataPoint(date[i],1);//new DataPoint(hemoglobins.get(i).getDate(), hemoglobins.get(i).getValue());
            }

            LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dataPoints);

            graph.addSeries(series);

            graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(ReportDetailsActivity.this));
            graph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space

            graph.getViewport().setMinX(date[0].getTime());
            graph.getViewport().setMaxX(date[2].getTime());
            graph.getViewport().setXAxisBoundsManual(true);

            graph.getGridLabelRenderer().setHumanRounding(false);


        }
    }

我想通了 graph.getGridLabelRenderer().setHumanRounding(false); 导致所有问题。我不知道为什么?

同样的问题: https ://github.com/jjoe64/GraphView/issues/440 我在那里使用了解决方案,同样的事情。

这是控制台日志 在此处输入图像描述

标签: androidandroid-volleyandroid-graphview

解决方案


推荐阅读