我正在使用achartEngine在我的Android应用程序中绘制线图。我在代码中设置数据。现在我需要从JSON获取数据并将其显示在我的图形中,但我不知道如何将我的JSON与achartengine连接并将其显示在线图中。
这是我错误的源代码:
解析json
public class ErizaChartEngine extends Activity {
List NabList = new ArrayList();
boolean statuskoneksi= true;
private ProgressDialog Dialog;
protected Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LineChartAsyncTask().execute();
setContentView(R.layout.layoutchart);
}
public class LineChartAsyncTask extends AsyncTask <String, String, String>{
@Override
protected void onPreExecute (){
super.onPreExecute();
Dialog=new ProgressDialog(ErizaChartEngine.this);
Dialog.setMessage("Mohon Tunggu Sebentar...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
@Override
protected String doInBackground(String... params) {
String url= "http://www.ab.com/NabChart.htm?id=03&nilai=10";
try {
JSONParser jp= new JSONParser();
JSONArray ja= jp.takeJson(url);
for (int i=0; i<ja.length();i++){
JSONObject jo = ja.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
if (jo.has("lnu_nilai"))
map.put("lnu_nilai", jo.get("lnu_nilai").toString());
if (jo.has("tanggal"))
map.put("tanggal", jo.get("tanggal").toString());
NabList.add(map);
System.out.println("json parser done");
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
Dialog.dismiss();
}
这是我绘制图形的课程:
public void drawNABContentSimpleChart
(
String strtanggal,
String strNilaiNABHMin0)
{
XYSeries nabseries = new XYSeries("nab");
for (int i=0;i<NabList.size();i++){
nabseries.add(strNilaiNABHMin0[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(nabseries);
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.WHITE);
incomeRenderer.setPointStyle(PointStyle.CIRCLE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("NAB 1 year");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Nilai");
multiRenderer.setZoomButtonsVisible(true);
for(int i=0;i<NabList.size();i++){
multiRenderer.addXTextLabel(strtanggal[i]);
}
multiRenderer.addSeriesRenderer(incomeRenderer);
// multiRenderer.addSeriesRenderer(expenseRenderer);
// Creating an intent to plot line chart using dataset and multipleRenderer
Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
// Start Activity
startActivity(intent);
}
我真的需要帮助来解决我的问题,因为我的代码仍然错误,我不知道如何解决它。有没有人可以帮我解决这个问题?
答案 0 :(得分:0)
只需初始化图表内容(数据集,渲染器)并开始解析您的json。将从文件中读取的每个数据项添加到数据集中的系列中。完成后,显示图表。