如何在achartengine折线图中显示刷新webservice值?

时间:2015-04-29 05:49:51

标签: android web-services achartengine

我有achartengine线图,我成功地在线图中显示来自json webservice的值。但问题是当我回调asynctask然后线图在x轴上显示旧值。不显示新值。以下是我的代码。

public void charDeclaration() {

    // For chart

    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    if (myChart == null) {

        initializeChartNew();

        myChart = ChartFactory.getLineChartView(this, dataset,
                multiRenderer);
        layout.addView(myChart);
    } else {
        myChart.repaint();
    }
}

@SuppressWarnings("deprecation")
private void initializeChartNew() {
    // Creating an XYSeries for Income
    fromTemp = new XYSeries("Temperature");

    for (int i = 0; i < temp_time.size(); i++) {
        fromTemp.add(i, Double.parseDouble(temp_details.get(i)));
        System.out.println("Temperature Details in Charts "
                + temp_details.get(i));
    }
    // Creating a dataset to hold each series
    dataset = new XYMultipleSeriesDataset();
    // Adding Income Series to the dataset
    dataset.addSeries(fromTemp);



    // Creating XYSeriesRenderer to customize incomeSeries
    from_tempRenderer = new XYSeriesRenderer();
    from_tempRenderer.setColor(Color.WHITE);
    from_tempRenderer.setPointStyle(PointStyle.CIRCLE);
    from_tempRenderer.setFillPoints(true);
    from_tempRenderer.setLineWidth(2);
    from_tempRenderer.setChartValuesTextSize(20);
    from_tempRenderer.setDisplayChartValues(true);

    // Creating XYSeriesRenderer to customize expenseSeries

    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setMargins(new int[] { 50, 50, 50, 50 });
    multiRenderer.setYLabelsAlign(Align.RIGHT);
    multiRenderer.setAxisTitleTextSize(20);
    multiRenderer.setChartTitleTextSize(20);
    multiRenderer.setLabelsTextSize(18);
    multiRenderer.setLegendTextSize(18);
    multiRenderer.setXLabels(0);

    for (int k = 0; k < temp_date.size(); k++) {
        multiRenderer.setChartTitle(temp_date.get(k));
        System.out
                .println("Temperature Date in Charts " + temp_date.get(k));
    }
    multiRenderer.setXTitle("Time");
    multiRenderer.setYTitle("Temperature");
    multiRenderer.setYLabelsPadding(5);
    multiRenderer.setZoomButtonsVisible(true);

    multiRenderer.setPointSize(5);

    for (int i = 0; i < temp_time.size(); i++) {
        multiRenderer.addXTextLabel(i, temp_time.get(i));

    }

    System.out.println("value of temp_details " + temp_details.size());

    multiRenderer.removeSeriesRenderer(from_tempRenderer);
    multiRenderer.addSeriesRenderer(from_tempRenderer);

}

