0

我是 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;
    }
      }
4

1 回答 1

1

为了更好地控制图表视图,最好创建 aGraphicalView而不是为 a 创建意图GraphicalActivity

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

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

graphicalView.repaint()
于 2012-12-12T10:31:44.457 回答