Android achartengine simple pie chart

2019-02-17 16:08发布

问题:

I'm following an example in this link and created a class as below

public class aChartExample {

    public Intent execute(Context context) {
        int[] colors = new int[] { Color.RED, Color.YELLOW, Color.BLUE };
        DefaultRenderer renderer = buildCategoryRenderer(colors);

        CategorySeries categorySeries = new CategorySeries("Vehicles Chart");
        categorySeries.add("cars ", 30);
        categorySeries.add("trucks", 20);
        categorySeries.add("bikes ", 60);

        return ChartFactory.getPieChartIntent(context, categorySeries, renderer, null);
    }

    protected DefaultRenderer buildCategoryRenderer(int[] colors) {
        DefaultRenderer renderer = new DefaultRenderer();
        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }
}

and I'm calling it when my app starts on my starting activity in the onCreate.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);

    Intent achartIntent = new aChartExample().execute(this);
    startActivity(achartIntent);
}

I then included the achartengine-0.6.0.jar in my project.

When I run the app it crashes on the startActivity line.

Not sure where to go from here.

回答1:

Did you add this below line in AndroidManifest.xml. If so can you post the error message, I tried the same example 2 days ago and worked for me.

<activity android:name="org.achartengine.GraphicalActivity"> 


回答2:

startActivity takes only activities. Not for all classes.



回答3:

final CategorySeries series = new CategorySeries("pie");

        for (int i = 0; i < Xaxes_value.size(); i++) {
            series.add(Xaxes_value.get(i) + "",
                    Long.parseLong(yaxes_value.get(i)));
        }


    List<Integer> colors = new ArrayList<Integer>();
    int[] colorr = { Color.parseColor("#56ADDB"),
            Color.parseColor("#F59331"), Color.parseColor("#73C83C"),
            Color.parseColor("#948D8B"), Color.parseColor("#E54273"),
            Color.parseColor("#FFD34E"), Color.parseColor("#2A86D6"),
            Color.parseColor("#00BFC0"), Color.parseColor("#95AC5F"),
            Color.parseColor("#FF8497") };

    for (int i = 0; i < yaxes_value.size(); i++) {
        colors.add(colorr[i]);

    }
    renderer = new DefaultRenderer();
    for (int color = 0; color < colors.size(); color++) {
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(colors.get(color));
        r.setDisplayBoundingPoints(false);
        // r.setDisplayChartValuesDistance(20);
        r.setDisplayChartValues(false);
        r.setChartValuesTextSize(18);
        r.setStroke(BasicStroke.SOLID);
        renderer.addSeriesRenderer(r);
    }
    // renderer.isInScroll();

    renderer.setZoomEnabled(false);
    renderer.setPanEnabled(false);
    renderer.setZoomButtonsVisible(false); // set zoom button in Graph
    renderer.setApplyBackgroundColor(false);
    renderer.setBackgroundColor(Color.WHITE); // set background color
    renderer.setChartTitle("Pie Chart");
    renderer.setLabelsColor(Color.BLACK);
     renderer.setMargins(new int[] { 20, 100, 20, 15 });
    renderer.setLabelsTextSize(20);
    renderer.setChartTitleTextSize((float) 30);
    renderer.setShowLabels(false);
    renderer.setLegendTextSize(25);
    renderer.setDisplayValues(true);
    renderer.setStartAngle(90);
    GraphicalView mChart = ChartFactory.getPieChartView(getApplicationContext(), series,
            renderer);

    layout_graph.addView(mChart);