1
votes

I am using mpandroidchart Barchart to plot data from web response,But chart is drawn for only one point however response is got for more than 10 points How to plot such dynamic charts?

Using response from web I need to plot multiple points in barchart help me with below code

Web Response

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

Java code

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;

    }
1

1 Answers

1
votes

You need to call notifyDataSetChanged and invalidate each time in your getDataSet function's for loop.