注:柱状图和饼状图在同一界面显示
第一步:在xml里定义图表显示的布局,并导入achartengine.jar
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:orientation="vertical"
android:id="@+id/ltu">
<LinearLayout
android:id="@+id/charlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/pielayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
第二步:拿到需要展示的数据
第三步:构造饼图以及柱状图数据,并获取渲染器
/**
* 构造饼图数据
*/
private CategorySeries getDataSet() {
// 构造数据
CategorySeries pieSeries = new CategorySeries("各分数段比例");
if (ExamActivity.listExam.size() > 0) {
Map<Object, Object> map = (Map<Object, Object>) ExamActivity.listExam
.get(0);
listtubaio = (List<DasTorverhaeltnis>) map.get("scoreSection");
} else {
listtubaio = new ArrayList<DasTorverhaeltnis>();
}
DasTorverhaeltnis torverhaeltnis = new DasTorverhaeltnis();
for (int i = 0; i < listtubaio.size() - 1; i++) {
String style = listtubaio.get(i).getType();
String o = style.substring(5);
String percent = listtubaio.get(i).getPrecent();
String[] p = percent.split("%");
String d = p[0];
float f = Float.parseFloat(d);
percent6 = (int) f;
pieSeries.add(o, percent6);
}
return pieSeries;
}
/**
* 获取一个饼图渲染器
*/
private DefaultRenderer getPieRenderer() {
// 构造一个渲染器
DefaultRenderer renderer = new DefaultRenderer();
// 设置渲染器显示缩放按钮
// renderer.setZoomButtonsVisible(true);
// 设置渲染器允许放大缩小
// renderer.setZoomEnabled(false);
// 设置渲染器标题文字大小
renderer.setChartTitleTextSize(20);
renderer.setPanEnabled(false);
List<Object> colors = new ArrayList<Object>();
colors.add(Color.BLUE);
colors.add(Color.CYAN);
colors.add(Color.YELLOW);
colors.add(Color.BLACK);
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.BLUE);
colors.add(Color.CYAN);
colors.add(Color.YELLOW);
colors.add(Color.BLACK);
colors.add(Color.RED);
colors.add(Color.BLUE);
colors.add(Color.CYAN);
colors.add(Color.YELLOW);
colors.add(Color.BLACK);
colors.add(Color.RED);
colors.add(Color.BLUE);
colors.add(Color.BLUE);
for (int i = 0; i < listtubaio.size() - 1; i++) {
SimpleSeriesRenderer enderer = new SimpleSeriesRenderer();
enderer.setColor((Integer) colors.get(i));
renderer.addSeriesRenderer(enderer);
}
// renderer.addSeriesRenderer(Color.CYAN);
// 设置饼图文字字体大小和饼图标签字体大小
renderer.setLabelsTextSize(20);
renderer.setLegendTextSize(18);
// 消除锯齿
renderer.setAntialiasing(true);
// 设置背景颜色
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.WHITE);
// 设置线条颜色
renderer.setAxesColor(Color.WHITE);
return renderer;
}
Map<Integer, Integer> shuju = new HashMap<Integer, Integer>();
public XYMultipleSeriesDataset getDatacharSet() {
// 构造柱状数据
XYMultipleSeriesDataset barDataset = new XYMultipleSeriesDataset();
CategorySeries barSeries = new CategorySeries(ExamActivity.tuBName);
if (mpecent != null) {
for (int a = 0; a < mpecent.size(); a++) {
String p = (String) mpecent.get(a);
String[] s = p.split("%");
String d = s[0];
float f = Float.parseFloat(d);
int pe = (int) f;
shuju.put(a, pe);
}
}
if (mpecentduo != null) {
for (int b = 0; b < mpecentduo.size(); b++) {
String p = (String) mpecent.get(b);
String[] s = p.split("%");
String d = s[0];
float f = Float.parseFloat(d);
int pe = (int) f;
shuju.put(b + mpecent.size(), pe);
}
}
if (mpecentduopd != null) {
for (int c = 0; c < mpecentduopd.size(); c++) {
String p = (String) mpecent.get(c);
String[] s = p.split("%");
String d = s[0];
float f = Float.parseFloat(d);
int pe = (int) f;
shuju.put(c + mpecent.size() + mpecentduo.size(), pe);
}
}
if (shuju != null) {
for (int g = 0; g < shuju.size(); g++) {
barSeries.add(shuju.get(g));
}
}
barDataset.addSeries(barSeries.toXYSeries());
return barDataset;
}
/**
* 构造柱状渲染器
*
* @return
*/
Map<Integer, Object> shuju2 = new HashMap<Integer, Object>();
public XYMultipleSeriesRenderer getRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
// renderer.setChartTitle("题目正确率");
// // 设置标题的字体大小
// renderer.setChartTitleTextSize(16);
// renderer.setXTitle("事业部");
renderer.setYTitle("正确率(%)");
renderer.setAxesColor(Color.WHITE);
renderer.setLabelsColor(Color.WHITE);
// 设置X轴的最小数字和最大数字
renderer.setXAxisMin(0.5);
renderer.setXAxisMax(15.5);
// 设置Y轴的最小数字和最大数字
renderer.setYAxisMin(0);
renderer.setYAxisMax(120);
if (mpecent != null) {
for (int a = 0; a < mpecent.size(); a++) {
int r = a + 1;
shuju2.put(a, "单选" + r);
}
}
if (mpecentduo != null) {
for (int b = 0; b < mpecentduo.size(); b++) {
int a = b + 1;
shuju2.put(b + mpecent.size(), "多选" + a);
}
}
if (mpecentduopd != null) {
for (int c = 0; c < mpecentduopd.size(); c++) {
int u = c + 1;
shuju2.put(c + mpecent.size() + mpecentduo.size(), "判断" + u);
}
}
if (shuju2 != null) {
for (int y = 0; y < shuju2.size(); y++) {
renderer.addXTextLabel(y + 1, (String) shuju2.get(y));
}
}
double[] str = new double[] { 0, shuju2.size() + 10, 0, 50 };
renderer.setPanLimits(str);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setShowGrid(true);// 设置是否在图表中显示网格
// renderer.setZoomButtonsVisible(true);
// 设置渲染器允许放大缩小
renderer.setBarWidth((float) 20);
// renderer.setBarSpacing(3.9);
renderer.setZoomEnabled(true);
// 消除锯齿
renderer.setAntialiasing(true);
// 设置背景颜色
renderer.setApplyBackgroundColor(true);
renderer.setMarginsColor(Color.CYAN);
renderer.setBackgroundColor(Color.WHITE);
// 设置每条柱子的颜色
SimpleSeriesRenderer sr = new SimpleSeriesRenderer();
sr.setColor(Color.RED);
renderer.addSeriesRenderer(sr);
// 设置每个柱子上是否显示数值
renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
// X轴的近似坐标数 (这样不显示横坐标)
renderer.setXLabels(0);
// Y轴的近似坐标数
renderer.setYLabels(6);
// 刻度线与X轴坐标文字左侧对齐
renderer.setXLabelsAlign(Align.LEFT);
// Y轴与Y轴坐标文字左对齐
renderer.setYLabelsAlign(Align.LEFT);
// 允许左右拖动,但不允许上下拖动.
renderer.setPanEnabled(true, false);
// 柱子间宽度
renderer.setBarSpacing(0.0001f);
// 设置X,Y轴单位的字体大小
renderer.setAxisTitleTextSize(20);
renderer.setLegendTextSize(20);
return renderer;
}
第四步:在界面上展示出来
GraphicalView mChartView = ChartFactory.getBarChartView(this,
getDatacharSet(), getRenderer(), Type.DEFAULT);
barchart.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
GraphicalView pieView = ChartFactory.getPieChartView(this,
getDataSet(), getPieRenderer());
piechart.addView(pieView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));