android的数据报表显示
从图中,我们可以看出,绘制一个图表我们其实,我们只需要理解三个概念
1,ChartFactory ,传入XYMutilpleSeriesRenderer,XYMutilpleSeriesDataset,然后,我们只需用 getXXXChartIntent(Context context,XYMutilpleSeriesRenderer,XYMutilpleSeriesDataset,)方法就可以进行图表的显示
2,XYMutilpleSeriesRenderer 用于进行绘制的设置,添加的XYSeriesRender对象,用于定义绘制的点集合设置,注意数量要和XYMutilpleSeriesDataset,添加的XYseries一致!!!
3,XYMutilpleSeriesDataset 用于数据的存放,添加的XYseries对象,用于提供绘制的点集合的数据
step1:在这个网址下载http://code.google.com/p/achartengine/图表achartengine的jar包,上面包括开发API和源码例子
step2:在在android项目下创建一个libs目录,把红线部分的jar包拷贝进去,然后右键项目->properties->Add jars选择lib下面的jar包。
step3:在manifest.xml里面添加 <activity android:name="org.achartengine.GraphicalActivity" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lp.ecjtu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ShowExam2Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.achartengine.GraphicalActivity" />
</application>
</manifest>
java代码:
package com.lp.ecjtu;
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.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
public class ShowExam2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//1 构造显示渲染图
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
//2,进行显示
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
//2.1构建数据
Random r = new Random();
for(int i=0;i<2;i++){
XYSeries series = new XYSeries("text"+(i+1));
//填充数据
for(int k=0;k<10;k++){
//填写x,y的值
series.add(k, r.nextInt() % 100);
}
//需要绘制的点放进dataset中
dataset.addSeries(series);
}
//3. 对点的绘制进行设置
XYSeriesRenderer xyRenderer = new XYSeriesRenderer();
//3.1设置颜色
xyRenderer.setColor(Color.GREEN);
//3.2设置点的样式
xyRenderer.setPointStyle(PointStyle.SQUARE);
// 3.3, 将要绘制的点添加到坐标绘制中
renderer.addSeriesRenderer(xyRenderer);
//重复1-3的步骤绘制第二个系列点
xyRenderer = new XYSeriesRenderer();
xyRenderer.setColor(Color.RED);
xyRenderer.setPointStyle(PointStyle.SQUARE);
renderer.addSeriesRenderer(xyRenderer);
Intent intent = ChartFactory.getLineChartIntent(this, dataset, renderer);
startActivity(intent);
}
}
运行效果图: