简体   繁体   中英

Android : Issue in creating pie chart in a fragment

In my Android application i am creating a pie chart using achartengine library. When click a button it takes data from sqlite database and draw a pie chart. This is my code segment.

btnpieChart.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    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);

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

                        DefaultRenderer defaultRenderer = new DefaultRenderer();
                        defaultRenderer.setApplyBackgroundColor(true);
                        defaultRenderer.setBackgroundColor(Color.WHITE);

                        for (int i = 0; i < distribution.size(); i++) {
                            SimpleSeriesRenderer 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.setLabelsTextSize(25);

                            defaultRenderer.addSeriesRenderer(seriesRenderer);
                        }

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

                        Intent intent = ChartFactory.getPieChartIntent(
                                getActivity(), distributionSeries,
                                defaultRenderer, "Dash Board");

                        getActivity().startActivity(intent);


                    }
                }
            });

This creates pie chart. But there is a problem. Let's say, first, I click the button then it creates pie chart. Then I back from the pie chart screen and click again the button, then it duplicates pie chart data.

I found something like this as a solution.

layout = new LinearLayout(getActivity());
mChartView = ChartFactory.getPieChartView(getActivity(), distributionSeries, defaultRenderer);
layout.removeAllViews();  
layout.addView(mChartView); getActivity().setContentView(layout);

I am doing all of these things in a fragment. I found that it's not a good thing of creating new layout within a fragment. And also when I use the above solution it creates pie chart once and cannot back from the pie chart screen.

I am very confused how can I solve this issue. I would be much appreciated if anyone please be so kind enough to explain what's going on here and how can I solve this issue.

Thanks a lot

Replace the fragment :

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace, "NewFragmentTag"); 
ft.commit(); 

and if you want to go back to the previous fragmnet,ensure you add the above transaction to the back stack, ie

ft.addToBackStack(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM