Android绘制饼图例子

public class MeFragment extends Fragment {
private View view;
private PieChartView pieChart;
PieChartData pieChardata;
List<SliceValue> values = new ArrayList<SliceValue>();
private int[] data = {21, 20, 9, 2, 8};
private int[] colorData = {Color.parseColor("#ec063d"),
Color.parseColor("#f1c704"),
Color.parseColor("#c9c9c9"),
Color.parseColor("#2bc208"),
Color.parseColor("#333333")};
private String[] stateChar = {"提示", "故障", "离线", "正常", "未激活"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_me, null);
setPieChartData();
initPieChart();
pieChart.setOnValueTouchListener(selectListener);//设置点击事件监听
return view;
}

public void initPieChart() {
pieChart = (PieChartView) view.findViewById(R.id.chart);
pieChardata = new PieChartData();
pieChardata.setHasLabels(false);//显示表情
pieChardata.setHasLabelsOnlyForSelected(false);//不用点击显示占的百分比
pieChardata.setHasLabelsOutside(false);//占的百分比是否显示在饼图外面
pieChardata.setHasCenterCircle(true);//是否是环形显示
pieChardata.setValues(values);//填充数据
pieChardata.setCenterCircleColor(Color.WHITE);//设置环形中间的颜色
pieChardata.setCenterCircleScale(0.7f);//设置环形的大小级别
pieChart.setPieChartData(pieChardata);
pieChart.setValueSelectionEnabled(true);//选择饼图某一块变大
pieChart.setAlpha(0.9f);//设置透明度
pieChart.setCircleFillRatio(1f);//设置饼图大小
}

private void setPieChartData() {
for (int i = 0; i < data.length; ++i) {
SliceValue sliceValue = new SliceValue((float) data[i], colorData[i]);
values.add(sliceValue);
}
}

private PieChartOnValueSelectListener selectListener = new PieChartOnValueSelectListener() {

@Override
public void onValueDeselected() {
// TODO Auto-generated method stub

}

@Override
public void onValueSelected(int arg0, SliceValue value) {
//选择对应图形后,在中间部分显示相应信息
pieChardata.setCenterText1(stateChar[arg0]);
pieChardata.setCenterText1Color(colorData[arg0]);
pieChardata.setCenterText1FontSize(10);
pieChardata.setCenterText2(value.getValue() + "(" + calPercent(arg0) + ")");
pieChardata.setCenterText2Color(colorData[arg0]);
pieChardata.setCenterText2FontSize(12);
Toast.makeText(getContext(), stateChar[arg0] + ":" + value.getValue(), Toast.LENGTH_SHORT).show();
}
};

private String calPercent(int i) {
String result = "";
int sum = 0;
for (int i1 = 0; i1 < data.length; i1++) {
sum += data[i1];
}
result = String.format("%.2f", (float) data[i] * 100 / sum) + "%";
return result;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/base"
android:gravity="center"
android:padding="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/me_title"
android:textColor="@color/white"
android:textSize="@dimen/title" />
</LinearLayout>

<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<lecho.lib.hellocharts.view.PieChartView
android:id="@+id/chart"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>

</LinearLayout>

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注