hellocharts图表插件---饼状图
来源:互联网 发布:英孚网络课程怎么样 编辑:程序博客网 时间:2024/06/13 19:25
前期插件那些准备和折线图一样
hellocharts图表插件—折线图 - An_nA的博客 - CSDN博客 http://blog.csdn.net/an_nal/article/details/76147185
查看了一些大神的帖子,学习了很多:
HelloChart–PieChartView(饼图) - 简书 http://www.jianshu.com/p/1498ac6c9f92
弄出来的效果图为:
不废话,直接上代码了:
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() { } }); }}
阅读全文
0 0
- hellocharts图表插件---饼状图
- hellocharts图表插件---柱状图
- hellocharts图表插件---折线图
- HelloCharts图表库之饼状图
- hellocharts-android图表库之饼状图:PieChartView
- HelloCharts优秀的图表库
- HelloCharts图表库之柱状图
- Android 图表开源框架HelloCharts 之 柱状图、折线图、饼状图,Fragment
- HelloCharts图表库之折线图
- Android开源图表库:HelloCharts
- 安卓学习笔记---图表控件 HelloCharts
- hellocharts-android图表库之柱状图:ColunmChartView
- hellocharts-android图表库使用详解
- HelloCharts 之饼状图使用
- HelloCharts
- HelloCharts
- hellocharts-android开源图表库(效果非常好)
- hellocharts-android开源图表库(效果非常好)
- fscanf()、fprintf()的使用
- 【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合
- Android无埋点数据收集SDK关键技术解析
- ubuntu下python3安装markdown
- VO、DTO、DO、PO的概念、区别和用处
- hellocharts图表插件---饼状图
- 阶乘之和(南阳oj,贪心,91)
- 文件的读写(I/O)操作总结
- PAT乙级 1003. 我要通过!(20)
- Vuex无法观察到值变化的解决办法
- 统计元音
- centos 卸载python和yum之后的解决办法
- SpringMVC接收复杂集合参数
- UVA11572UniqueSnowflakes