如何使用Android中的齐射创建带有远程服务器数据的条形图?

时间:2016-12-30 08:07:11

标签: android android-volley

我想使用volley

创建带有mysq数据库的this one 条形图

我已经设法从数据库中检索数据并在文本视图中查看它工作正常但是当我尝试使用这些数据来填充条形图时没有发生任何事情我不知道它有什么问题。

我的杰森:

{"result":[{"id":"1","std1":"30","std2":"55","std3":"70","std4":"47","std5":"91","std6":"10","std7":"54"}]}

以下是我的整个java代码,我遵循here

中的barGraph概念
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.widget.TextView;
   import android.widget.Toast;

      import com.android.volley.Request;
      import com.android.volley.RequestQueue;
      import com.android.volley.Response;
      import com.android.volley.VolleyError;
      import com.android.volley.toolbox.JsonObjectRequest;
      import com.android.volley.toolbox.Volley;

      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;

      import java.util.ArrayList;
      import com.github.mikephil.charting.charts.BarChart;
      import com.github.mikephil.charting.data.BarData;
      import com.github.mikephil.charting.data.BarDataSet;
      import com.github.mikephil.charting.data.BarEntry;
      import com.github.mikephil.charting.utils.ColorTemplate;

  public class MainActivity extends AppCompatActivity {
     TextView textView;
     RequestQueue requestQueue;

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

      @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.viewData);


    chart = (BarChart) findViewById(R.id.chart1);

    BARENTRY = new ArrayList<>();

    BarEntryLabels = new ArrayList<String>();

    AddValuesToBarEntryLabels();

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

    BARDATA = new BarData(BarEntryLabels, Bardataset);

    Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

    chart.setData(BARDATA);

    chart.animateY(3000);
    AddValuesToBARENTRY();

         requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest
   =new JsonObjectRequest(Request.Method.POST,  http://**/**/barGraphData2.php", new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray= response.getJSONArray("result");
             for(int i=0;i<jsonArray.length();i++){
             JSONObject result= jsonArray.getJSONObject(i);
                 String std1= result.getString("std1");
                 String std2= result.getString("std2");
                 String std3= result.getString("std3");
                 String std4= result.getString("std4");
                 String std5= result.getString("std5");
                 String std6= result.getString("std6");
                 String std7= result.getString("std7");


                 // View data in text few
                 textView.setText(
                          "bar 1 -->"+std1+"\n"
                         +"bar 2 -->"+std2+"\n"
                         +"bar 3 -->"+std3+"\n"
                         +"bar 4 -->"+std4+"\n"
                         +"bar 5 -->"+std5+"\n"
                         +"bar 6 -->"+std6+"\n"
                         +"bar 7 -->"+std7+"\n"
                               );
                //call method  to use information in  barGraph 
       AddValuesToBARENTRY (std1, std2, std3, std4, std5, std6);

             }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(),"there is error bro",Toast.LENGTH_LONG).show();
                }
            }
    ) ;
requestQueue.add(jsonObjectRequest);
}


public void AddValuesToBARENTRY(String std1,String std2,String std3,String std4,String std5,String std6){
    BARENTRY.add(new BarEntry(Float.parseFloat(std1), 0));
    BARENTRY.add(new BarEntry(Float.parseFloat(std2), 1));
    BARENTRY.add(new BarEntry(Float.parseFloat(std3), 2));
    BARENTRY.add(new BarEntry(Float.parseFloat(std4), 3));
    BARENTRY.add(new BarEntry(Float.parseFloat(std5), 4));
    BARENTRY.add(new BarEntry(Float.parseFloat(std6), 5));

}

public void AddValuesToBarEntryLabels(){

    BarEntryLabels.add("January");
    BarEntryLabels.add("February");
    BarEntryLabels.add("March");
    BarEntryLabels.add("April");
    BarEntryLabels.add("May");
    BarEntryLabels.add("June");

}
}

1 个答案:

答案 0 :(得分:0)

根据Github Link的wiki,您需要在设置数据后刷新条形图。

尝试

//call method  to use information in  barGraph 
AddValuesToBARENTRY (std1, std2, std3, std4, std5, std6);
// refresh the bar chart
chart.invalidate();