图表引擎AChartEngine 一

时间:2023-03-08 18:09:44

图表引擎AChartEngine 一

MainActivity.java

package com.example.achartengine0;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type; import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.LinearLayout; public class MainActivity extends Activity {
private Context context;
private LinearLayout layout0, layout1, layout2, layout3;
private String chartName0, chartName1;
private GraphicalView lView, bView;
private int number; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
layout0 = (LinearLayout) findViewById(R.id.container0);
layout1 = (LinearLayout) findViewById(R.id.container1);
layout2 = (LinearLayout) findViewById(R.id.container2);
layout3 = (LinearLayout) findViewById(R.id.container3);
for (number = 0; number <= 2; number++)
initGraphicalView(number); } public void initGraphicalView(int number) {
String[] xlab = new String[] { "一月", "二月", "", "三月", "四月", "五月" };
int[] value = new int[] { 20, 30, 40, 30, 10, 20 };
ChartDrawing chartDrawing = new ChartDrawing("图" + number, "日期", "数值",
xlab);
chartDrawing.setXYSeries(value, "线" + number);
chartDrawing.setXYMultipleSeriesRenderer(
chartDrawing.setXYSeriesRender(), number);
switch (number) {
case 0:
lView = ChartFactory.getLineChartView(context,
chartDrawing.multiDate, chartDrawing.multiRenderer);
layout0.addView(lView);
break;
case 1:
lView = ChartFactory.getLineChartView(context,
chartDrawing.multiDate, chartDrawing.multiRenderer);
layout1.addView(lView);
break;
case 2:
lView = ChartFactory.getBarChartView(context,
chartDrawing.multiDate, chartDrawing.multiRenderer,
Type.DEFAULT);
layout2.addView(lView);
break;
} } }

AChartDrawing.java

package com.example.achartengine0;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer; import android.graphics.Color;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style; public class ChartDrawing {
private String chartName;
private String xTitle;
private String yTitle;
private String[] xLabels;
public XYMultipleSeriesDataset multiDate ;//需要在构造函数中初始化,不然main函数调用时是Null
public XYMultipleSeriesRenderer multiRenderer ;
public CategorySeries date; public ChartDrawing(String chartName,String xTitle, String yTitle, String[] xLabels ) {
super();
this.chartName=chartName;
this.xTitle = xTitle;
this.yTitle = yTitle;
this.xLabels = xLabels;
multiDate=new XYMultipleSeriesDataset();
multiRenderer=new XYMultipleSeriesRenderer();
date=new CategorySeries(yTitle);
} public void setXYSeries(int[] value,String lName){//设置点的值
XYSeries xySeries=new XYSeries(lName);//某条线的点线对象
for(int i=0;i<value.length;i++){
xySeries.add(i+1, value[i]);
}
multiDate.addSeries(xySeries);//把所有点添加到数据集
}
public XYSeriesRenderer setXYSeriesRender(){//点线的渲染,返回 点线渲染 对象
XYSeriesRenderer pointRender=new XYSeriesRenderer();
pointRender.setPointStyle(PointStyle.CIRCLE);
pointRender.setFillPoints(false);
pointRender.setLineWidth(2f);
pointRender.setColor(Color.RED);
pointRender.setDisplayChartValues(true); return pointRender;
}
public void setXYMultipleSeriesRenderer(XYSeriesRenderer pointRender,int number ){//坐标轴图形整体渲染。传入的参数是 点线渲染 对象
multiRenderer.setChartTitle(chartName);//设置图表标题
multiRenderer.setShowGrid(true);//显示网格
multiRenderer.setPointSize(3f);//设置点的大小 !
multiRenderer.setChartTitleTextSize(20);//设置图表标题文字的大小
multiRenderer.setLabelsTextSize(15);//设置标签的文字大小
multiRenderer.setXLabelsAlign(Align.CENTER);//刻度线与刻度标注之间的相对位置关系
multiRenderer.setPanEnabled(true, false);//允许左右拖动,但不允许上下拖动.
multiRenderer.setZoomEnabled(false);//设置允许放大缩小.
multiRenderer.setXTitle(xTitle);
multiRenderer.setYTitle(yTitle);
multiRenderer.setMarginsColor(Color.WHITE);//设置空白区(图表区域外)背景色, 默认是黑色 /**设置整体渲染中图表区域内背景色
* multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.argb(0, 220, 228, 234) );
multiRenderer.setMarginsColor(Color.argb(0, 220, 228, 234));
*/ switch(number){
case 0://最基本情形 折线图,设置Y轴坐标数10
multiRenderer.setYLabels(10);
multiRenderer.setZoomButtonsVisible(true);//显示放大缩小的功能栏
break;
case 1://自定义X轴坐标 折线图,不设置Y坐标数,隐藏Y轴! 遗留问题:自定义某个轴后,某个轴对应的网格背景就木有了。。
multiRenderer.setXLabels(0);//这样才能显示我们自定义的x轴
multiRenderer.setYLabels(0);
multiRenderer.setYTitle("");
setXLabels(xLabels);
break;
case 2://柱形图
multiRenderer.setBarSpacing(1.5);
break; } multiRenderer.addSeriesRenderer(pointRender);
}
public void setXLabels(String[] xl){
for(int i=0;i<xl.length;i++)
multiRenderer.addXTextLabel(i+1, xl[i]);
} }