AndroidでAChartEngineの使い方:棒グラフ、バブルグラフ、折れ線グラフ

1.ファイル名:ABarChart.java

コード:
package com.anjoyo.achartengine;

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 android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class ABarChart {
public Intent execute(Context context) {

return ChartFactory.getBarChartIntent(context, getBarChartDataset(), getBarChartRenderer(), Type.DEFAULT);
}

private XYMultipleSeriesRenderer getBarChartRenderer() {
// TODO Auto-generated method stub
XYMultipleSeriesRenderer renderer=new XYMultipleSeriesRenderer();
SimpleSeriesRenderer r;
r=new SimpleSeriesRenderer();
r.setColor(Color.BLUE);
renderer.addSeriesRenderer(r);
r=new SimpleSeriesRenderer();
r.setColor(Color.GREEN);
renderer.addSeriesRenderer(r);
setBarChartRenderer(renderer);
return renderer;
}

private void setBarChartRenderer(XYMultipleSeriesRenderer renderer) {
// TODO Auto-generated method stub
renderer.setChartTitle(“棒グラフ");
renderer.setXTitle(“X Axis");
renderer.setYTitle(“Y Axis");
renderer.setXAxisMax(10.5);
renderer.setXAxisMin(0.5);
renderer.setYAxisMax(210);
renderer.setYAxisMin(0);
}

private XYMultipleSeriesDataset getBarChartDataset() {
// TODO Auto-generated method stub
XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
final int SERIES_NR=2;
final int NR=10;
Random random=new Random();
for (int i = 0; i < SERIES_NR; i++) {
CategorySeries series=new CategorySeries(“Bar Chart—“+(i+1));
for (int j = 0; j < NR; j++) {
series.add(100+random.nextInt(100));
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
}

2.ファイル名 ABubbleChart.java 

コード:

package com.anjoyo.achartengine;

import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class ABubbleChart {
private final int SERIES_NR = 3;
private final int NR = 20;

public Intent execute(Context context) {
return ChartFactory.getBubbleChartIntent(context,
getBubbleChartDataset(), getBubbleChartRenderer(),
“Bubble Chart Demo");
}

private XYMultipleSeriesRenderer getBubbleChartRenderer() {
// TODO Auto-generated method stub
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
int[] linearColor = { Color.RED, Color.GREEN, Color.BLUE };
XYSeriesRenderer r;
for (int i = 0; i < SERIES_NR; i++) {
// entry=new XYEntry<Integer, Integer>(key, value);
r = new XYSeriesRenderer();
r.setColor(linearColor[i]);
r.setPointStyle(PointStyle.POINT);
r.setLineWidth(2);
renderer.addSeriesRenderer(r);
}
setBarChartRenderer(renderer);
return renderer;
}

private XYMultipleSeriesDataset getBubbleChartDataset() {
// TODO Auto-generated method stub
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
for (int i = 0; i < SERIES_NR; i++) {
XYValueSeries series = new XYValueSeries(“Bubble Chart—"
+ (i + 1));
int temp = 10 * i;
int[][] data = getBasicData();
for (int j = 0; j < data.length; j++) {
if((j+1)%5 ==0){
int x = data[j][0];
int y = temp;
int value=data[j][1]+temp;
series.add(x, y, value);
}
}
dataset.addSeries(series);
}
return dataset;
}

private int[][] getBasicData() {
int[][] data = new int[20][2];
Random rand = new Random();
for (int i = 0; i < data.length; i++) {
data[i][0] = i;
data[i][1] = 20 + rand.nextInt(20) % 20;
}
return data;
}

private void setBarChartRenderer(XYMultipleSeriesRenderer renderer) {
// TODO Auto-generated method stub
renderer.setChartTitle(“Bubble Chart");
renderer.setXTitle(“X Axis");
renderer.setYTitle(“Y Axis");
renderer.setXAxisMax(20);
renderer.setXAxisMin(0);
renderer.setYAxisMax(30);
renderer.setYAxisMin(0);
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);
}
}

3.ファイル名:ALinearChart.java 

コード下記:

package com.anjoyo.achartengine;

import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class ALinearChart {
	private final int SERIES_NR=3;
	private final int NR=20;
    public Intent execute(Context context) {
		return ChartFactory.getLineChartIntent(context, getLinearChartDataset(), getLinearChartRenderer(), "Linear Chart Another");
	}

	private XYMultipleSeriesRenderer getLinearChartRenderer() {
		// TODO Auto-generated method stub
		int[] linearColor={Color.RED, Color.GREEN, Color.BLUE};
		int[] belowLinearColor={Color.YELLOW, Color.CYAN, Color.GRAY};
		XYMultipleSeriesRenderer renderer=new XYMultipleSeriesRenderer();
		XYSeriesRenderer tempRender;
		for (int i = 0; i < SERIES_NR; i++) {
			tempRender=new XYSeriesRenderer();
			tempRender.setColor(linearColor[i]);
//			tempRender.setFillBelowLine(true);
//			tempRender.setFillBelowLineColor(belowLinearColor[i]);
			tempRender.setPointStyle(PointStyle.POINT);
			tempRender.setLineWidth(2);
			renderer.addSeriesRenderer(tempRender);
		}
		setLinearChartRenderer(renderer);
		return renderer;
	}

	private void setLinearChartRenderer(XYMultipleSeriesRenderer renderer) {
		// TODO Auto-generated method stub
		renderer.setChartTitle("Three Linear Chart");
		renderer.setXAxisMax(20);
		renderer.setXAxisMin(0);
		renderer.setYAxisMax(60);
		renderer.setYAxisMin(0);
	}

	private XYMultipleSeriesDataset getLinearChartDataset() {
		// TODO Auto-generated method stub
		XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
		for (int i = 0; i < SERIES_NR; i++) {
			XYSeries series=new XYSeries("Stock Trend---"+(i+1));
			int temp=10*i;
			int[][] data=getBasicData();
			for (int j = 0; j < data.length; j++) {
				int x=data[j][0];
				int y=temp+data[j][1];
				series.add(x, y);
			}
			dataset.addSeries(series);
		}
		return dataset;
	}
	private int[][] getBasicData() {
		int[][] data=new int[20][2];
		Random rand=new Random();
		for (int i = 0; i < data.length; i++) {
			data[i][0]=i;
			data[i][1]=20+rand.nextInt(20)%20;
		}
		return data;
	}
}

Development

Posted by arkgame