Shirin Shirin - 13 days ago 8
JSON Question

AChartEngine doesn't show graph from remote database

I am new to AChartEngine. I'm trying to retrieve JSON data from a remote database and plug it in AchartEngine Bar Chart. I can successfully fetch data. However, I'm having difficulties to plug it into the graph. The chart is not getting my data. When I hardcode or put a random number, it shows on the chart but when I try to include data from database it shows a blank page. I'm posting my code. Any help would be appreciated. I'm trying to show date in "X" axis and amount in "Y" axis.

public class BarGraphActivity extends AppCompatActivity {

private GraphicalView mChart;
private XYSeries transactionSeries;
private XYMultipleSeriesDataset dataset;
private XYSeriesRenderer transactionRenderer;
private XYMultipleSeriesRenderer multiRenderer;

//JSON node name
private static final String TAG_TYPE = "type";
private static final String TAG_AMOUNT = "amount";
private static final String TAG_CATEGORY = "category";
private static final String TAG_DESC = "desc";
private static final String TAG_DATE = "date";
private static final String TAG_SUCCESS = "success";
static final String FETCH_URL = "http://moneymoney.zapto.org:8080";


String amount = null;
String desc = null;
String type = null;
String date = null;
String category = null;
ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bar_graph);

//showTransaction();
//settingup bar chart
setupChart();

//start plotting chart
new ChartTask().execute();
}

public void setupChart(){

//creating an XYSeries for Transactions
transactionSeries = new XYSeries("Transactions");

//creating a dataset to hold each series
dataset = new XYMultipleSeriesDataset();
//adding transactionseries to the dataset
dataset.addSeries(transactionSeries);

//Creating a XYMultipleSeriesRenderer to customize transaction series
transactionRenderer = new XYSeriesRenderer();
transactionRenderer.setColor(Color.YELLOW);
transactionRenderer.setPointStyle(PointStyle.CIRCLE);
transactionRenderer.setFillPoints(true);
transactionRenderer.setLineWidth(4);
transactionRenderer.setDisplayChartValues(true);

// Creating a XYMultipleSeriesRenderer to customize the whole chart
multiRenderer = new XYMultipleSeriesRenderer();

multiRenderer.setChartTitle("Transaction Trends");
multiRenderer.setXTitle("Date");
multiRenderer.setYTitle("Amount");
multiRenderer.setZoomButtonsVisible(true);

multiRenderer.setXAxisMin(0);
multiRenderer.setXAxisMax(10);

multiRenderer.setYAxisMin(0);
multiRenderer.setYAxisMax(10);

multiRenderer.setBarSpacing(1);

// Adding transactionRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(transactionRenderer);

// Getting a reference to LinearLayout of the bar graph activity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);

mChart = (GraphicalView) ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, BarChart.Type.DEFAULT);

// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}

private class ChartTask extends AsyncTask<String, String, String> {

protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(BarGraphActivity.this);
pDialog.setMessage("Loading data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... String) {
//int i = 0;
//String[] amount = new String[];
try {
URL url = new URL(FETCH_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

int responsecode = urlConnection.getResponseCode();

if(responsecode == HttpURLConnection.HTTP_OK){
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null){
sb.append(line);
}
br.close();
return sb.toString();
}

} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*try{
do{
String [] values = new String[2];
Random r = new Random();
int visits = r.nextInt(10);

values[0] = Integer.toString(i);
values[1] = Integer.toString(visits);

publishProgress(values);
Thread.sleep(1000);
i++;
}while(i<=10);
}catch(Exception e){ }*/
return null;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pDialog.dismiss();
//
// showTransaction();
//System.out.println("Fuck" + amount);
try {
//get JSONObject from JSONArray of String
JSONArray result = new JSONArray(s);
JSONObject jsonObject = null;
String[] values = new String[2];
//loop through the array and break the JSONObject into String
for(int i = 0; i<result.length(); i++) {

jsonObject = result.getJSONObject(i);

amount = jsonObject.getString(TAG_AMOUNT);
desc = jsonObject.getString(TAG_DESC);
type = jsonObject.getString(TAG_TYPE);
date = jsonObject.getString(TAG_DATE);
category = jsonObject.getString(TAG_CATEGORY);

//int amt = Integer.parseInt(amount);
//int date = Integer.parseInt(date);

//values[0] = Integer.toString(amt);
//values[1] = Integer.toString(date);

System.out.println(jsonObject);
// System.out.println(type);
//System.out.println(category);

//transactionSeries.add(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
//mChart.repaint();


}

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

}
}


Here is how my data is modeled:

{"type":"income","amount":"651.52","desc":"pariatur laborum et","category":"want","date":"Sunday, July 24, 2016 12:17 AM"}
{"type":"expense","amount":"812.37","desc":"laboris eu ullamco","category":"want","date":"Saturday, April 9, 2016 3:13 PM"}

Answer

here the method "setupChart()", is calling before getting values from webservice via asynctask "new ChartTask().execute()". So, there is not getting values to plot the chart, or can say that values getting after ploting.

Solution: Please call the method "setupChart()" in asynctask onpostexecute.