Android图表库MPAndroidChart(六)饼状图 -折线百分比

来源:互联网 发布:51单片机ad转换 编辑:程序博客网 时间:2024/05/17 02:16

应用

package com.fate.mpandroidchartfive;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.SpannableString;import android.view.View;import android.widget.Button;import com.github.mikephil.charting.animation.Easing;import com.github.mikephil.charting.charts.PieChart;import com.github.mikephil.charting.components.Legend;import com.github.mikephil.charting.data.Entry;import com.github.mikephil.charting.data.PieData;import com.github.mikephil.charting.data.PieDataSet;import com.github.mikephil.charting.data.PieEntry;import com.github.mikephil.charting.formatter.PercentFormatter;import com.github.mikephil.charting.highlight.Highlight;import com.github.mikephil.charting.interfaces.datasets.IDataSet;import com.github.mikephil.charting.listener.OnChartValueSelectedListener;import com.github.mikephil.charting.utils.ColorTemplate;import java.util.ArrayList;public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener, View.OnClickListener {    private PieChart mPieChart;    //显示百分比    private Button btn_show_percentage;    //显示类型    private Button btn_show_type;    //x轴动画    private Button btn_anim_x;    //y轴动画    private Button btn_anim_y;    //xy轴动画    private Button btn_anim_xy;    //保存到sd卡    private Button btn_save_pic;    //显示中间文字    private Button btn_show_center_text;    //旋转动画    private Button btn_anim_rotating;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    //初始化    private void initView() {        btn_show_percentage = (Button) findViewById(R.id.btn_show_percentage);        btn_show_percentage.setOnClickListener(this);        btn_show_type = (Button) findViewById(R.id.btn_show_type);        btn_show_type.setOnClickListener(this);        btn_anim_x = (Button) findViewById(R.id.btn_anim_x);        btn_anim_x.setOnClickListener(this);        btn_anim_y = (Button) findViewById(R.id.btn_anim_y);        btn_anim_y.setOnClickListener(this);        btn_anim_xy = (Button) findViewById(R.id.btn_anim_xy);        btn_anim_xy.setOnClickListener(this);        btn_save_pic = (Button) findViewById(R.id.btn_save_pic);        btn_save_pic.setOnClickListener(this);        btn_show_center_text = (Button) findViewById(R.id.btn_show_center_text);        btn_show_center_text.setOnClickListener(this);        btn_anim_rotating = (Button) findViewById(R.id.btn_anim_rotating);        btn_anim_rotating.setOnClickListener(this);        //折现饼状图        mPieChart = (PieChart) findViewById(R.id.mPieChart);        mPieChart.setUsePercentValues(true);        mPieChart.getDescription().setEnabled(false);        mPieChart.setExtraOffsets(5, 10, 5, 5);        mPieChart.setDragDecelerationFrictionCoef(0.95f);        //绘制中间文字        mPieChart.setCenterText(generateCenterSpannableText());        mPieChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);        mPieChart.setDrawHoleEnabled(true);        mPieChart.setHoleColor(Color.WHITE);        mPieChart.setTransparentCircleColor(Color.WHITE);        mPieChart.setTransparentCircleAlpha(110);        mPieChart.setHoleRadius(58f);        mPieChart.setTransparentCircleRadius(61f);        mPieChart.setDrawCenterText(true);        mPieChart.setRotationAngle(0);        // 触摸旋转        mPieChart.setRotationEnabled(true);        mPieChart.setHighlightPerTapEnabled(true);        // 添加一个选择监听器        mPieChart.setOnChartValueSelectedListener(this);        //模拟数据        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();        entries.add(new PieEntry(40, "优秀"));        entries.add(new PieEntry(20, "满分"));        entries.add(new PieEntry(30, "及格"));        entries.add(new PieEntry(10, "不及格"));        //设置数据        setData(entries);        //默认动画        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);        Legend l = mPieChart.getLegend();        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);        l.setOrientation(Legend.LegendOrientation.VERTICAL);        l.setDrawInside(false);        l.setEnabled(false);    }    //设置数据    private void setData(ArrayList<PieEntry> entries) {        PieDataSet dataSet = new PieDataSet(entries, "三年级一班");        dataSet.setSliceSpace(3f);        dataSet.setSelectionShift(5f);        ArrayList<Integer> colors = new ArrayList<Integer>();        for (int c : ColorTemplate.VORDIPLOM_COLORS)            colors.add(c);        for (int c : ColorTemplate.JOYFUL_COLORS)            colors.add(c);        for (int c : ColorTemplate.COLORFUL_COLORS)            colors.add(c);        for (int c : ColorTemplate.LIBERTY_COLORS)            colors.add(c);        for (int c : ColorTemplate.PASTEL_COLORS)            colors.add(c);        colors.add(ColorTemplate.getHoloBlue());        dataSet.setColors(colors);        dataSet.setValueLinePart1OffsetPercentage(80.f);        dataSet.setValueLinePart1Length(0.2f);        dataSet.setValueLinePart2Length(0.5f);        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);        PieData data = new PieData(dataSet);        data.setValueFormatter(new PercentFormatter());        data.setValueTextSize(11f);        data.setValueTextColor(Color.BLACK);        mPieChart.setData(data);        // 撤销所有的亮点        mPieChart.highlightValues(null);        mPieChart.invalidate();    }    //绘制中心文字    private SpannableString generateCenterSpannableText() {        SpannableString s = new SpannableString("成绩占比");        return s;    }    @Override    public void onValueSelected(Entry e, Highlight h) {    }    @Override    public void onNothingSelected() {    }    @Override    public void onClick(View v) {        switch (v.getId()) {            //显示百分比            case R.id.btn_show_percentage:                for (IDataSet<?> set : mPieChart.getData().getDataSets())                    set.setDrawValues(!set.isDrawValuesEnabled());                mPieChart.invalidate();                break;            //显示类型            case R.id.btn_show_type:                if (mPieChart.isDrawHoleEnabled())                    mPieChart.setDrawHoleEnabled(false);                else                    mPieChart.setDrawHoleEnabled(true);                mPieChart.invalidate();                break;            //x轴动画            case R.id.btn_anim_x:                mPieChart.animateX(1400);                break;            //y轴动画            case R.id.btn_anim_y:                mPieChart.animateY(1400);                break;            //xy轴动画            case R.id.btn_anim_xy:                mPieChart.animateXY(1400, 1400);                break;            //保存到sd卡            case R.id.btn_save_pic:                mPieChart.saveToPath("title" + System.currentTimeMillis(), "");                break;            //显示中间文字            case R.id.btn_show_center_text:                if (mPieChart.isDrawCenterTextEnabled())                    mPieChart.setDrawCenterText(false);                else                    mPieChart.setDrawCenterText(true);                mPieChart.invalidate();                break;            //旋转动画            case R.id.btn_anim_rotating:                mPieChart.spin(1000, mPieChart.getRotationAngle(), mPieChart.getRotationAngle() + 360, Easing.EasingOption                        .EaseInCubic);                break;        }    }}

布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.github.mikephil.charting.charts.PieChart        android:id="@+id/mPieChart"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"/>    <!--还有显示描述等等-->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_show_percentage"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="显示百分比"/>        <Button            android:id="@+id/btn_show_type"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="是否铺满"/>        <Button            android:id="@+id/btn_anim_x"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="X轴动画"/>        <Button            android:id="@+id/btn_anim_y"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Y轴动画"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_anim_xy"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="XY轴动画"/>        <Button            android:id="@+id/btn_show_center_text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="显示中间文字"/>        <Button            android:id="@+id/btn_save_pic"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="保存画廊"/>        <Button            android:id="@+id/btn_anim_rotating"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="旋转动画"/>    </LinearLayout></LinearLayout>

http://blog.csdn.net/qq_26787115/article/details/53258500

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狙击手遇到热追踪导弹怎么办 做完卷腹脖子疼怎么办 医疗设备销售遭遇瓶颈怎么办 给顾客加油加超了怎么办 卡密码输错两次怎么办 擤鼻涕耳朵会响怎么办 鼻子里有血丝是怎么办 怀孕8周上火了怎么办 鼻炎犯了鼻涕流不停怎么办 擤鼻涕眼睛肿了怎么办 感冒咳嗽鼻子不通气怎么办 宝宝感冒不会擤鼻涕怎么办 新生儿鼻腔里有鼻涕怎么办 宝宝鼻腔有鼻涕出不来怎么办 怀孕的人感冒了怎么办 孕37周感冒咳嗽怎么办 吹鼻涕耳朵堵了怎么办 怀孕的孔雀鱼生病了怎么办 生病了咳嗽一直不好怎么办 宝宝生病治疗后咳嗽怎么办 2个月宝宝老是生病怎么办 2个月的哈士奇生病怎么办 怀孕的猫生病了怎么办 宝宝生病咳嗽啥都不吃怎么办 怀孕了感冒了怎么办啊 2个月宝宝生病了怎么办 刚刚怀孕了就生病了怎么办 一岁多宝宝总是发烧咳嗽生病怎么办 7个月宝宝生病怎么办 4个月宝宝老是生病怎么办 孕早期嗓子有痰怎么办 鼻炎早晨起床鼻涕带血怎么办 鼻子破皮了结痂怎么办 擤鼻子耳朵好像堵住了怎么办 鼻子和脸上起皮怎么办 鼻子擦鼻涕擦红怎么办 鼻子下面擦红了怎么办 鼻子擤鼻涕破皮怎么办 哭完鼻子不通气怎么办 擦鼻子擦多了疼怎么办 擤鼻涕时耳朵听不见了怎么办