用acharengine作Android圖表

來源:互聯網
上載者:User

標籤:

首先要下載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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubAverageTemperatureChart 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




用acharengine作Android圖表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.