如何删除饼图中的底部空间?

时间:2019-06-08 12:59:43

标签: android android-layout pie-chart mpandroidchart

我在我的应用程序中使用饼图,现在饼图显示正确,唯一的问题是在饼图的底部获得了空间。以下是饼图的代码,并输出我想要的和我想要得到的。

将此库用于饼图

https://github.com/PhilJay/MPAndroidChart

 PieChart pieChart = (PieChart) view.findViewById(R.id.fragment_chart);
        pieChart.setUsePercentValues(true);
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        int height = display.getHeight();  // deprecated

        int offset = (int)(height * 0.65); /* percent to move */

       /* RelativeLayout.LayoutParams rlParams =
                (RelativeLayout.LayoutParams)pieChart.getLayoutParams();
        rlParams.setMargins(0, 0, 0, -offset);
        pieChart.setLayoutParams(rlParams);*/
        // 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.
        ArrayList<PieEntry> yvalues = new ArrayList<PieEntry>();
        yvalues.add(new PieEntry(8f, 0));
        yvalues.add(new PieEntry(15f, 1));
        yvalues.add(new PieEntry(12f, 2));


        PieDataSet dataSet = new PieDataSet(yvalues, "Reward Points");

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

        xVals.add("January");
        xVals.add("February");
        xVals.add("March");
        xVals.add("April");
        xVals.add("May");
        xVals.add("June");

        PieData data = new PieData( dataSet);

        data.setValueFormatter(new PercentFormatter());
        pieChart.setData(data);
        //pieChart.setDescription("This is Pie Chart");

        pieChart.setBackgroundColor(Color.TRANSPARENT);
        pieChart.setHoleColor(Color.WHITE);
        pieChart.setTransparentCircleColor(Color.WHITE);
        pieChart.setTransparentCircleAlpha(0);
        pieChart.setHoleRadius(18f);
        pieChart.setDrawCenterText(true);
        pieChart.isRotationEnabled();
        pieChart.isHighlightPerTapEnabled();
        pieChart.setCenterTextOffset(0f,-20f);
        pieChart.setEntryLabelColor(Color.WHITE);
        pieChart.setEntryLabelTypeface(Typeface.DEFAULT);
        pieChart.setEntryLabelTextSize(16f);
        pieChart.setTransparentCircleRadius(11f);

        pieChart.setDrawHoleEnabled(true);


        pieChart.setMaxAngle(180.0f);
        pieChart.setRotationAngle(180.0f);
        pieChart.setCenterTextSize(30);

        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
        data.setValueTextSize(13f);

        data.setValueTextColor(Color.DKGRAY);
        //pieChart.setOnChartValueSelectedListener(getActivity());

        pieChart.animateY(1400, Easing.EaseInOutQuad);

预期产量

Out

获取输出

enter image description here

2 个答案:

答案 0 :(得分:1)

要获得预期的输出,

  1. 您需要添加负底边距     //使用LinearLayout.LayoutParams,因为您使用的是线性布局     //设置按钮边距(我使用-450根据需要更改此值)

    findViewById()
  2. 禁用描述

    LinearLayout.LayoutParams rlParams =
            (LinearLayout.LayoutParams) pieChart.getLayoutParams();
    rlParams.setMargins(0, 0, 0, -450);
    pieChart.setLayoutParams(rlParams);
    
  3. 增加孔半径

    pieChart.getDescription().setEnabled(false); // remove the description
    
  4. 减小中心字体大小并设置文本

    pieChart.setHoleRadius(50f);        // increase hole radius
    

还要在xml文件中将高度更改为400dp。

pieChart.setCenterTextSize(15); // reduce font size
pieChart.setCenterText("Rewarded points: 140"); // set center text

enter image description here

答案 1 :(得分:1)

使用以下代码。

pieChart.setHoleRadius(64f);//18f
pieChart.setCenterTextSize(10);//30
pieChart.setCenterText("Reward Points: 150");//new line

pieChart.setExtraOffsets(5f,0f,10f,-100f);

setExtraOffsets中调整底部偏移的值(在上面的代码值-100f中),以调整空间。enter image description here