使用MPAndroidChart分别绘制LineChart?

时间:2017-07-05 08:56:53

标签: android json linechart mpandroidchart

我正在使用MPAndroidChart库来显示LineChart。我无法独立地使用TermWise显示LineChart.I已使用相同的ArrayList,即名为allMarks来存储所有Term的信息。当我清除时如果ArrayList.Clear();那么ArrayList只会在Chart中绘制特定术语标记。如果未使用,则显示所有标记。每个术语标记如何单独显示?

控制台

public class Dashboard extends Fragment {

    ListView listViewNotice, listviewpendingbills;
    String Navigation_URL = "http://192.168.100.5:84/api/academics/getNotices";
    String Navigation_URL_BIlls = "http://192.168.100.5:84/Api/financeApi/getAllFees";
    String Navigation_URL_CHART = "http://192.168.100.5:84/api/academics/getSingleStudentsMarks";
    String Navigation_URL_getExam = "http://192.168.100.5:84/api/academics/getExams";
    String Navigation_URL_getExamData = "http://192.168.100.5:84/api/academics/getStdMarksByTermStd";


    String access_token;
    String Notice, BillNumber, Status, AmountPaid, ReceiptIssueDate;
    String master_id;
    TextView pendingbills, NoNotice;
    LineChart chart;


    private static final String TAG = "Test";
    ArrayList<Entry> entries, entry;
    ArrayList<String> labels = new ArrayList<String>();
    LineDataSet set, set1;
    LineData data, data2;

    // ArrayList<LineData> xVals = new ArrayList<String>();

    ArrayList allExams;
    ArrayList getCourses;
    ArrayList getMarks;
    ArrayList getCourseID;
    ArrayList getExamDesc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        // getActivity().setTitle("St. Xavier's High School");
        setHasOptionsMenu(true);

        listViewNotice = (ListView) view.findViewById(R.id.list_student_notice_dashboard);
        listviewpendingbills = (ListView) view.findViewById(R.id.student_dashboard_pendingbills);
        pendingbills = (TextView) view.findViewById(R.id.textpendingbills);
        NoNotice = (TextView) view.findViewById(R.id.textnoticedashboard);
        //  lineChart = (BarChart) view.findViewById(R.id.chart1);
        chart = (LineChart) view.findViewById(R.id.chart1);

        SessionManagement session = new SessionManagement(getContext());
        session.checkLogin();
        access_token = session.getAccesstToken();
        master_id = session.getMasterId();
        makeJsonObjectRequestNotices();
        makeJsonObjectRequestBills();
        //  makeJsonObjectRequestGraph();

        makeJsonObjectMarksWithGraph();

        allExams = new ArrayList();
        getCourses = new ArrayList();
        getMarks = new ArrayList();
        getCourseID = new ArrayList();
        getExamDesc = new ArrayList();
        labels.add("Math");
        chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
        chart.invalidate();

        return view;
    }

  String examDescription;
    private void makeJsonObjectMarksWithGraph() {

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        String URL1 = Navigation_URL_getExam;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            JSONArray jArray = new JSONArray(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);
                                String TermID = jsonObject.getString("TermID");
                                examDescription = jsonObject.getString("examDescription");
                                getTermID(TermID, examDescription);
                                allExams.add(TermID);
                                Log.d("Term ID", getTermID(TermID, examDescription));
                            }
                            Log.d("All Exam", String.valueOf(allExams.size()));
                            int j;
                            for (j = 0; j < allExams.size(); j++) {
                                String termID = (String) allExams.get(j);
                                getExamData(termID);
                            }


                            //Collections.sort(entries, new EntryXComparator());

                        } catch (JSONException e) {
                            Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }
                }

                , new Response.ErrorListener()

        {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.toString(), LENGTH_LONG).show();
            }
        }
        )

        {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Bearer " + access_token);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }

      /*      @Override
            protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();

                map.put("id", master_id);
                //map.put("accessID", accessID);
                map.put("currentUser", master_id);
                return map;

            }   */
        };


        requestQueue.add(stringRequest);
    }

 public String TermID;
    public String Description;

    private String getTermID(String TermID, String Description) {
        this.TermID = TermID;
        this.Description = Description;
        return TermID;
    }

    private void getExamData(String TermID) {

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        String URL1 = Navigation_URL_getExamData + "?StdID=" + master_id + "&TermID=" + TermID;
        //String URL1 = Navigation_URL_CHART + "?StdID=" + master_id;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            JSONArray jArray = new JSONArray(response);
                            if (jArray.length() != 0) {
                                for (int i = 0; i < jArray.length(); i++) {
                                    JSONObject jsonObject = jArray.getJSONObject(i);


                                    Log.d("JSON Object", String.valueOf(jsonObject));
                                    String CourseId = jsonObject.getString("CourseID");
                                    String CourseName = jsonObject.getString("CourseName");
                                    String examDescription = jsonObject.getString("examDescription");
                                    Float Marks = Float.valueOf(jsonObject.getString("Marks"));


                                    getMarks.add(Marks);
                                    getCourses.add(CourseName);
                                    getCourseID.add(CourseId);
                                    getExamDesc.add(examDescription);
                                    System.out.println("the Courses are" + getCourses);

                                }
                                Log.d("JSON Object", String.valueOf(getMarks));
                                plotGraph(getMarks);
                                //getMarks.clear();

                            }

                        } catch (JSONException e) {
                            Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.toString(), LENGTH_LONG).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Bearer " + access_token);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }
        };

        requestQueue.add(stringRequest);
    }
  ArrayList<Entry> allMarks = new ArrayList<Entry>();

    private void plotGraph(ArrayList getMarks) {
        ArrayList<Entry> allMarks = new ArrayList<Entry>();
        System.out.println("GetMARKS" + getMarks.size());
        ArrayList<ILineDataSet> testDataSet = new ArrayList<>();
        // HashMap<ILineDataSet> map = new HashMap<String, String>();


        for (int k = 0; k < getMarks.size(); k++) {
          /*  allMarks.add(new Entry(k, (Float) getMarks.get(k)));
            set = new LineDataSet(allMarks, examDescription);
            data = new LineData(set);
            lines.add(data);

            chart.setData(data);

            set.setColor(Color.GREEN);
            set.setDrawFilled(true);
            set.setDrawValues(true);
            chart.notifyDataSetChanged();
            chart.invalidate();

            */


            allMarks.add(new Entry(k, (Float) getMarks.get(k)));
            LineDataSet set = new LineDataSet(allMarks, examDescription);
            // set different color for each set as your requirement
            testDataSet.add(set);
            chart.notifyDataSetChanged();
            chart.invalidate();
            //  allMarks.clear();

        }

        chart.setData(new LineData(testDataSet));
    }
}

http://192.168.100.5:84/api/academics/getSingleStudentsMarks?StdID=0095

[
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4027,
    "Marks": 45,
    "Sno": 2107,
    "StdID": 95,
    "TermID": 6022,
    "CourseName": "Arts",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "First Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5034,
    "Marks": 90,
    "Sno": 2108,
    "StdID": 95,
    "TermID": 6022,
    "CourseName": "Math",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "First Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5035,
    "Marks": 52,
    "Sno": 2109,
    "StdID": 95,
    "TermID": 6022,
    "CourseName": "English",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "First Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 6034,
    "Marks": 70,
    "Sno": 2110,
    "StdID": 95,
    "TermID": 6022,
    "CourseName": "hi",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "First Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4027,
    "Marks": 50,
    "Sno": 4119,
    "StdID": 95,
    "TermID": 7024,
    "CourseName": "Arts",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Second",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5034,
    "Marks": 60,
    "Sno": 4120,
    "StdID": 95,
    "TermID": 7024,
    "CourseName": "Math",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Second",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5035,
    "Marks": 70,
    "Sno": 4121,
    "StdID": 95,
    "TermID": 7024,
    "CourseName": "English",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Second",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 6034,
    "Marks": 80,
    "Sno": 4122,
    "StdID": 95,
    "TermID": 7024,
    "CourseName": "hi",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Second",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4027,
    "Marks": 10,
    "Sno": 4123,
    "StdID": 95,
    "TermID": 7025,
    "CourseName": "Arts",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Third",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5034,
    "Marks": 20,
    "Sno": 4124,
    "StdID": 95,
    "TermID": 7025,
    "CourseName": "Math",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Third",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5035,
    "Marks": 30,
    "Sno": 4125,
    "StdID": 95,
    "TermID": 7025,
    "CourseName": "English",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Third",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 6034,
    "Marks": 90,
    "Sno": 4126,
    "StdID": 95,
    "TermID": 7025,
    "CourseName": "hi",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "Third",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4027,
    "Marks": 80,
    "Sno": 4127,
    "StdID": 95,
    "TermID": 7026,
    "CourseName": "Arts",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "fourth term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5034,
    "Marks": 10,
    "Sno": 4128,
    "StdID": 95,
    "TermID": 7026,
    "CourseName": "Math",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "fourth term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 5035,
    "Marks": 76,
    "Sno": 4129,
    "StdID": 95,
    "TermID": 7026,
    "CourseName": "English",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "fourth term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 6034,
    "Marks": 93,
    "Sno": 4130,
    "StdID": 95,
    "TermID": 7026,
    "CourseName": "hi",
    "Terminal_FM": 100,
    "Terminal_PM": 40,
    "UT_FM": 50,
    "UT_PM": 20,
    "examDescription": "fourth term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  }
]
  

