2012-08-14 151 views
1

我使用achartEngine在我的android應用程序中繪製圖形。我正在設置代碼中的數據。現在我需要從mySQL數據庫獲取數據並將其顯示在我的圖形中。我正在使用PHP Web服務和JSOn解析器。如何將achartEngine連接到數據庫?

回答

2

您可以編寫一些橋接代碼,從數據庫中獲取數據並填充AChartEngine數據集。

0

我可以得到的數據,並使用此代碼列表視圖中顯示出來:{`

公共類Linedbconn延伸活動{

String id,abscisses,ordonnees; 

//進度對話框 私人ProgressDialog pDialog;

// JSON parser class 
JSONParser jsonParser = new JSONParser(); 

// single product url 
private static final String url_line_graph = "http://api.androidhive.info/application_connect/line_graph.php"; 




// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_LINE = "line"; 
private static final String TAG_ID = "id"; 
private static final String TAG_ABSCISSES = "abscisses"; 
private static final String TAG_ORDONNEES = "ordonnees"; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_linedbconn); 

    // getting product details from intent 
    Intent intent = getIntent(); 

    // getting product id (id) from intent 
    id = intent.getStringExtra(TAG_ID); 
    abscisses=intent.getStringExtra(TAG_ABSCISSES); 
    ordonnees=intent.getStringExtra(TAG_ORDONNEES); 

    // Getting complete product details in background thread 
    new GetLineDetails().execute(); 

} 

/** 
* Background Async Task to Get complete line details 
* */ 
class GetLineDetails extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Linedbconn.this); 
     pDialog.setMessage("Loading information. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    /** 
    * Getting information in background thread 
    * */ 
    protected String doInBackground(String... params) { 

     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 
       // Check for success tag 
       int success; 
       try { 
        // Building Parameters 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("id", id)); 

        // getting line details by making HTTP request 
        // Note that line details url will use GET request 
        JSONObject json = jsonParser.makeHttpRequest(
          url_line_graph, "GET", params); 

        // check your log for json response 
        Log.d("Single Line Details", json.toString()); 

        // json success tag 
        success = json.getInt(TAG_SUCCESS); 
        if (success == 1) { 
         // successfully received line details 
         JSONArray productObj = json 
           .getJSONArray(TAG_LINE); // JSON Array 

         // get first product object from JSON Array 
         JSONObject product = productObj.getJSONObject(0); 

         // line with this id found 
         // Edit Text 
         //txtName = (EditText) findViewById(R.id.inputName); 
         //txtPrice = (EditText) findViewById(R.id.inputPrice); 
         //txtDesc = (EditText) findViewById(R.id.inputDesc); 

         // display product data in EditText 
         //txtName.setText(product.getString(TAG_NAME)); 
         //txtPrice.setText(product.getString(TAG_PRICE)); 
         //txtDesc.setText(product.getString(TAG_DESCRIPTION)); 

         abscisses=(String)product.getString(TAG_ABSCISSES); 
         ordonnees=(String)product.getString(TAG_ORDONNEES); 

        }else{ 
         // product with id not found 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     return null; 
    } 


    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once got all details 
     pDialog.dismiss(); 
    } 
} 

}

但是,當我想要把這些值串在我的圖表使用它們的表不工作 `

0

那麼我所做的是;

// to display the values for a single category (ie. sales per month) 
//put the data into an array 
int[] y = new int[12]; 
for (int i = 0; i < 12; i++){ 
    y[i] = Integer.valueOf(sales); 
} 
//Create the Category series (ie. one value (sales) per month)   
CategorySeries series = new CategorySeries(""); 
for (int i = 0; i < y.length; i++) { 
    series.add(y[i]); 
} 

現在實例的數據集

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 
dataset.addSeries(series.toXYSeries()); 

創建要與圖表的類型呈現

// This is how the "Graph" itself will look like 
     XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 
     mRenderer.setChartTitleTextSize(15); 
     mRenderer.setLabelsColor(Color.BLUE); 
     mRenderer.setChartTitle("Sales per month"); 
     mRenderer.setMargins(new int[]{30, 30, 20, 20}); 
     mRenderer.setOrientation(Orientation.HORIZONTAL); 
etc.... 

,並最終使並顯示圖形

//make 
mChartView1 = ChartFactory.getBarChartView(this, dataset,mRenderer, null); 

//show 
RelativeLayout layout = (RelativeLayout)step.findViewById(R.id.barChart); 
layout.addView(mChartView1); 

如果你想做多tiple類別(即某些年份的每月銷售額,請執行此操作;

// to display the values for a several categories (ie. sales per month per year) 
//put the data into an array 

int[] y = new int[12]; 

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 
for (int year = 0; year < numberofYears; year++){ 
    CategorySeries series = new CategorySeries(""); 
    //get the data for the specific year then 
    //iterate through it values and add the data to the series 
    double[] v = values.get(year); 
    for (int i = 0; i < v.length; i++){ 
     series.add(v[i]); 
    } 
    dataset.addSeries(series.toXYSeries()); 
} 

希望這有助於!