安卓图表hellochart - 饼状图PieChartView(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34536167/article/details/78754752

效果图:

这里写图片描述

1、饼状图相对于前2种图更加简单,因为没有X、Y轴的定义,代码量非常少,同时支持无限旋转轮盘

1.1、


import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

public class MainActivity extends AppCompatActivity {

    private PieChartView pieChart;
    private TextView text, text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pieChart = (PieChartView) this.findViewById(R.id.pie_chart);
        text = (TextView) this.findViewById(R.id.chart_text);
        text1 = (TextView) this.findViewById(R.id.chart_text1);

        setPieChartData();
    }

    /**
     * 获取数据
     */
    private void setPieChartData() {

        List<SliceValue> values = new ArrayList<>();
        //颜色list
        final List<Integer> colorData = new ArrayList<>();
        //标签信息
        final List<String> titleData = new ArrayList<>();
        //10种颜色
        colorData.add(Color.parseColor("#85B74F"));
        colorData.add(Color.parseColor("#009BDB"));
        colorData.add(Color.parseColor("#FF0000"));
        colorData.add(Color.parseColor("#9569F8"));
        colorData.add(Color.parseColor("#F87C67"));
        colorData.add(Color.parseColor("#F1DA3D"));
        colorData.add(Color.parseColor("#87EA39"));
        colorData.add(Color.parseColor("#48AEFA"));
        colorData.add(Color.parseColor("#4E5052"));
        colorData.add(Color.parseColor("#D36458"));
        //10中标签
        titleData.add("华为 Mate 10");
        titleData.add("荣耀6X");
        titleData.add("一加5T");
        titleData.add("华为Mate 10 Pro");
        titleData.add("魅族note6");
        titleData.add("360N6S");
        titleData.add("三星GALAXY Note 8");
        titleData.add("苹果iPhone X");
        titleData.add("vivo X20");
        titleData.add("OPPO R11s");

        //10种模块,数据100随机数
        for (int i = 0; i < colorData.size(); i++) {
            //注意数据list大一定要对应颜色值大小否者越界
            SliceValue sliceValue = new SliceValue((float) (100 * Math.random()), colorData.get(i));
            values.add(sliceValue);
        }

        final PieChartData pieChardata = new PieChartData();
        //显示标签信息
        pieChardata.setHasLabels(true);
        //true:只有点击对应的模块才显示标签信息  false:全部展示出来
        pieChardata.setHasLabelsOnlyForSelected(false);
        //true:占的百分比否显示在饼图外面 false:显示在模块的中间
        pieChardata.setHasLabelsOutside(true);
        //true:环形显示 false:圆形显示
        pieChardata.setHasCenterCircle(true);
        //设置每个模板之间的间隙
        pieChardata.setSlicesSpacing(5);
        //只有设置样式为圆环才能有效设置文字 (在assets目录下新建fonts目录,把ttf字体文件放到这)
        //Typeface tf = Typeface.createFromAsset(getAssets(), "你的字体资源文件路径");
        //pieChardata.setCenterText1Typeface(tf);
        //填充数据 注意不能放在最后 否则就只显示条
        pieChardata.setValues(values);

        //设置中间环形的颜色 只有setHasCenterCircle(true)为环形时配合使用 这里我的模式为圆形故不用
        pieChardata.setCenterCircleColor(Color.WHITE);
        //设置环形的大小级别 也是配合setHasCenterCircle(true)使用
        pieChardata.setCenterCircleScale(0.3f);

        //将参数设置到控件上
        pieChart.setPieChartData(pieChardata);
        //true:点击选中模块变大  false:只有手指按住模板时才变大,手指离开恢复原状
        pieChart.setValueSelectionEnabled(true);
        pieChart.setAlpha(0.9f);//设置透明度
        //设置饼图大小 值越大图越大 1是与父控件相等(如果设置便签信息在饼图外面建议设置0.9f 否则会出现展示不全)
        pieChart.setCircleFillRatio(0.9f);

        //点击事件 (只有设置样式为圆环才能有效)
        pieChart.setOnValueTouchListener(new PieChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int i, SliceValue sliceValue) {
                /**
                 *字体大小(查看了源码 text1:默认为42 text2:默认16 无论怎么修改字体都不能改变原因未知_(:з」∠)_)
                 * 同时注释Text1参数设置text2也不能显示出来...
                 * 建议另写TextView 赋值上去
                 */
                //设置上面文字显示内容
                //pieChardata.setCenterText1(titleData.get(i));
                //设置颜色
                //pieChardata.setCenterText1Color(colorData.get(i));
                //字体大小(无用)
                //pieChardata.setCenterText1FontSize(42);
                //pieChardata.setCenterText2(String.valueOf(sliceValue.getValue()));
                //pieChardata.setCenterText2Color(colorData.get(i));
                //pieChardata.setCenterText2FontSize(16);

                //自建文本赋值上去
                text.setText(titleData.get(i));
                text1.setText(sliceValue.getValue() + "");
            }

            @Override
            public void onValueDeselected() {
            }
        });
    }
}

1.2、

<?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:background="#fff"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <lecho.lib.hellocharts.view.PieChartView
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="400dp" />

        <TextView
            android:id="@+id/chart_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/chart_text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:maxLength="5"
            android:paddingTop="50dp"
            android:textSize="18sp" />


    </RelativeLayout>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qq_34536167/article/details/78754752
今日推荐