MPAndroidChar--饼状图

MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

GitHub地址:
https://github.com/PhilJay/MPAndroidChart

定义XML文件

    <com.github.mikephil.charting.charts.PieChart
                    android:id="@+id/chart1"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginLeft="15dp"
                    />

主要Java逻辑代码如下:

public class MainActivity extends ActionBarActivity {    

    private PieChart mChart;    

    @Override    
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);    

        mChart = (PieChart) findViewById(R.id.spread_pie_chart);    
        PieData mPieData = getPieData(4, 100);    
        showChart(mChart, mPieData);    
    }    

    private void showChart(PieChart pieChart, PieData pieData) {    
        //显示成内圈颜色是否透明
        pieChart.setHoleColorTransparent(true);  
        //半径  
        pieChart.setHoleRadius(60f);      
        // 半透明圈    
        pieChart.setTransparentCircleRadius(64f); 
         //实心圆 
        //pieChart.setHoleRadius(0);    
        //右下角名字
        pieChart.setDescription("测试饼状图");    
        // mChart.setDrawYValues(true);   
        //饼状图中间可以添加文字     
        pieChart.setDrawCenterText(true);
        //变成空心的饼图(有白色边)
        pieChart.setDrawHoleEnabled(true);    
        // 初始旋转角度  
        pieChart.setRotationAngle(90);   

        // draws the corresponding description value into the slice    
        // mChart.setDrawXValues(true);    
        // 可以手动旋转      
        pieChart.setRotationEnabled(true); 

        //显示成百分比      
        pieChart.setUsePercentValues(true);
        // mChart.setUnit(" €");    
        // mChart.setDrawUnitsInChart(true);    

        // add a selection listener    
//      mChart.setOnChartValueSelectedListener(this);    
        // mChart.setTouchEnabled(false);    

//      mChart.setOnAnimationListener(this);    

        pieChart.setCenterText("Quarterly Revenue");  //饼状图中间的文字    

        //设置数据    
        setData(pieData);   
        //setData(总共几部分,传进来的char,每个部分的值的集合)  
        setData(stoclist.size(), chart,bililist);
        // undo all highlights    
//      pieChart.highlightValues(null);    
//      pieChart.invalidate();    

        //设置比例图 
        Legend mLegend = pieChart.getLegend();   
        //最右边显示   
   mLegend.setPosition(LegendPosition.RIGHT_OF_CHART);  
        //设置比例图的形状,默认是方形   
//      mLegend.setForm(LegendForm.LINE);   
        mLegend.setXEntrySpace(7f);    
        mLegend.setYEntrySpace(5f);    
        //隐藏说明(比例圈)
        mLegend.setEnabled(false);
        pieChart.animateXY(1000, 1000);  //设置动画    
        // mChart.spin(2000, 0, 360);    
    }    

    /**  
     *   
     * @param count 分成几部分  
     * @param range  
     */    

    private void setData(int count, PieChart chart,List<Float> blist) {

        ArrayList<Entry> yVals1 = new ArrayList<Entry>();
        ArrayList<String> xVals = new ArrayList<String>();
        ArrayList<Integer> colors = new ArrayList<Integer>();
        // IMPORTANT: In a PieChart, no values (Entry) should have the same
        // xIndex (even if from different DataSets), since no values can be
        // drawn above each other.
        if (new BigDecimal(pricecount.getText().toString()).compareTo(new BigDecimal(0))!=1 &&chart.equals(chart2)){
            yVals1.add(new Entry(1.0f, 0));
            xVals.add("stock");
            colors.add(getResources().getColor(R.color.cC3));
        }
        else {
            for (int i = 0; i < count ; i++) {
                //确定比例权重
                yVals1.add(new Entry(blist.get(i), i));
            }

            //往饼图上写的分类名字(介绍上也有这个名字)
            for (int i = 0; i < count ; i++)
                xVals.add(stoclist.get(i));

            // add a lot of colors
            for (int c : colorlist)
                colors.add(c);

        }

        PieDataSet dataSet = new PieDataSet(yVals1, "");
        //设置个饼状图之间的距离
        dataSet.setSliceSpace(0f);
        dataSet.setSelectionShift(5f);

        // 饼图颜色
        dataSet.setColors(colors);
        // 选中态多出的长度
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(9f);
        data.setValueTextColor(Color.TRANSPARENT);

        chart.setData(data);

        // undo all highlights
        chart.highlightValues(null);

        chart.invalidate();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值