Achartengine的饼图不会从重绘功能刷新

时间:2011-07-17 09:46:42

标签: java android charts pie-chart achartengine

我正在构建一个应用程序,根据精确的日期间隔从数据库制作饼图。不幸的是,当我踩着按钮时,achartengine的重绘功能无效。 当Activity开始时一切都很好,图表没问题但是当我使用按钮时没有任何反应。

我希望有人可以帮助我。

这是代码:

private GraphicalView chartView;

...

 tabHost.addTab(tabHost.newTabSpec("").setIndicator("Grafikon")
            .setContent(new TabHost.TabContentFactory() {
                public View createTabContent(String arg0) {
                    final View view = LayoutInflater.from(SumMenuActivity.this).inflate(
                            R.layout.summenu_tab3, null);

                    periodSeekBar = (SeekBar) view.findViewById(R.id.summenutab3_periodslide);
                    final TextView periodName = (TextView) view
                            .findViewById(R.id.summenutab3_periodtw);

                    if (chartView == null) {
                        LinearLayout layout = (LinearLayout) view.findViewById(R.id.chart);
                        intValChanger(intVal, 0);
                        layout.addView(chartView);
                    } else {                           
                        chartView.repaint();
                    }

                    periodSeekBar
                            .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                                public void onProgressChanged(SeekBar seekBar, int progress,
                                        boolean fromUser) {
                                    if (progress == 0) {
                                        periodName.setText("Heti");
                                    } else if (progress == 1) {
                                        periodName.setText("Havi");
                                    } else if (progress == 2) {
                                        periodName.setText("Eves");
                                    }

                                    intVal = 0;
                                    intValChanger(intVal, progress);
                                }

                                public void onStartTrackingTouch(SeekBar seekBar) {
                                }

                                public void onStopTrackingTouch(SeekBar seekBar) {
                                }
                            });

                    final View minusButton = view.findViewById(R.id.summenutab3_frombutton);
                    final View plusButton = view.findViewById(R.id.summenutab3_tobutton);

                    minusButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            intVal--;
                            intValChanger(intVal, periodSeekBar.getProgress());
                            if (chartView != null) {
                                chartView.refreshDrawableState(); 
                                chartView.repaint();
                            }

                        }
                    });

                    plusButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            intVal++;
                            intValChanger(intVal, periodSeekBar.getProgress());
                            if (chartView != null) {
                                chartView.refreshDrawableState(); 
                                chartView.repaint();
                            }

                        }
                    });

                    return view;
                }

...

 public void intValChanger(int intVal, int type) {

    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();

    if (type == 0) {

        start.setFirstDayOfWeek(Calendar.MONDAY);
        end.setFirstDayOfWeek(Calendar.MONDAY);
        int currentStartOfWeek = (start.get(Calendar.DAY_OF_WEEK) + 7 - start
                .getFirstDayOfWeek()) % 7;
        int currentEndOfWeek = (end.get(Calendar.DAY_OF_WEEK) + 7 - end.getFirstDayOfWeek()) % 7;
        start.add(Calendar.DAY_OF_YEAR, -currentStartOfWeek + intVal * 7);
        end.add(Calendar.DAY_OF_YEAR, (-currentEndOfWeek + intVal * 7) + 6);

    } else if (type == 1) {

        start.set(Calendar.DATE, 1);
        start.set(Calendar.MONTH, start.get(Calendar.MONTH) + 1 * intVal);
        end.set(Calendar.DATE, start.getActualMaximum(Calendar.DATE));
        end.set(Calendar.MONTH, end.get(Calendar.MONTH) + 1 * intVal);

    } else if (type == 2) {

        start.set(Calendar.YEAR, start.get(Calendar.YEAR) + 1 * intVal);
        start.set(Calendar.MONTH, 0);
        start.set(Calendar.DATE, 1);
        end.set(Calendar.YEAR, end.get(Calendar.YEAR) + 1 * intVal);
        end.set(Calendar.MONTH, 11);
        end.set(Calendar.DATE, 31);

    }

    if (tabHost.getCurrentTab() == 0) {
        populateTimeList(start.getTime(), end.getTime());
    } else if (tabHost.getCurrentTab() == 1) {
        populateSumArrayList(start.getTime(), end.getTime());
    } else if (tabHost.getCurrentTab() == 2) {
        populateChart(start.getTime(), end.getTime());
    }

    Toast.makeText(getApplicationContext(),
            android.text.format.DateFormat.format("yyyy-MM-dd", start) +
                    "-tól " + android.text.format.DateFormat.format("yyyy-MM-dd", end)
                    + "-ig", 3).show();
}

public GraphicalView executeChart(Context context, ArrayList<String> name,
        ArrayList<Integer> value) {

    ArrayList<Integer> colors = new ArrayList<Integer>();

    CategorySeries categorySeries = new CategorySeries("Torta diagram");
    for (int i = 0; i < name.size(); i++) {
        Random r = new Random();
        categorySeries.add(name.get(i), value.get(i));
        colors.add(Color.rgb(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
    }

    DefaultRenderer renderer = buildCategoryRenderer(colors);
    renderer.setLabelsTextSize(14);
    GraphicalView gview = ChartFactory.getPieChartView(context,
            categorySeries, renderer);
    return gview;
}

protected DefaultRenderer buildCategoryRenderer(ArrayList<Integer> colors) {
    DefaultRenderer renderer = new DefaultRenderer();
    for (int color : colors) {
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(color);
        renderer.addSeriesRenderer(r);
    }
    return renderer;
}

    public void populateChart(Date from, Date to) {
    ArrayList<String> tmpName = new ArrayList<String>();
    ArrayList<Integer> tmpValue = new ArrayList<Integer>();

    ArrayList<Category> tmpCategory = getCategories();
    ArrayList<Transaction> tmpTransaction = new ArrayList<Transaction>();

    for (int i = 0; i < tmpCategory.size(); i++) {
        tmpName.add(tmpCategory.get(i).getName());
        tmpTransaction = (ArrayList<Transaction>) getTransactionsByTimeIntvalAndId(from, to,
                tmpCategory.get(i).getId());
        int ammount = 0;
        for (int j = 0; j < tmpTransaction.size(); j++) {
            ammount += tmpTransaction.get(j).getAmmount();
        }
        tmpValue.add(ammount);
    }

    for (int i = 0; i < tmpName.size(); i++) {
        Log.d("DEBUG MESSAGE", tmpName.get(i) + ": " + tmpValue.get(i));

    }

    chartView = null;
    chartView = executeChart(SumMenuActivity.this, tmpName, tmpValue);

    Log.d("DEBUG MESSAGE", "megtörtént a repaint!" + chartView);
}

1 个答案:

答案 0 :(得分:0)

我没有使用过Achart,但这是一个简单的饼图实现。您可以参考演示,它可能会有所帮助。 在更新值后的此演示中,您需要调用invalidate()来重绘饼图。

https://github.com/blessenm/PieChartDemo