标签:
首先要下载acharengine的包,里面重要的有lib和一些简易的工具,等下我附在文件夹里,而这些包都必须调用的。
然后以下附上主要的作图代码:
package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.List; import org.achartengine.ChartFactory; import org.achartengine.chart.PointStyle; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.graphics.Paint.Align; public class AverageTemperatureChart extends AbstractDemoChart { public String getName() { return "Average temperature"; } public String getDesc() { return "The average temperature in 4 Greek islands (line chart)"; } public Intent execute(Context context) { String[] titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" };//图例 List<double[]> x = new ArrayList<double[]>(); for (int i = 0; i < titles.length; i++) { x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });//每一个序列中点的X坐标 } List<double[]> values = new ArrayList<double[]>(); values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9 });//序列1中点的y坐标 values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });//序列2中点的Y坐标 values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });//序列3中点的Y坐标 values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });//序列4中点的Y坐标 int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW };//每一个序列的颜色设置 PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE };//每一个序列中点的形状设置 XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);//调用AbstractDemoChart中的方法设置renderer. int length = renderer.getSeriesRendererCount(); for (int i = 0; i < length; i++) { ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);//设置图上的点为实心 } setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY);//调用AbstractDemoChart中的方法设置图表的renderer属性. renderer.setXLabels(12);//设置x轴显示12个点,依据setChartSettings的最大值和最小值自己主动计算点的间隔 renderer.setYLabels(10);//设置y轴显示10个点,依据setChartSettings的最大值和最小值自己主动计算点的间隔 renderer.setShowGrid(true);//是否显示网格 renderer.setXLabelsAlign(Align.RIGHT);//刻度线与刻度标注之间的相对位置关系 renderer.setYLabelsAlign(Align.CENTER);//刻度线与刻度标注之间的相对位置关系 renderer.setZoomButtonsVisible(true);//是否显示放大缩小button renderer.setPanLimits(new double[] { -10, 20, -10, 40 }); //设置拖动时X轴Y轴同意的最大值最小值. renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });//设置放大缩小时X轴Y轴同意的最大最小值. Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values), renderer, "Average temperature111");//构建Intent return intent; } }
有一点要注意,上面这个返回的是intent,也就是说必须有个主activity来作为传承。以下附上我的主activity:
package com.example.acharengine; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button btn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AverageTemperatureChart intent = new AverageTemperatureChart(); Intent mIntent = intent.execute(MainActivity.this); startActivity(mIntent); } }); } }
至于XML仅仅要有个button跳过去即可:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="33dp" android:layout_marginTop="56dp" android:layout_toRightOf="@+id/textView1" android:text="Button" /> </RelativeLayout>
再次强调,两个重要的包,一个是lib,一个是package org.achartengine.chartdemo.demo.chart;都必须调进去。
别忘了在manifest中增加 <activity android:name="org.achartengine.GraphicalActivity" />这个activity。
下载地址:http://download.csdn.net/detail/modiziri/8179119
同类文章參考网址:http://www.apkway.com/thread-6363-1-1.html
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4466959.html