如何刷新绘制图形的活动?

时间:2012-12-12 10:14:28

标签: android bar-chart achartengine

我是Android新手,我开发了以下代码,以显示前10个旅行目的地的条形图 我的问题是我需要刷新活动,以便每次刷新时都会绘制一个新图表。请发布我可以学习如何更新它的任何链接。我在互联网上搜索过但找不到合适的教程。

public class ChartActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
        setChartSettings(renderer);
        Intent intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
        startActivity(intent);
    }

    public void createIntent() 

     {



//    return ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
    }



    public XYMultipleSeriesRenderer getBarDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setMargins(new int[] {10, 20, 0, 0});
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.BLUE);
        renderer.addSeriesRenderer(r);
        r = new SimpleSeriesRenderer();
        r.setColor(Color.GREEN);
        renderer.addSeriesRenderer(r);
        return renderer;
      }

     private XYMultipleSeriesDataset getBarDemoDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 5;
        Random r = new Random();
        int SERIES_NR = 2;
        for (int i = 0; i < SERIES_NR; i++) {
          CategorySeries series = new CategorySeries("Top 10 Destinations " );
          for (int k = 0; k < nr; k++) {
            series.add(800 + r.nextInt() % 100);
          }
          dataset.addSeries(series.toXYSeries());
        }
        return dataset;
      }



    private void setChartSettings(XYMultipleSeriesRenderer renderer) {
        renderer.setChartTitle("Top 10 Destinations Chart");
        renderer.setXTitle("Travel Destinations Around the World");
        renderer.setYTitle("Number of Visitors");
        renderer.setXAxisMin(0);
        renderer.setXAxisMax(6);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(1000);
      }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_chart, menu);
        return true;
    }
      }

1 个答案:

答案 0 :(得分:1)

要更好地控制图表视图,最好创建GraphicalView而不是为GraphicalActivity创建意图:

GraphicalView graphicalView = getBarChartView(this, getBarDemoDataset(), renderer, Type.DEFAULT);

将此视图添加到您的活动的视图层次结构中。然后要显示新数据,首先修改数据集,然后使用以下方式安排重绘:

graphicalView.repaint()