当前位置: 首页 > 工具软件 > AChartEngine > 使用案例 >

安卓图表:使用achartengine做动态饼图 (二)

曹乐意
2023-12-01

动态饼图

MainActivity:

import java.util.Timer;
import java.util.TimerTask;
import org.achartengine.GraphicalView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * <Description functions in a word>
 * 动态更新饼图数据
 * <Detail description>
 */
public class MainActivity extends Activity {
    /**
     *mChartView:显示PieChart
     */
    private GraphicalView mChartView;
    /**
     *mHandler:消息通讯器
     */
    private Handler mHandler;
    /**
     *timer:定时器
     */
    private Timer timer = new Timer();// 设计定时器
    private Context mContext;
    PieChart mPieChart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = getApplicationContext();

        LinearLayout mLinear = (LinearLayout) findViewById(R.id.chart);
        mPieChart = new PieChart(mContext);

        mPieChart.setRenderer("年度支出表",Color.RED,Color.BLUE,Color.GREEN,"购物","水电费","伙食费");

        mChartView = mPieChart.getGraphicalView();

        mLinear.addView(mChartView,new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mHandler = new Handler() {// 这里的Handler实例将配合下面的Timer实例,完成定时更新图表的功能
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    mPieChart.updateChart("购物","水电费","伙食费");// 刷新图表具体方法 Handler将此并入主线程
                }
                super.handleMessage(msg);
            }
        };
        TimerTask task = new TimerTask() {
            @Override
            public void run() {// 通过消息更新
                Log.i("qiuzhping", " task ok ");
                Message message = new Message();
                message.what = 1;// 消息定义标志
                mHandler.sendMessage(message);
            }
        };

        timer.schedule(task, 100, 200 );// 执行任务
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
        }
        super.onDestroy();
    }
}

PieChart类:

package com.example.fuck;

import android.content.Context;
import android.graphics.Color;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import java.text.NumberFormat;
import java.util.Random;

public class PieChart {
    private GraphicalView mGraphicalView;
    private CategorySeries mCategorySeries;
    private DefaultRenderer renderer;
    private SimpleSeriesRenderer mSeriesRenderer1;
    private SimpleSeriesRenderer mSeriesRenderer2;
    private SimpleSeriesRenderer mSeriesRenderer3;
    private Context context;

    public PieChart(Context context){
        this.context = context;
    }
    /**
     * 获取图表
     *
     * @return
     */
    public GraphicalView getGraphicalView() {
        mGraphicalView = ChartFactory.getPieChartView(context, mCategorySeries, renderer);
        return mGraphicalView;
    }
    public void setRenderer(String chartTitle,int Color1,int Color2,int Color3,String title1,String title2,String title3){
        //创建渲染器,描绘器对象
        renderer = new DefaultRenderer();
        mCategorySeries = new CategorySeries(chartTitle);
        mSeriesRenderer1 = new SimpleSeriesRenderer();
        mSeriesRenderer2 = new SimpleSeriesRenderer();
        mSeriesRenderer3 = new SimpleSeriesRenderer();
        mSeriesRenderer1.setChartValuesFormat(NumberFormat.getPercentInstance());// 设置百分比
        mSeriesRenderer2.setChartValuesFormat(NumberFormat.getPercentInstance());// 设置百分比
        mSeriesRenderer3.setChartValuesFormat(NumberFormat.getPercentInstance());// 设置百分比
        mSeriesRenderer1.setColor(Color1);
        mSeriesRenderer2.setColor(Color2);
        mSeriesRenderer3.setColor(Color3);
        renderer.addSeriesRenderer(mSeriesRenderer1);
        renderer.addSeriesRenderer(mSeriesRenderer2);
        renderer.addSeriesRenderer(mSeriesRenderer3);
        renderer.setChartTitle(chartTitle);
        renderer.setShowLabels(true);//设置显示标签
        renderer.setShowLegend(true);//显示底部说明标签
        renderer.setLabelsTextSize(20);//设置标签字体大小,
        renderer.setAntialiasing(true);//消失锯齿
        renderer.setApplyBackgroundColor(true);//想要添加背景要先申请
        renderer.setBackgroundColor(Color.DKGRAY);
        renderer.setChartTitleTextSize(30);
        renderer.setDisplayValues(true);   //显示数据,这个不写就不会显示出百分比。。
        renderer.setZoomButtonsVisible(false); //显示缩小放大图标
        mCategorySeries.add(title1,30);
        mCategorySeries.add(title2,10);
        mCategorySeries.add(title3,60);
    }



    public void updateChart(String title1, String title2, String title3){
        mCategorySeries.clear();//清空之前的数据
        Random random = new Random();
        int R1 = random.nextInt(100);
        int R2 = random.nextInt(100);
        int R3 = random.nextInt(100);
        // 设置种类名称和对应的数值,前面是(key,value)键值对
        mCategorySeries.add(title1,R1);
        mCategorySeries.add(title2,R2);
        mCategorySeries.add(title3,R3);
        //让底部说明标签显示,如果不行,那就只会在初始化的时候显示一次
        renderer.setShowLabels(true);//设置显示标签
        renderer.setShowLegend(true);//显示底部说明标签
        renderer.setLabelsTextSize(20);//设置标签字体大小,
        renderer.setAntialiasing(true);//消失锯齿
        renderer.setApplyBackgroundColor(true);//想要添加背景要先申请
        renderer.setBackgroundColor(Color.DKGRAY);
        mGraphicalView.repaint();//重画,不写就不会显示动态变化
    }
}

activity_main: 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/chart"
        android:orientation="vertical" />

</LinearLayout>

 动态柱状图:

 安卓图表:achartengine之动态柱状图(一)

 类似资料: