1

我正在使用 achartEngine 在我的 android 应用程序中绘制图形。我在代码中设置数据。现在我需要从 mySQL 数据库中获取数据并将其显示在我的图形中。我正在使用 PHP Web 服务和 JSOn 解析器。

4

3 回答 3

2

您可以编写一些桥接代码,这些代码将从您的数据库中获取数据并填充 AChartEngine 数据集。

于 2012-08-15T07:15:13.110 回答
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);

如果您想制作多个类别(即某些年份的每月销售额,请执行此操作;

// 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());
}

希望这可以帮助!

于 2012-08-31T05:05:49.870 回答
0

我可以使用以下代码获取数据并将其显示在列表视图中:{ `

公共类 Linedbconn 扩展 Activity {

String id,abscisses,ordonnees;

// 进度对话框 private 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();
    }
}

}

但是当我想将这些值放在一个字符串表中以在我的图表中使用它们时,它不起作用`

于 2012-08-15T08:58:47.323 回答