0

我是安卓新手。我正在使用 achartengine 创建条形图。我无法绘制条形图。你能告诉我我在这里做错了什么吗?我使用 sqlite 数据浏览器创建了一个数据库,并将数据库复制到我项目的 assets 文件夹中。

我编写了一个代码来绘制带有静态值的条形图,现在我修改它以从数据库中获取值。

package flu.solutions.travelsense;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;  
import android.view.Menu;
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 Intent createIntent() 

     {

    String[] titles = new String[] { "Top 10 Destinations", " " };
    List<double[]> values = new ArrayList<double[]>();
    values.add(new double[] { 8, 6, 4, 6, 7, 7, 9, 5, 7, 8, 8 });
    values.add(new double[] {});

    int[] colors = new int[] { Color.RED, Color.BLACK};

    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
    renderer.setOrientation(Orientation.HORIZONTAL);

    setChartSettings(renderer);

    renderer.setXLabels(1);
    renderer.setYLabels(10);

    renderer.addXTextLabel(1, "Bangalore");
    renderer.addXTextLabel(2, "Mysore");
    renderer.addXTextLabel(3, "Chennai");
    renderer.addXTextLabel(4, "Delhi");
    renderer.addXTextLabel(5, "Kolkatta");
    renderer.addXTextLabel(6, "Kashmir");
    renderer.addXTextLabel(7, "Hyderabad");
    renderer.addXTextLabel(8, "Mumbai");
    renderer.addXTextLabel(9, "Kerala");
    renderer.addXTextLabel(10, "Gujarat");

    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) 
    {
      SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
      seriesRenderer.setDisplayChartValues(false);
      }

    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[] {20, 30, 15, 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 = 10;
        Random r = new Random();
        int SERIES_NR = 2;
        for (int i = 0; i < SERIES_NR; i++) {
          CategorySeries series = new CategorySeries("Demo series " + (i + 1));
          for (int k = 0; k < nr; k++) {
            series.add(100 + r.nextInt() % 100);
          }
          dataset.addSeries(series.toXYSeries());
        }
        return dataset;
      }



    private void setChartSettings(XYMultipleSeriesRenderer renderer) {
        renderer.setChartTitle("Chart demo");
        renderer.setXTitle("x values");
        renderer.setYTitle("y values");
        renderer.setXAxisMin(0);
        renderer.setXAxisMax(5);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(250);
      }
    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

您的createIntent()方法甚至看起来都没有被引用。我相信正确的OnCreate是:

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

    Intent intent = createIntent();
    startActivity(intent);

}

但是您应该知道,您最终会希望将图形加载为视图,而不是意图获得所需的实时功能。

于 2012-12-11T09:37:42.520 回答