重复的饼图数据

时间:2013-11-26 04:59:59

标签: java android achartengine popupwindow pie-chart

在我的Android应用程序中,我使用achartengine libraray绘制饼图。单击按钮时,图表将显示在弹出窗口中。图表数据是从sqlite数据库中检索的。这是我的代码。

btnpieChart.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (distributionSeries != null || defaultRenderer != null) {
            distributionSeries.clear();
            defaultRenderer.removeSeriesRenderer(seriesRenderer);
        }

        SelectDBAdapter selectDBAdapter = SelectDBAdapter
                .getDBAdapterInstance(getActivity());
        try {
            selectDBAdapter.openDataBase();
            chartDataMap = selectDBAdapter
                    .getPieChartData(strBusinessUnit,
                            currentPeriod, currentYear);
        } catch (Exception e) {
            selectDBAdapter.close();
            e.printStackTrace();
        } finally {
            selectDBAdapter.close();
        }
        System.out.println("chartDataMap === "+ chartDataMap);

        if (chartDataMap.size() > 0) {
            for (Map.Entry<String, Double> entry : chartDataMap.entrySet()) {
                lstBrandNames.add(entry.getKey());
                lstAchievedVals.add(entry.getValue());
            }

            ArrayList<Double> distribution = calc_Percentage(lstAchievedVals);
            System.out.println("distribution === " + distribution);
            lstBrandNames = set_lables(lstBrandNames, distribution);

            distributionSeries = new CategorySeries(
                    "Brands - Achievement Progress");
            for (int i = 0; i < distribution.size(); i++) {
                distributionSeries.add(lstBrandNames.get(i), distribution.get(i));
            }

            defaultRenderer = new DefaultRenderer();
            defaultRenderer.setApplyBackgroundColor(true);
            defaultRenderer.setBackgroundColor(Color.WHITE);
            defaultRenderer.setLabelsTextSize(25);
            //defaultRenderer.setPanEnabled(true, false);

            for (int i = 0; i < distribution.size(); i++) {

                seriesRenderer = new SimpleSeriesRenderer();
                Random rnd = new Random(); 
                int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
                seriesRenderer.setColor(color);
                seriesRenderer.setGradientEnabled(false);
                seriesRenderer.setDisplayChartValues(true);
                seriesRenderer.setShowLegendItem(false);

                defaultRenderer.addSeriesRenderer(seriesRenderer);
            }

            defaultRenderer.setLabelsColor(Color.BLACK);
            defaultRenderer.setChartTitle("Brands - Achievement Progress");
            defaultRenderer.setChartTitleTextSize(30);
            defaultRenderer.setZoomButtonsVisible(true);
            defaultRenderer.setShowLabels(true);

            mChartView = ChartFactory.getPieChartView(getActivity(), distributionSeries, defaultRenderer);
            View popupView = inflater.inflate(R.layout.pie_chart, null);

            PopupWindow popupWindow = new PopupWindow(popupView, 
                                   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            LinearLayout layout = (LinearLayout) popupView.findViewById(R.id.chart_container);
            layout.removeAllViews();  
            layout.addView(mChartView);

            popupWindow.setFocusable(true);

            popupWindow.setBackgroundDrawable(new BitmapDrawable());

            popupWindow.showAsDropDown(popupView, 50, 50);

        }
    }
});

问题是当我再次单击该按钮时,图表数据是重复的饼图数据。

我认为当从饼图返回时,distributionSeries和defaultRenderer不能正确清除。并且。当我从图表窗口回来时,logcat说:

11-26 05:15:33.163: W/InputEventReceiver(1431): Attempted to finish an input event but the input event receiver has already been disposed.

有谁可以帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

在我看来,您删除了系列中的项目,但是您的lstBrandNames不会执行相同操作,这会导致数据重复。