不能使用MPAndroidChart像圆形饼图一样绘制甜甜圈

时间:2015-04-10 07:27:55

标签: android pie-chart mpandroidchart

我正在使用MPAndroidChart,我正在尝试使用mpandroidchart lib创建一个看起来像甜甜圈的饼图,但不知怎的,我无法做到这一点。 以下是我的代码 -

ArrayList<Entry> entries = new ArrayList<>();
        entries.add(new Entry((float) 20.0, 0));
        entries.add(new Entry((float) 30.0, 1));

        int colors[] = {Color.parseColor("#DCDEE0"),Color.parseColor("#466A80"),Color.parseColor("#0078CA"),Color.parseColor("#5BC2E7"),Color.parseColor("#99E4FF")};
        PieDataSet dataset = new PieDataSet(entries, "# of Calls");
        dataset.setColors(colors);
        dataset.setSliceSpace(3f);
        ArrayList<String> labels = new ArrayList<String>();
        labels.add("January"); 
        labels.add("February"); 
    /*  labels.add("March"); 
        labels.add("April"); 
        labels.add("May");
        labels.add("June");*/

        PieChart chart1 = new PieChart(this);
        chart1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f));
        chart1.setHoleRadius(60f);
        chart1.setHoleColorTransparent(false);
        chart1.setDrawHoleEnabled(true);
        chart1.setUsePercentValues(true);
        chart1.setHoleColor(Color.WHITE);
        PieChart chart2 = new PieChart(this);
        chart2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f));
        LinearLayout chart = (LinearLayout)findViewById(R.id.chart);
        chart.addView(chart1);
        chart.addView(chart2);

        PieData data = new PieData(labels, dataset);
        chart1.setData(data);
        chart2.setData(data);

我能够绘制饼图,但不能让它看起来像甜甜圈。请参阅随附的屏幕截图。有人可以帮忙。

enter image description here

3 个答案:

答案 0 :(得分:2)

这是已知问题,已在最新提交中修复。 https://github.com/PhilJay/MPAndroidChart/issues/527

包含修复程序的库的v2.1.0应在下周内发布!

答案 1 :(得分:2)

是试试这段代码:=

public class piechart extends ActionBarActivity {

    RelativeLayout mainLayout;
   PieChart mChart;
    // we're going to display pie chart for smartphones martket shares
   float[] yData = { 15, 10, 15, 8, 4,10 };
   String[] xData = { "Technical excellence", "Nimble", "Innovation", "Integrity", "Colloboration","Passion" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_piechart);
        mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
        mChart = new PieChart(this);

        mainLayout.addView(mChart);
        mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));

        // configure pie chart
        mChart.setUsePercentValues(true);
        mChart.setDescription("Employee Analysis");

        // enable hole and configure
        mChart.setDrawHoleEnabled(true);
        mChart.setHoleColorTransparent(true);
        mChart.setHoleRadius(7);
        mChart.setTransparentCircleRadius(10);

        // enable rotation of the chart by touch
        mChart.setRotationAngle(0);
        mChart.setRotationEnabled(true);

        // set a chart value selected listener
        mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                // display msg when value selected
                if (e == null)
                    return;

                Intent i=new Intent(piechart.this,Nimble.class);
                startActivity(i);
            }

            @Override
            public void onNothingSelected() {

            }
        });

        // add data
        addData();

        // customize legends
        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        l.setXEntrySpace(7);
        l.setYEntrySpace(5);

    }

    private void addData() {
        ArrayList<Entry> yVals1 = new ArrayList<Entry>();

        for (int i = 0; i < yData.length; i++)
            yVals1.add(new Entry(yData[i], i));

        ArrayList<String> xVals = new ArrayList<String>();

        for (int i = 0; i < xData.length; i++)
            xVals.add(xData[i]);

        // create pie data set
        PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
        dataSet.setSliceSpace(3);
        dataSet.setSelectionShift(5);

        // add many colors
        ArrayList<Integer> colors = new ArrayList<Integer>();

        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(colors);

        // instantiate pie data object now
        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.GRAY);

        mChart.setData(data);

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

        // update pie chart
        mChart.invalidate();
        mChart.getLegend().setEnabled(false);
    }

答案 2 :(得分:1)

我面临同样的问题,这是如何解决它

    final int[] My_COLORS = { Color.rgb(206, 37, 42), Color.rgb(0, 255, 0), Color.rgb(0, 0, 255)
};

    ArrayList<Integer> colors = new ArrayList<Integer>(); 

    for (int c : My_COLORS)
        colors.add(c);
    dataSet.setColors(colors);