BarChart没有显示正确的动态数据

时间:2018-07-23 06:37:48

标签: java android bar-chart mpandroidchart

我已经实现了条形图,其中数据将动态生成。在我的代码中,数据来自服务器,但图形未显示正确的数据,它仅显示-1、0和1位置。我正在使用com.github.PhilJay:MPAndroidChart:v2.2.4库制作条形图。

下面是我的片段类

public class MonthsFragment extends Fragment {

public String user_id;
// Session Manager Class
UserSessionManager session;

BarChart chartUniqueVisitors;
ArrayList<BarEntry> BARENTRY ;
ArrayList<String> BarEntryLabels ;
BarDataSet Bardataset ;
BarData BARDATA ;

public float jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec;

public MonthsFragment(){}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_unique_visitors, container, false);

    session = new UserSessionManager(getContext());
    session.checkLogin();
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    user_id = user.get(UserSessionManager.KEY_USER_ID);
    return view;
}

    private void getDataForBusinessAddress() {
        String URL ="XYZ.php?username="+user_id;
        StringRequest stringRequest = new StringRequest(Request.Method.POST,URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("Response montghly____",response);

                JSONArray jsonarray = null;
                try {
                    jsonarray = new JSONArray(response);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        jan = Float.parseFloat(jsonobject.getString("Jan"));
                        feb = Float.parseFloat(jsonobject.getString("Feb"));
                        mar = Float.parseFloat(jsonobject.getString("Mar"));
                        apr = Float.parseFloat(jsonobject.getString("Apr"));
                        may = Float.parseFloat(jsonobject.getString("May"));
                        jun = Float.parseFloat(jsonobject.getString("Jun"));
                        jul = Float.parseFloat(jsonobject.getString("Jul"));
                        aug = Float.parseFloat(jsonobject.getString("Aug"));
                        sep = Float.parseFloat(jsonobject.getString("Sep"));
                        oct = Float.parseFloat(jsonobject.getString("Oct"));
                        nov = Float.parseFloat(jsonobject.getString("Nov"));
                        dec = Float.parseFloat(jsonobject.getString("Dece"));
                        AddValuesToBARENTRY(jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

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

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.getCache().clear();
        requestQueue.add(stringRequest);
    }

public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);
    chartUniqueVisitors = (BarChart) v.findViewById(R.id.chartUniqueVisitors);

    getDataForBusinessAddress();

    BARENTRY = new ArrayList<>();
    BarEntryLabels = new ArrayList<String>();
    AddValuesToBarEntryLabels();
    Bardataset = new BarDataSet(BARENTRY, "Projects");
    BARDATA = new BarData(BarEntryLabels, Bardataset);
    Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
}

AddValuesToBARENTRY函数将响应数据添加到BARENTRY ArrayList

 public void AddValuesToBARENTRY(float jan, float feb, float mar, float apr, float may, float jun, float jul, float aug, float sep, float oct, float nov, float dec){
    BARENTRY.add(new BarEntry(jan, 0));
    BARENTRY.add(new BarEntry(feb, 1));
    BARENTRY.add(new BarEntry(mar, 2));
    BARENTRY.add(new BarEntry(apr, 3));
    BARENTRY.add(new BarEntry(may, 4 ));
    BARENTRY.add(new BarEntry(jun, 5 ));
    BARENTRY.add(new BarEntry(jul, 6));
    BARENTRY.add(new BarEntry(aug, 7));
    BARENTRY.add(new BarEntry(sep, 8));
    BARENTRY.add(new BarEntry(oct, 9));
    BARENTRY.add(new BarEntry(nov, 10));
    BARENTRY.add(new BarEntry(dec, 11));
    chartUniqueVisitors.setData(BARDATA);
}

AddValuesToBarEntryLabels函数可在Barchat上添加值

public void AddValuesToBarEntryLabels(){
    BarEntryLabels.add("Jan");
    BarEntryLabels.add("Feb");
    BarEntryLabels.add("Mar");
    BarEntryLabels.add("Apr");
    BarEntryLabels.add("May");
    BarEntryLabels.add("Jun");
    BarEntryLabels.add("Jul");
    BarEntryLabels.add("Aug");
    BarEntryLabels.add("Sep");
    BarEntryLabels.add("Oct");
    BarEntryLabels.add("Nov");
    BarEntryLabels.add("Dec");
  }
 }

输出类似于this,格式不正确,谢谢

1 个答案:

答案 0 :(得分:1)

问题在于填写BarEntry

BARENTRY.add(new BarEntry(jan, 0));
BARENTRY.add(new BarEntry(feb, 1));

应该是:

BARENTRY.add(new BarEntry(0, jan));
BARENTRY.add(new BarEntry(1, feb));

第一个值索引(x值)第二个是实际值-BAR(来自fom服务器)的高度。

也可以在onCreate方法上调用setData一次,现在您可以循环调用它,并且多次调用。

chartUniqueVisitors.setData(BARDATA);

这是我对您的数据的了解

enter image description here