我能够解析JSON数据并在LOG中显示信息,但数据未在图表中正确显示。我已经添加了要显示的标签,但不幸的是标签也没有显示。
控制台
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 access_token;
String Notice, BillNumber, Status, AmountPaid, ReceiptIssueDate;
String master_id;
TextView pendingbills, NoNotice;
BarChart lineChart;
LineChart chart;
private static final String TAG = "Test";
ArrayList<Entry> entries;
ArrayList<String> labels;
LineDataSet set;
LineData data;
@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();
makeJsonObjectRequest();
makeJsonObjectRequestBills();
makeJsonObjectRequestGraph();
entries = new ArrayList<Entry>();
// set = new LineDataSet(entries, "Valuesl");
// data = new LineData(set);
entries.add(new Entry(0f, 30f));
entries.add(new Entry(1f, 80f));
entries.add(new Entry(2f, 36f));
entries.add(new Entry(3f, 40f));
entries.add(new Entry(23.00f, 0));
entries.add(new Entry(40.00f, 1));
// entries.add(new Entry(00.00f, 2)); // want to skip this index 2(Mar)
// entries.add(new Entry(00.00f, 3)); // want to skip this index 3 (Apr)
entries.add(new Entry(94.00f, 4));
entries.add(new Entry(20.00f, 5));
// entries.add(new Entry(4f, 0));
//entries.add(new Entry(8f, 1));
//entries.add(new Entry(6f, 2));
//entries.add(new Entry(2f, 3));
//entries.add(new Entry(18f, 4));
//entries.add(new Entry(9f, 5));
// chart.invalidate();
labels = new ArrayList<>();
labels = new ArrayList<>();
labels.add("Nepali");
labels.add("English");
labels.add("Math");
labels.add("Science");
// Collections.sort(entries, new EntryXComparator());
return view;
}
private void makeJsonObjectRequestGraph() {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
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);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String Marks = jsonObject.getString("Marks");
String examDescription = jsonObject.getString("examDescription");
if (examDescription.equals("First Term")) {
entries.add(new BarEntry(Float.parseFloat(Marks), i));
System.out.println(new BarEntry(Float.parseFloat(Marks), i));
// chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
}
}
set = new LineDataSet(entries, "");
data = new LineData(set);
chart.setData(data);
chart.notifyDataSetChanged();
chart.invalidate();
set.setColor(Color.RED);
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);
}
}
记录输出
05-21 16:50:43.405 12459-12459/com.example.user.mis I/System.out: Entry, x: 80.0 y: 0.0
05-21 16:50:43.405 12459-12459/com.example.user.mis I/System.out: Entry, x: 80.0 y: 1.0
05-21 16:50:43.405 12459-12459/com.example.user.mis I/System.out: Entry, x: 80.0 y: 2.0
05-21 16:50:43.405 12459-12459/com.example.user.mis I/System.out: Entry, x: 80.0 y: 3.0
05-21 16:50:43.405 12459-12459/com.example.user.mis I/System.out: Entry, x: 80.0 y: 4.0
05-21 16:50:43.491 12459-12459/com.example.user.mis W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
如何在图表中正确显示信息?
答案 0 :(得分:1)
更改x轴和Y轴值。标记将根据您的要求绘制。
private void makeJsonObjectRequestGraph() {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
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);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String Marks = jsonObject.getString("Marks");
String examDescription = jsonObject.getString("examDescription");
if (examDescription.equals("First Term ")) {
entries.add(new BarEntry(i, Float.parseFloat(Marks)));
System.out.println(new BarEntry(i, Float.parseFloat(Marks)));
}
}
set = new LineDataSet(entries, "Wow");
data = new LineData(set);
chart.setData(data);
chart.notifyDataSetChanged();
chart.invalidate();
set.setColor(Color.RED);
//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);
}
变化
entries.add(new BarEntry(Float.parseFloat(Marks),i));
要
entries.add(new BarEntry(i,Float.parseFloat(Marks)));
显示标签
add this
chart.getXAxis()。setValueFormatter(new IndexAxisValueFormatter(labels));