zoukankan      html  css  js  c++  java
  • MPAndroidChart饼图属性及相关设置

    公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图:

    布局文件:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <com.github.mikephil.charting.charts.PieChart  
    2.                android:id="@+id/chart"  
    3.                android:layout_width="match_parent"  
    4.                android:layout_height="match_parent" />  

    java代码:

    1、初始化饼图

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1.  private void initChart(){  
    2.   
    3.         mChart = (PieChart) findViewById(R.id.chart);  
    4.         mChart.setUsePercentValues(true);  
    5.         mChart.setDescription("");  
    6.         mChart.setExtraOffsets(5, 10, 5, 5);  
    7. //        mChart.setDrawSliceText(false);//设置隐藏饼图上文字,只显示百分比  
    8.         mChart.setDrawHoleEnabled(true);  
    9.         mChart.setHoleColorTransparent(true);  
    10.   
    11.         mChart.setTransparentCircleColor(getResources().getColor(R.color.buttombar_bg));  
    12.         mChart.setTransparentCircleAlpha(110);  
    13.         mChart.setOnChartValueSelectedListener(this);  
    14.         mChart.setHoleRadius(45f); //半径  
    15.         //mChart.setHoleRadius(0)  //实心圆  
    16.         mChart.setTransparentCircleRadius(48f);// 半透明圈  
    17.         mChart.setDrawCenterText(true);//饼状图中间可以添加文字  
    18.         // 如果没有数据的时候,会显示这个,类似ListView的EmptyView  
    19.         mChart.setNoDataText(getResources().getString(R.string.no_data));  
    20.         mChart.setUsePercentValues(true);//设置显示成比例  
    21.         SimpleDateFormat format = new SimpleDateFormat("yyyy");  
    22.         String year = format.format(since_at*1000);  
    23.         mChart.setCenterText(generateCenterSpannableText(year));  
    24.         mChart.setRotationAngle(0); // 初始旋转角度  
    25.         // enable rotation of the chart by touch  
    26.         mChart.setRotationEnabled(true); // 可以手动旋转  
    27.         mChart.setHighlightPerTapEnabled(true);  
    28.         mChart.animateY(1000, Easing.EasingOption.EaseInOutQuad); //设置动画  
    29.         Legend mLegend = mChart.getLegend();  //设置比例图  
    30.         mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下边显示  
    31.         mLegend.setFormSize(12f);//比例块字体大小  
    32.         mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合  
    33.         mLegend.setYEntrySpace(2f);  
    34.         //设置比例块换行...  
    35.         mLegend.setWordWrapEnabled(true);  
    36.         mLegend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);  
    37.   
    38.         mLegend.setTextColor(getResources().getColor(R.color.alpha_80));  
    39.         mLegend.setForm(Legend.LegendForm.SQUARE);//设置比例块形状,默认为方块  
    40. //        mLegend.setEnabled(false);//设置禁用比例块  
    41.     }  

    2、设置饼图数据

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1.  /** 
    2.      * 设置饼图的数据 
    3.      * @param names 饼图上显示的比例名称 
    4.      * @param counts 百分比 
    5.      */  
    6.     private void setData(ArrayList<String> names,ArrayList<Entry> counts) {  
    7.         PieDataSet dataSet = new PieDataSet(counts, "");  
    8.         dataSet.setSliceSpace(2f);  
    9.         dataSet.setSelectionShift(5f);  
    10.   
    11.         ArrayList<Integer> colors = new ArrayList<Integer>();  
    12.         for (int c : ColorTemplate.JOYFUL_COLORS)  
    13.             colors.add(c);  
    14. //  
    15.         for (int c : ColorTemplate.COLORFUL_COLORS)  
    16.             colors.add(c);  
    17.   
    18.         for (int c : ColorTemplate.LIBERTY_COLORS)  
    19.             colors.add(c);  
    20.   
    21. //        for (int c : ColorTemplate.PASTEL_COLORS)  
    22. //            colors.add(c);  
    23.         colors.add(ColorTemplate.getHoloBlue());  
    24. //        colors.add(getResources().getColor(R.color.stastic_team));  
    25.         dataSet.setColors(colors);  
    26.         //dataSet.setSelectionShift(0f);  
    27.         PieData data = new PieData(names, dataSet);  
    28.         data.setValueFormatter(new PercentFormatter());  
    29.         data.setValueTextSize(12f);  
    30.         data.setValueTextColor(getResources().getColor(R.color.whrite));  
    31.         mChart.setData(data);  
    32.         // undo all highlights  
    33.         mChart.highlightValues(null);  
    34.   
    35.         mChart.invalidate();  
    36.     }  


    在这个过程中遇到几个问题:

    1、底部的比例块显示只有一行,如果数据较多的话,会超出屏幕外边,所以需要换行显示比例块,设置如下:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. //设置比例块换行...  
    2.   mLegend.setWordWrapEnabled(true);  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. 同时需要把比例块放到下边或者放到上边  <span style="font-family: Arial, Helvetica, sans-serif;"> mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下边显示</span>  
    2、饼图上怎么只显示百分比,查了好多资料没有看有人提到这个,最后在stackoverflow上找到答案。

          http://stackoverflow.com/questions/31154706/mpandroidchart-piechart-remove-percents

    pieChart.setDrawSliceText(false)

    3、如果比例块放在左边或者右边,如果字体太长,会和饼图叠加在一起,做以下处理可以防止叠加。

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合  
    2. mLegend.setYEntrySpace(2f);
  • 相关阅读:
    Django 之 admin管理工具
    第二十七天- 网络通信协议 TCP UDP 缓冲区
    第二十六天- C/S架构 通信流程 socket
    第二十五天- 包
    第二十四天- 模块导入 import from xxx import xxx
    第二十三天- 模块 re
    第二十二天- 序列化 pickle json shelve
    第二十一天- 基本模块
    第二十天- 多继承 经典MRO 新式MRO super()
    第十九天- 约束 异常处理 日志使用
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5664528.html
Copyright © 2011-2022 走看看