我正在开发一个应用程序,我必须显示饼图,我正在将 achartengine 库导入我的应用程序。achartengine 的演示版工作正常,它显示图表。但是当我在我的应用程序中使用它时,什么都不会显示。任何人都可以帮我解决这个问题。
这是我的代码:
主要活动:
public class HomeScreenPage extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreenpage);
PieChart achartIntent = new PieChart();
achartIntent.execute(this);
}
饼图类:
import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
public class PieChart {
public Intent execute(Context context) {
double[] values = new double[] {12, 14, 11, 10,2};
int[] colors = new int[] {Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN};
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setLabelsTextSize(10);
return ChartFactory.getPieChartIntent(context, buildCategoryDataset("Project budget", values), renderer);
}
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;
}
protected CategorySeries buildCategoryDataset(String title, double[] values) {
CategorySeries series = new CategorySeries(title);
int k = 0;
for (double value : values) {
series.add("Project " + ++k, value);
}
return series;
}
}