[img]https://img-my.csdn.net/uploads/201206/08/1339140332_9510.jpg[/img]
[img]https://img-my.csdn.net/uploads/201206/08/1339140629_5237.jpg[/img]
[img]https://img-my.csdn.net/uploads/201206/08/1339140587_8411.jpg[/img]
package org.achartengine.chartdemo.demo.chart;
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
public class SalesStackedBarChart extends AbstractDemoChart {
public String getName() {
return "Sales stacked bar chart";
}
public String getDesc() {
return "The monthly sales for the last 2 years (stacked bar chart)";
}
public Intent execute(Context context) {
String[] titles = new String[] { "2008", "2007" };//圖例
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200, 22030, 21200, 19500, 15500,
12600, 14000 });//第一種柱子的數值
values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030, 11200, 9500, 10500,
11600, 13500 });//第二中柱子的數值
int[] colors = new int[] { Color.BLUE, Color.CYAN };//兩種柱子的顔色
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);//調用AbstractDemoChart中的方法建構renderer.
setChartSettings(renderer, "Monthly sales in the last 2 years", "Month", "Units sold", 0.5,
12.5, 0, 24000, Color.GRAY, Color.LTGRAY);//調用AbstractDemoChart中的方法設定renderer的一些屬性.
renderer.getSeriesRendererAt(0).setDisplayChartValues(true);//設定柱子上是否顯示數量值
renderer.getSeriesRendererAt(1).setDisplayChartValues(true);//設定柱子上是否顯示數量值
renderer.setXLabels(12);//X軸的近似坐标數
renderer.setYLabels(5);//Y軸的近似坐标數
renderer.setXLabelsAlign(Align.LEFT);//刻度線與X軸坐标文字左側對齊
renderer.setYLabelsAlign(Align.LEFT);//Y軸與Y軸坐标文字左對齊
renderer.setPanEnabled(true, false);//允許左右拖動,但不允許上下拖動.
// renderer.setZoomEnabled(false);
renderer.setZoomRate(1.1f);//放大的倍率
renderer.setBarSpacing(0.5f);//柱子間寬度
return ChartFactory.getBarChartIntent(context, buildBarDataset(titles, values), renderer,
Type.STACKED);//建構Intent, buildBarDataset是調用AbstractDemochart中的方法.
}
}