AndroidでAChartEngineの使い方:円グラフ
ファイル名:APieChart.java
コード:
package com.anjoyo.achartengine;
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 APieChart {
private double[] values = { 25, 30, 45 };
public APieChart() {
// TODO Auto-generated constructor stub
}
public APieChart(double[] values) {
if (values.length == 3) {
for (int i = 0; i < values.length; i++) {
this.values[i] = values[i];
}
}
}
public Intent execute(Context context) {
int[] colors={Color.BLUE, Color.GREEN, Color.YELLOW};
DefaultRenderer renderer=buildCategoryRenderer(colors);
renderer.setAntialiasing(true);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.TRANSPARENT);
renderer.setShowAxes(true);//
renderer.setAxesColor(Color.GRAY);
renderer.setClickEnabled(true);
renderer.setZoomButtonsVisible(true);
renderer.setZoomEnabled(true);
renderer.setExternalZoomEnabled(true);
renderer.setLabelsColor(Color.GREEN);
renderer.setLabelsTextSize(16);
// renderer.setLegendHeight(50);
// renderer.setPanEnabled(true);
renderer.setStartAngle(180);
CategorySeries series=new CategorySeries(“Fruit Ratio");
series.add(“Apple", values[0]);
series.add(“Banana", values[1]);
series.add(“Orange", values[2]);
return ChartFactory.getPieChartIntent(context, series, renderer, “果物");
}
private DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer=new DefaultRenderer();
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.GRAY);
renderer.setLabelsTextSize(20);
renderer.setChartTitle(“円グラフ");
renderer.setChartTitleTextSize(20);
renderer.setLegendTextSize(20);
renderer.setLegendHeight(50);
for (int i = 0; i < colors.length; i++) {
int j = colors[i];
SimpleSeriesRenderer r=new SimpleSeriesRenderer();
r.setColor(j);
renderer.addSeriesRenderer(r);
}
return renderer;
}
}