0

每次我选择一个 m 适配器时,我都想重新绘制我的图表,第一次绘制的图表看起来很完美,但是当我去选择一个并回来时,它看起来像绘制了两次相同的图表。

这是我的源代码:

  public class MainActivity extends Activity {

    private Button btnConnect;
    private Spinner spnTime;

    private String[] durationData = new String[]{
        "Pilih Durasi",
        "1 Minggu", 
        "1 Bulan", 
        "3 Bulan",
        "9 Bulan", 
        "12 Bulan"
    };

    int period = 0;

    private LinearLayout lnChart;

    private ProgressDialog dialog;

    public String baseURL = "http://www.abcj.com/i=";

    private ArrayList<DataItem> listData;


    //CHART VARIABLES

    public static final String TYPE = "type";

    private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

    private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

    private XYSeries mCurrentSeries;

    private XYSeriesRenderer mCurrentRenderer;

    private String mDateFormat;

    private GraphicalView mChartView;

    private int index = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnConnect = (Button)findViewById(R.id.btnDownloadChartData);
        spnTime = (Spinner)findViewById(R.id.spOptDuration);

        listData = new ArrayList<DataItem>();

        //CHART INITIATION

        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
        mRenderer.setAxisTitleTextSize(16);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setLabelsTextSize(15);
        mRenderer.setLegendTextSize(15);
        mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setPointSize(10);

        btnConnect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_dropdown_item_1line, durationData);

        spnTime.setAdapter(adapter);

        spnTime.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                switch (arg2+1) {
                    case 0:
                    break;
                    case 1:
                        period = 7;
                    break;

                    case 2:
                        period = 30;
                    break;

                    case 3:
                        period = 90;
                    break;

                    case 4:
                        period = 180;
                    break;

                    case 5:
                        period = 360;
                    break;
                default:
                    break;
                }

                new LoadData(baseURL+period).execute();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        }
}); 
    }

    @SuppressLint("ShowToast")
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        if (mChartView == null) {
             lnChart = (LinearLayout) findViewById(R.id.chart);
              mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
              mRenderer.setClickEnabled(true);
              mRenderer.setSelectableBuffer(100);
              mChartView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
                  double[] xy = mChartView.toRealPoint(0);
                  if (seriesSelection == null) {
                    Toast.makeText(MainActivity.this, "No chart element was clicked", Toast.LENGTH_SHORT)
                        .show();
                  } 
                }
              });
              mChartView.setOnLongClickListener(new View.OnLongClickListener() {
                @SuppressLint("ShowToast")
                @Override
                public boolean onLongClick(View v) {
                  SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
                  if (seriesSelection == null) {
                    Toast.makeText(MainActivity.this, "No chart element was long pressed",
                        Toast.LENGTH_SHORT);
                    return false; // no chart element was long pressed, so let something
                    // else handle the event
                  } else {
                    Toast.makeText(MainActivity.this, "Chart element in series index "
                        + seriesSelection.getSeriesIndex() + " data point index "
                        + seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
                    return true; // the element was long pressed - the event has been
                    // handled
                  }
                }
              });
              mChartView.addZoomListener(new ZoomListener() {
                public void zoomApplied(ZoomEvent e) {
                  String type = "out";
                  if (e.isZoomIn()) {
                    type = "in";
                  }
                  System.out.println("Zoom " + type + " rate " + e.getZoomRate());
                }

                public void zoomReset() {
                  System.out.println("Reset");
                }
              }, true, true);
              mChartView.addPanListener(new PanListener() {
                public void panApplied() {
                  System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax()
                      + "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]");
                }
              });
              lnChart.removeAllViews();
              lnChart.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
                  LayoutParams.FILL_PARENT));
            } else {
              mChartView.repaint();
            }
    }

    private class LoadData extends AsyncTask<Void, Void, String>{

        String url = "";

        public LoadData(String url) {
            // TODO Auto-generated constructor stub
            this.url = url;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            dialog = ProgressDialog.show(MainActivity.this, "", "Please wait");
        }

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String result = "";
            try {
                result = Connection.get(url);
            } catch (Exception e) {
                // TODO: handle exception
                result = "";
                Log.d("TEST CHART", e.getMessage());
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            fetchResponse(result.replace("\n", "").trim());

            dialog.dismiss();
        }

    }

    private void fetchResponse(String result) {
        // TODO Auto-generated method stub
        if (!result.equals("")) {
            try {
                JSONArray jsonArray = new JSONArray(result);
                DataItem item = null;

                for (int i = 1; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    item = new DataItem(jsonObject.optString("lji_invest"), 
                            jsonObject.optDouble("persen_hmin1"), 
                            jsonObject.optDouble("lnu_nilai"), 
                            jsonObject.optDouble("selisih"), 
                            jsonObject.optString("lji_id"), 
                            jsonObject.optString("tanggal"));

                    listData.add(item);
                }

                renderChart(listData);

            } catch (Exception e) {
                Log.d("TEST CHART", e.getMessage());
            }
        }   
    }

    private void renderChart(ArrayList<DataItem> listData) {
        // TODO Auto-generated method stub

        String seriesTitle = "Series Data";
        XYSeries series = new XYSeries(seriesTitle);
        mDataset.addSeries(series);
        mCurrentSeries = series;
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        mRenderer.addSeriesRenderer(renderer);

        renderer.setFillPoints(true);

        mCurrentRenderer = renderer;

        for (int i = 0; i < listData.size(); i++) {
            mCurrentSeries.add(listData.get(i).getInuNilai(), 
                    listData.get(i).getPersenHmint1());
        }

        if (mChartView != null) {
              mChartView.repaint();
            }

    }

    @Override
      protected void onRestoreInstanceState(Bundle savedState) {
        super.onRestoreInstanceState(savedState);
        mDataset = (XYMultipleSeriesDataset) savedState.getSerializable("dataset");
        mRenderer = (XYMultipleSeriesRenderer) savedState.getSerializable("renderer");
        mCurrentSeries = (XYSeries) savedState.getSerializable("current_series");
        mCurrentRenderer = (XYSeriesRenderer) savedState.getSerializable("current_renderer");
        mDateFormat = savedState.getString("date_format");
      }

      @Override
      protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("dataset", mDataset);
        outState.putSerializable("renderer", mRenderer);
        outState.putSerializable("current_series", mCurrentSeries);
        outState.putSerializable("current_renderer", mCurrentRenderer);
        outState.putString("date_format", mDateFormat);
      }

那么如何重新绘制我的图表?我希望有人能告诉我我的错在哪里,并告诉我解决方法

4

3 回答 3

1

使用 chartView.removeView(Graphical_View)

在使用新值设置图表视图之前,然后使用“Graphical_View.repaint()”为布局设置新值

但是使用 multirenderer.setInScroll(true);

于 2013-09-24T12:42:47.310 回答
0

很难理解这个问题,但我会尝试猜测。

为了重新绘制图表,您需要调用chartView.repaint();

但是,您遇到的问题可以通过mRenderer.setInScroll(true);拨打电话来解决。

于 2013-05-21T08:33:48.500 回答
0

我用这段代码解决了这个问题

if(layout != null)
       layout.removeAllViews();     
layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));          
    mChartView = null;
于 2013-07-20T10:59:00.323 回答