我目前正在OneArray中获得所有的术语标记,称为 AllMarks 。我不能在同一图表中用不同颜色的不同术语绘制标记吗?

2 个答案:

答案 0 :(得分:2)

这应该是你的工作...... 术语数据应该分组并显示

private void makeJsonObjectMarksWithGraph() {

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    String URL1 = Navigation_URL_getExam;
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL1,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {


                        JSONArray jsonArray = new JSONArray(response);

    HashMap<Integer, ArrayList<JSONObject>> dataFilteredByTerm = new HashMap<>();

// filter response to add terms
    ArrayList<Integer> termIDs = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {

        if (i == 0) {
            termIDs.add(jsonArray.optJSONObject(i).optInt("TermID"));
        }
        boolean check = termIDs.contains(jsonArray.optJSONObject(i).optString("TermID"));
        if (check) {
            termIDs.add(jsonArray.optJSONObject(i).optInt("TermID"));
        }
    }
//group json response according to term ids
    for (int i = 0; i < termIDs.size(); i++) {

        ArrayList<JSONObject> jsonObjects = new ArrayList<>();

        for (int k = 0; k < jsonArray.length(); k++) {

            if (termIDs.get(i) == jsonArray.optJSONObject(k).optInt("TermID")) {

                jsonObjects.add(jsonArray.optJSONObject(k));
            }
        }

        dataFilteredByTerm.put(termIDs.get(i), jsonObjects);
    }
//set entry for every marks grouped by terms
    ArrayList<ILineDataSet> testDataSet = new ArrayList<>();

    for (int i = 0; i < dataFilteredByTerm.size(); i++) {

        ArrayList<JSONObject> values = dataFilteredByTerm.get(termIDs.get(i));
        ArrayList<Entry> allMarks = new ArrayList<>();

        for (int k = 0; k < values.size(); k++) {
            allMarks.add(new Entry(k, values.get(k).optInt("Marks")));
        }
        LineDataSet set = new LineDataSet(allMarks, values.get(i).optString("examDescription"));

        testDataSet.add(set);

    }
    chart.setData(new LineData(testDataSet));
    chart.notifyDataSetChanged();
    chart.invalidate();


                    } catch (JSONException e) {
                        Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }


                }


            }

            , new Response.ErrorListener()

    {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getContext(), error.toString(), LENGTH_LONG).show();
        }
    }

    )

    {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "Bearer " + access_token);
            headers.put("Content-Type", "application/x-www-form-urlencoded");
            return headers;
        }

  /*      @Override
        protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();

            map.put("id", master_id);
            //map.put("accessID", accessID);
            map.put("currentUser", master_id);
            return map;

        }   */
    };


    requestQueue.add(stringRequest);

}

答案 1 :(得分:0)

试试这个。它会将图表显示为立方体形状而不是直线。

dataset.setDrawFilled(true);
dataset.setDrawCubic(true);
dataset.setDrawValues(false);

数据集是 LineDataSet 的对象。

以上代码适用于版本编译'com.github.PhilJay:MPAndroidChart:v2.1.6'