hellocharts-android图表库之饼状图:PieChartView
来源:互联网 发布:crf算法实现 编辑:程序博客网 时间:2024/05/17 09:46
接上篇http://blog.csdn.net/true100/article/details/59480360
现在项目中刚好用到了饼状图和条形图统计,就边学习边记录个DEMO。
废话不多说,直接上代码
public class ChartActivity extends AppCompatActivity { //饼形图控件 private PieChartView pie_chart; //数据 private 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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chart); pie_chart = (PieChartView) findViewById(R.id.pie_chart); pie_chart.setOnValueTouchListener(selectListener);//设置点击事件监听 setPieChartData(); initPieChart(); } /** * 获取数据 */ private void setPieChartData() { for (int i = 0; i < data.length; ++i) { SliceValue sliceValue = new SliceValue((float) data[i], colorData[i]); values.add(sliceValue); } } /** * 初始化 */ private void initPieChart() { pieChardata = new PieChartData(); pieChardata.setHasLabels(true);//显示表情 pieChardata.setHasLabelsOnlyForSelected(false);//不用点击显示占的百分比 pieChardata.setHasLabelsOutside(false);//占的百分比是否显示在饼图外面 pieChardata.setHasCenterCircle(true);//是否是环形显示 pieChardata.setValues(values);//填充数据 pieChardata.setCenterCircleColor(Color.WHITE);//设置环形中间的颜色 pieChardata.setCenterCircleScale(0.5f);//设置环形的大小级别 pie_chart.setPieChartData(pieChardata); pie_chart.setValueSelectionEnabled(true);//选择饼图某一块变大 pie_chart.setAlpha(0.9f);//设置透明度 pie_chart.setCircleFillRatio(1f);//设置饼图大小 } /** * 监听事件 */ 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(ChartActivity.this, 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; }
对应布局文件
<?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="horizontal" > <lecho.lib.hellocharts.view.PieChartView android:id="@+id/pie_chart" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:layout_gravity="center_vertical" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="报警"/> <View android:layout_width="15dp" android:layout_height="10dp" android:background="#ec063d" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="故障"/> <View android:layout_width="15dp" android:layout_height="10dp" android:background="#f1c704" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="离线"/> <View android:layout_width="15dp" android:layout_height="10dp" android:background="#c9c9c9" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正常"/> <View android:layout_width="15dp" android:layout_height="10dp" android:background="#2bc208" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未激活"/> <View android:layout_width="15dp" android:layout_height="10dp" android:background="#333333" /> </LinearLayout> </LinearLayout></LinearLayout>
效果图:
阅读全文
1 0
- hellocharts-android图表库之饼状图:PieChartView
- HelloCharts图表库之饼状图
- hellocharts-android图表库之柱状图:ColunmChartView
- HelloCharts图表库之柱状图
- HelloCharts图表库之折线图
- Android开源图表库:HelloCharts
- hellocharts-android图表库使用详解
- hellocharts图表插件---饼状图
- Android 图表开源框架HelloCharts 之 柱状图、折线图、饼状图,Fragment
- HelloCharts开源图表库(一)之折线图
- hellocharts-android开源图表库(效果非常好)
- hellocharts-android开源图表库(效果非常好)
- hellocharts-android开源图表库(效果非常好)
- hellocharts-android开源图表库(效果非常好)
- hellocharts-android开源图表库(效果非常好)
- HelloCharts优秀的图表库
- HelloCharts 之饼状图使用
- 开源图表库hellocharts之最详细的使用介绍
- Qt线程间的信号与槽 以及 QThread
- 硅谷 机器学习 深度学习 课程
- H5简单的小弹框 && JQ判断手机和电话
- GC类型以及不同类型GC的搭配
- MyReport报表引擎2.7.10.0发布
- hellocharts-android图表库之饼状图:PieChartView
- 将Linux文件清空的几种方法
- C语言——水仙花数
- Android Studio 使用教程(七)之同时打开多个项目
- 素数筛法 +HDU #2136
- sudo apt-get update报错 (osm)
- Bootstrap3.0学习第十五轮(大屏幕介绍、页面标题、缩略图、警示框、Well)
- IntelliJ IDEA 使用心得与常用快捷键
- maven项目配置jetty9插件