1. 程式人生 > >hellocharts圖表外掛---餅狀圖

hellocharts圖表外掛---餅狀圖

弄出來的效果圖為:

不廢話,直接上程式碼了:
1.佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <lecho.lib.hellocharts.view.PieChartView
android:id="@+id/piechart" android:layout_width="wrap_content" android:layout_height="wrap_content" >
</lecho.lib.hellocharts.view.PieChartView> </LinearLayout>

2.activity程式碼:

public class PieChartActivity extends AppCompatActivity {

    @BindView(R.id.piechart
) PieChartView piechart; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_piechart); ButterKnife.bind(this); //為piechart設定屬性 ViewGroup.LayoutParams layoutParams = piechart.getLayoutParams
(); layoutParams.height = AppUtil.dp2px(this, 180) ; layoutParams.width = AppUtil.getScreenWidth(this); piechart.setLayoutParams(layoutParams);//設定寬高 piechart.setViewportCalculationEnabled(true);//設定餅圖自動適應大小 piechart.setChartRotationEnabled(false);//設定餅圖是否可以手動旋轉 //例項化PieChartData物件 final PieChartData pd = new PieChartData(); pd.setHasLabelsOutside(false);//設定餅圖外面是否顯示值 pd.setHasCenterCircle(true);//設定餅圖中間是否有第二個圈 pd.setCenterCircleColor(getResources().getColor(R.color.colorWhite));//設定餅圖中間圈的顏色 pd.setCenterCircleScale(0.7f);////設定第二個圈的大小比例 Typeface typeface = Typeface.create("typeface", Typeface.BOLD_ITALIC);//設定文字字型,BOLD_ITALIC粗斜體 pd.setCenterText1Typeface(typeface); pd.setCenterText1Color(getResources().getColor(R.color.colorPrimary)); pd.setCenterText1FontSize(16); //pd.setCenterText1("總投訴事項"); pd.setCenterText2Color(getResources().getColor(R.color.colorRed)); pd.setCenterText2FontSize(16); pd.setValueLabelsTextColor(Color.BLACK);//設定顯示值的字型顏色 //pd.setSlicesSpacing(int sliceSpacing);//設定資料間的間隙 //pd.setHasLabelsOnlyForSelected(true);//設定當值被選中才顯示 //pd.hasLabelsOutside(); pd.setHasLabels(false); pd.setValueLabelsTextColor(getResources().getColor(R.color.colorWhite)); //初始化餅圖資料 new SliceValue(float value, int color).setLabel(String label) final List<SliceValue> sliceList = new ArrayList<SliceValue>(); for( int i = 1; i <= 10 ; i++){ sliceList.add(new SliceValue( i,getResources().getColor(R.color.pie_normal)).setLabel( String.valueOf( i) + "沒有名字")); } pd.setValues(sliceList);//為餅圖新增資料 piechart.setPieChartData(pd);//為餅圖設定資料 //將第一個不為0值預設選中() SliceValue defaultValue = sliceList.get(0); defaultValue.setColor(getResources().getColor(R.color.pie_selected)); if( defaultValue.getValue() < 0 ){ pd.setCenterText1("0 沒有名字"); pd.setCenterText2((int) defaultValue.getValue() + "項"); }else{ pd.setCenterText1(String.valueOf(defaultValue.getLabel())); pd.setCenterText2((int) defaultValue.getValue() + "項"); } piechart.startDataAnimation(); //為餅圖新增觸屏相應事件 piechart.setOnValueTouchListener(new PieChartOnValueSelectListener() { @Override public void onValueSelected(int i, SliceValue sliceValue) { for (int j = 0; j < sliceList.size(); j++) { if (i == j) { sliceList.get(j).setColor(getResources().getColor(R.color.pie_selected)); } else { sliceList.get(j).setColor(getResources().getColor(R.color.pie_normal)); } } pd.setCenterText1(String.valueOf(sliceList.get(i).getLabel())); pd.setCenterText2((int) sliceList.get(i).getValue() + "項"); piechart.startDataAnimation(); } @Override public void onValueDeselected() { } }); } }