我正在创建一个应用程序以从Web服务获取Json数据并将其绑定到条形图。
我不知道该怎么做。我正在使用MPAndroid
库来制作图表。
帮我绑定JSON数据中的条形图数据。
网络服务:
[
{
"NAME": "601 GRIETA EN CUERPO",
"PERCENTAGE": "46"
},
{
"NAME": "77 ENFRIAMIENTO O DUNTING",
"PERCENTAGE": "18"
},
{
"NAME": "78 PRECALENTAMIENTO",
"PERCENTAGE": "18"
},
{
"NAME": "209 MAL MANEJO",
"PERCENTAGE": "9"
},
{
"NAME": "92 FUGA DE MANOMETRO",
"PERCENTAGE": "9"
}
]
BARCHART。的java
package com.example.saravanakumars.chart;
/**
* Created by saravanakumars on 9/18/2017.
*/
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
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;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class barchart extends AppCompatActivity {
BarChart chart ;
ArrayList<BarEntry> BARENTRY ;
ArrayList<String> BarEntryLabels ;
BarDataSet Bardataset ;
BarData BARDATA ;
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
private static String url = "http://113.193.30.155/MobileService/MobileService.asmx/GetSampleData";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bar_chart);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
// bar chart
chart = (BarChart) findViewById(R.id.barchart);
BARENTRY = new ArrayList<>();
BarEntryLabels = new ArrayList<String>();
AddValuesToBARENTRY();
AddValuesToBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY, "Projects");
BARDATA = new BarData(BarEntryLabels, Bardataset);
Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
chart.setData(BARDATA);
chart.animateY(3000);
}
});
// registerForContextMenu( txt1 );
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
menu.setHeaderTitle("Select The Action");
inflater.inflate( R.menu.popup_menu,menu );
}
@Override
public boolean onContextItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.one:
Toast.makeText(barchart.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.two:
Toast.makeText(barchart.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(barchart.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray array = new JSONArray(jsonStr);
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.get(i);
String NAME = object.getString("NAME");
String PERCENTAGE = object.getString("PERCENTAGE");
int minqty = Integer.parseInt(object.getString("PERCENTAGE"));
HashMap<String, String> contact = new HashMap<>();
BARENTRY.add(minqty);
contact.put("NAME", NAME);
contact.put("PERCENTAGE", PERCENTAGE);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
}
}
}
答案 0 :(得分:0)
使用gson从你的jSON创建对象:
YourModelClass yourModelClassObject = gson.fromJson(args[0].toString(), YourModelClass.class);
然后获取您的arrayList并将其提供给图表数据集 使用以下链接从您的JSON生成pojo http://www.jsonschema2pojo.org/
答案 1 :(得分:0)
作为替代方案,您可以使用wannacharts.com ..是一种api like服务,您可以在api的url中发布Json,响应是带有图表base64图像的json({ {3}}),或带有图表的png文件的url。
好的部分是你不必使用任何外部库!
是免费的,但之前您必须在图表设计器中注册并预先设置图表。