我正在使用mpandroidchart Barchart从Web响应中绘制数据,但是图表仅绘制了1点,但是响应却获得了10点以上
如何绘制这样的动态图表?

使用网络响应,我需要在条形图中绘制多个点,以帮助我使用下面的代码

网络回应

[{{date(answerDate)“:” 2015-11-21“,” sum(answerSelectedCode)“:” 23“},{” date(answerDate)“:” 2015-11-23“,” sum(answerSelectedCode) “:” 21“}]

Java代码

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
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;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profiles);
        toolbar = (Toolbar) findViewById(R.id.appBarSecond);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.setTitle("Profile");

        BarChart chart = (BarChart) findViewById(R.id.chart);
        BarData data = new BarData(getXAxisValues(), getDataSet());
        chart.setData(data);
        YAxis leftAxis  = chart.getAxisLeft();
        YAxis rightAxis  = chart.getAxisRight();
        rightAxis.setAxisMaxValue(3);
        rightAxis.setLabelCount(3);

        leftAxis.setAxisMaxValue(3);
        leftAxis.setLabelCount(3);

        chart.setDescription("Productivity");
        chart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
        chart.setDrawGridBackground(false);
        chart.animateXY(2000, 2000);
        chart.invalidate();



    }


      private ArrayList<BarDataSet> getDataSet() {
        ArrayList<BarDataSet> dataSets = null;

        final ArrayList<BarEntry> valueSet2 = new ArrayList<>();

        final Intent intent = getIntent();
        final String getUserId =  intent.getStringExtra("UserId");
        new Thread(new Runnable() {
            public void run() {
                HttpRequest req = null;
                try {
                    req = new HttpRequest("http://xyz.abc.in/response.php");
                    final String response = req.preparePost().withData("answerUserId="+getUserId).sendAndReadString();
                    System.out.println("Response"+response);

                    runOnUiThread(new Runnable() {
                        public void run() {
                            try {

                                JSONArray myObjects = new JSONArray(response);

                                for (int j = 0; j < myObjects.length(); j++) {
                                    JSONObject jsonobject = myObjects.getJSONObject(j);
                                    String Date = jsonobject.getString("date(answerDate)");
                                    Integer Total = Integer.valueOf(jsonobject.getString("sum(answerSelectedCode)"));


                                    valueSet2.add(new BarEntry(Total,j));
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    });


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Productivity");
        barDataSet2.setColor(Color.parseColor("#8BC34A"));
        dataSets = new ArrayList<>();
        dataSets.add(barDataSet2);
        return dataSets;
    }

     private ArrayList<String> getXAxisValues() {
        final ArrayList<String> xAxis = new ArrayList<>();
        final Intent intent = getIntent();
        final String getUserId =  intent.getStringExtra("UserId");
        new Thread(new Runnable() {
            public void run() {
                HttpRequest req = null;
                try {
                    req = new HttpRequest("http://abc.xre.in/response.php");
                    final String response = req.preparePost().withData("answerUserId="+getUserId).sendAndReadString();


                    JSONArray myObjects = null;
                    try {
                        myObjects = new JSONArray(response);

                        for (int ji = 0; ji < myObjects.length(); ji++) {
                            JSONObject jsonobject = myObjects.getJSONObject(ji);
                            final String Date = jsonobject.getString("date(answerDate)");
                            System.out.println("Date Response =====>: " + Date);
                            System.out.println("Loop "+ji+" executed");
                            xAxis.add(Date);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }






                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();



        return xAxis;

    }

最佳答案

您每次需要在notifyDataSetChanged函数的for循环中调用invalidategetDataSet

关于android - 动态MpAndroidChart无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33885867/

10-10 08:29