有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

条形图上未设置java截击响应数据

我想在我的应用程序片段上实现条形图,数据将来自截击响应。我正在使用compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'库来绘制图形。数据来自响应并成功拆分,但当我将该数据放在条形图上时,它显示为null。请检查我的代码并帮助我解决此问题。先谢谢你

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;
}


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

    BARENTRY = new ArrayList<>();
    BarEntryLabels = new ArrayList<String>();

    getDataForBusinessAddress();
    AddValuesToBARENTRY();
    AddValuesToBarEntryLabels();

    Bardataset = new BarDataSet(BARENTRY, "Projects");

    BARDATA = new BarData(BarEntryLabels, Bardataset);

    Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

    chartUniqueVisitors.setData(BARDATA);

    chartUniqueVisitors.animateY(3000);
}

函数从服务器获取数据

private void getDataForBusinessAddress() {
    String getBusinessAddressURL ="xyz.php?username="+user_id;
    StringRequest stringRequest = new StringRequest(Request.Method.POST,getBusinessAddressURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String 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"));
                }
            } 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);
}

函数设置BarEntry上的数据。我尝试在这里登录,但没有数据

public void AddValuesToBARENTRY(){
    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));
}

函数设置条形图中的名称

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");
  }
}      

共 (1) 个答案

  1. # 1 楼答案

    请尝试使用以下代码:

            private BarChart positiveChart;
    
            ArrayList<BarEntry> value;
    
            @Override
                public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View view = inflater.inflate(R.layout.fragment_view_que, container, false);
    
                    this.positiveChart = view.findViewById(R.id.positiveChart);
                    value = new ArrayList<>();
    
        chartAdjust();  // it is optional for my required formatting
    setValue();
            }
    
    
        private void chartAdjust() {
                Description description = new Description();
                description.setText("");
    
                positiveChart.setMaxVisibleValueCount(100);
                positiveChart.setFitBars(true);
                positiveChart.setHighlightFullBarEnabled(false);
                positiveChart.disableScroll();
                positiveChart.setDescription(description);
                positiveChart.getLegend().setEnabled(true);
                positiveChart.setDoubleTapToZoomEnabled(false);
                positiveChart.setScaleEnabled(false);
                positiveChart.getAxisLeft().setDrawLabels(false);
                positiveChart.getAxisRight().setDrawLabels(false);
                positiveChart.getXAxis().setDrawLabels(false);
                positiveChart.getAxisLeft().setDrawGridLines(false);
                positiveChart.getAxisRight().setDrawGridLines(false);
                positiveChart.getXAxis().setDrawGridLines(false);
                positiveChart.setNoDataTextColor(Color.RED);
                positiveChart.setBackgroundColor(Color.WHITE);
                positiveChart.setDrawBorders(false);
                positiveChart.getXAxis().setDrawAxisLine(false);
                positiveChart.getAxisRight().setEnabled(false);
                positiveChart.getAxisLeft().setEnabled(false);
        }
    
    
    public void setValue()
    {
    float maleOp1 = (float) (data.getAnswerOfManOption1());
                float femaleOp1 = (float) (data.getAnswerOfWomanOption1());
    
                xValue.add(new BarEntry(0, new float[]{maleOp1, femaleOp1}));
    
                BarDataSet set1;
                set1 = new BarDataSet(xValue, "");
                set1.setDrawIcons(true);
                set1.setStackLabels(new String[]{"Male", "Female"});
                set1.setDrawValues(true);
                set1.setValueTextSize(12f);
                set1.setColors(ColorTemplate.JOYFUL_COLORS);
    
                BarData data1 = new BarData(set1);
                data1.setBarWidth(15f);
    
    // And BarDataSet And BarData Is set as per your required then  set  the value in the  Variable of barchart : see below you
    
                positiveChart.setData(data1);
                positiveChart.setFitBars(true);
                positiveChart.invalidate();
    }
    

    我希望这段代码能帮助你