Below is my AsyncTask

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity_New.this);
        pDialog.setMessage("Please Wait...");
        pDialog.setCancelable(false);
        pDialog.setIndeterminate(false);
        pDialog.setCanceledOnTouchOutside(false);
        pDialog.show();

        // building_segment_id="";
        /*
         * temp_details.clear(); date_time.clear(); temp_date.clear();
         * temp_time.clear();
         */

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            System.out
                    .println("Temp Building Seg ID" + building_segment_id);
            // String temperature_url =
            // "http://www.intelatrek.com:98/Service1.svc/TempratueList?BeconID=329%20King&Date=2015-04-09";
            String temp_url = "http://db.stat-systems.co.nz:81/Service1.svc/TempratureList?BeconID="
                    + building_segment_id + "&Date=2015-04-13";
            System.out.println("BlucatID in Asynctask " + blucat_id);
            System.out.println("building_segment_id in AsyncTask "
                    + building_segment_id);

            temp_details.clear();
            date_time.clear();
            temp_date.clear();
            temp_time.clear();

            JSONObject jObject = JSONParser.getJsonFromURL(temp_url);
            tempjsonflag = jObject.getString("Flag");
            JSONArray jArray = jObject.getJSONArray("TempTransList");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject job = jArray.getJSONObject(i);

                String BuildingId = job.getString("BuildingId");
                String BuildingSegmentId = job
                        .getString("BuildingSegmentId");
                // String Customer_ID = job.getString("Customer_ID");
                String DateTime = job.getString("DateTime");
                // String InnerLocationId =
                // job.getString("InnerLocationId");
                String Temp_Date = job.getString("TempDate");
                String Temp_Time = job.getString("TempTime");
                String Temperature = job.getString("Temperature");

                temp_details.add(Temperature);
                date_time.add(DateTime);
                temp_date.add(Temp_Date);
                temp_time.add(Temp_Time);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (pDialog != null) {
            pDialog.dismiss();
        }

        for (int i = 0; i < temp_details.size(); i++) {
            System.out
                    .println("Temperature Details " + temp_details.get(i));
            System.out.println("Temperature Date Time " + date_time.get(i));
            System.out.println("Temperature Temp Date " + temp_date.get(i));
            System.out.println("Temperature Temp Time " + temp_time.get(i));
        }
        System.out.println("Temp Details " + temp_details.size());
        System.out.println("Temp Date Time " + date_time.size());
        System.out.println("Temp Temp Date " + temp_date.size());
        System.out.println("Temp Temp Time " + temp_time.size());
        System.out.println("Temp JSON FLAG " + tempjsonflag);

        // if(tempjsonflag.equals("Success")){
        if (tempe_detail.toString().equals("")) {
            temp_textview_temperature.setVisibility(View.GONE);
            temp_textview_notfound.setVisibility(View.VISIBLE);
            imageview_degree.setVisibility(View.GONE);

            /*
             * LinearLayout layout = (LinearLayout)
             * findViewById(R.id.chart);
             */
            chart.setVisibility(View.GONE);
        } else {
            temp_textview_notfound.setVisibility(View.GONE);
            temp_textview_temperature.setVisibility(View.VISIBLE);

            temp_textview_temperature.setText(tempe_detail);
            temp_textview_temperature.setTextSize(70);
            imageview_degree.setVisibility(View.VISIBLE);
            // temp_textview_temperature.setLayoutParams(llp);
            /*
             * LinearLayout layout = (LinearLayout)
             * findViewById(R.id.chart);
             */
            chart.setVisibility(View.VISIBLE);

            charDeclaration();
        }

        if (temp_textview_temperature.getText().toString().equals("1")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("2")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("3")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("4")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("5")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("6")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("7")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("8")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else if (temp_textview_temperature.getText().toString()
                .equals("9")) {
            temp_textview_temperature.setPadding(25, 0, 0, 0);
        } else {
            temp_textview_temperature.setPadding(0, 0, 0, 0);
        }

        int dip = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, (float) 1, getResources()
                        .getDisplayMetrics());

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int widthPixels = dm.widthPixels;
        int heightPixels = dm.widthPixels;

        float scaleFactor = dm.density;

        float widthInches = widthPixels / scaleFactor;

        if (widthInches >= 720) {

            // 10" tablet resolutions
            if (tempe_detail.toString().equals("")) {
                temp_textview_temperature.setVisibility(View.GONE);
                temp_textview_notfound.setVisibility(View.VISIBLE);
                imageview_degree.setVisibility(View.GONE);
                // temp_textview_notfound.setText(60);

                chart.setVisibility(View.GONE);
            } else {
                temp_textview_notfound.setVisibility(View.GONE);
                temp_textview_temperature.setVisibility(View.VISIBLE);

                temp_textview_temperature.setText(tempe_detail);

                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    // Do some stuff
                    temp_textview_temperature.setTextSize(70);
                    imageview_degree.getLayoutParams().height = 30;
                    imageview_degree.getLayoutParams().width = 30;
                } else {
                    temp_textview_temperature.setTextSize(180);
                    imageview_degree.getLayoutParams().height = 50;
                    imageview_degree.getLayoutParams().width = 50;
                }

                imageview_degree.setVisibility(View.VISIBLE);

                // temp_textview_temperature.setLayoutParams(llp);

                chart.setVisibility(View.VISIBLE);
                charDeclaration();
            }



            if (temp_textview_temperature.getText().toString()
                    .matches("[0-9.]*")) {
                temp_textview_temperature.setPadding(0, 0, 0, 0);
            } else if (temp_textview_temperature.getText().length() == 3) {
                temp_textview_temperature.setPadding(20, 0, 0, 0);
            } else {
                temp_textview_temperature.setPadding(0, 0, 0, 0);
            }
        } else if (widthInches >= 600) {

            // 7" tablet resolutions
            if (tempe_detail.toString().equals("")) {
                temp_textview_temperature.setVisibility(View.GONE);
                temp_textview_notfound.setVisibility(View.VISIBLE);
                imageview_degree.setVisibility(View.GONE);


                chart.setVisibility(View.GONE);
            } else {
                temp_textview_notfound.setVisibility(View.GONE);
                temp_textview_temperature.setVisibility(View.VISIBLE);

                temp_textview_temperature.setText(tempe_detail);
                temp_textview_temperature.setTextSize(140);
                imageview_degree.setVisibility(View.VISIBLE);
                // temp_textview_temperature.setLayoutParams(llp);

                chart.setVisibility(View.VISIBLE);
                charDeclaration();
            }


            if (temp_textview_temperature.getText().toString()
                    .matches("[0-9.]*")) {
                temp_textview_temperature.setPadding(25, 0, 0, 0);
                if (temp_textview_temperature.getText().length() == 3) {


                    temp_textview_temperature.setPadding(0, 0, 0, 0);
                }
            }

            else {
                temp_textview_temperature.setPadding(0, 0, 0, 0);
            }
        }
        // }


    }

}

1 个答案:

答案 0 :(得分:1)

使用myChart.repaint();在日期更改后重绘您的图表,如果重绘不起作用,那么在重新绘制图表之前尝试使用removeAllViews()绘制图表的父级布局。

chartlayout.removeAllViews();