MPAndroid图表饼图标签被裁剪

6
我一直在尝试修改MPAndroid图表库中的饼图,以避免切断标签。我已经尝试使用chart.setExtraOffsets()chart.setPadding()修改视口,以及尝试使用视口处理程序使此饼图适应视图。

enter image description here

 public void setupInventoryChart(PieChart chart) {
        chart.setUsePercentValues(false);
        chart.setDescription("");

        chart.setDragDecelerationFrictionCoef(0.95f);

        mtf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");
        chart.setCenterTextTypeface(mtf);

        chart.setDrawHoleEnabled(true);
        //chart.setHoleColorTransparent(false);

        chart.setTransparentCircleColor(Color.WHITE);
//        chart.setTransparentCircleAlpha(110);

        chart.setHoleRadius(58f);
        chart.setTransparentCircleRadius(61f);

        chart.setDrawCenterText(true);

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

        // mChart.setUnit(" €");
        // mChart.setDrawUnitsInChart(true);

        // add a selection listener
        chart.setOnChartValueSelectedListener(this);

        chart.setExtraOffsets(0.5f, 0.5f, 0.5f, 0.8f);

        //chart.animateY(1500, Easing.EasingOption.EaseInOutQuad);
        // mChart.spin(2000, 0, 360);

        Legend l = chart.getLegend();
        l.setEnabled(false);
       // l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
      //  l.setXEntrySpace(7f);
      //  l.setYEntrySpace(5f);
      //  l.setYOffset(0f);
      //  l.setTextColor(Color.BLACK);

        setInventoryData(chart);

    }

    private void setInventoryData(PieChart chart) {

        //This defines the order of  how the data is shown.
        InventoryType[] inventoryStatusOrder = new InventoryType[] {
                InventoryType.ACTIVE_POSTINGS, InventoryType.SOLD, InventoryType.NEEDS_ATTENTION, InventoryType.URGENT};

        int count = inventoryStatusOrder.length;

        String[] parties = new String[count];
        for (int i = 0 ; i < parties.length ; i++) {
            parties[i] = inventoryStatusOrder[i].getValue();
        }

        ArrayList<Entry> yVals1 = new ArrayList<Entry>();   //yVals1 is value of each label.
        ArrayList<String> xVals = new ArrayList<String>();  //xVals is the labels

        // IMPORTANT: In a PieChart, num of values (Entry) should have the same
        // xIndex (even if from different DataSets), since no values can be
        // drawn above each other.
        int total = 0;
        int[] colorPositions = new int[count];
        for (int i = 0 ; i < count ; i++) {
            int item = mInventoryStatus.getCountByType(inventoryStatusOrder[i]);
            if (item != 0) {
                xVals.add(parties[i % parties.length]);
                yVals1.add(new Entry(item, i));
                total += item;
                colorPositions[i] = 1;
            }
        }

        PieDataSet dataSet = new PieDataSet(yVals1, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        int[] dashboardColors = getResources().getIntArray(R.array.dashboard_color);    //there are 5 orange colors.
        ArrayList<Integer> colors = new ArrayList();
        for (int i = 0 ; i < count ; i++) {
            if(colorPositions[i] != 0)
                colors.add(dashboardColors[i]);      //In this case, it will use the first color again.
        }

        int[] temp = new int[colors.size()];
        for(int i = 0; i < colors.size(); i++) {
            temp[i] = colors.get(i);
        }

        dataSet.setColors(temp);

        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.4f);
        dataSet.setValueLinePart2Length(0.4f);
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        //dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);


        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new IntValueFormatter(false, false));
        data.setValueTextSize(15f);
        data.setValueTextColor(Color.BLACK);
        data.setValueTypeface(mtf);

        chart.setData(data);

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

        mBoldTf = Typeface.createFromAsset(getActivity().getAssets(),"OpenSans-Bold.ttf");
        chart.setCenterText(generateCenterSpannableText(mInventoryStatus.getVins() + "\nUnique\nVINs"));
        chart.setCenterTextColor(Color.BLACK);
        chart.setCenterTextSize(16f);
        chart.setCenterTextTypeface(mBoldTf);

        chart.invalidate();
        chart.animateXY(900, 900);
    }

如何让图表使用手臂来表示输入值,例如 ------35 而不是直接在图表上显示 35? - pnizzle
2个回答

7

我也遇到了同样的问题。我使用下面这行代码解决了这个问题。

piecPieChart.setExtraBottomOffset(20f); 
piecPieChart.setExtraLeftOffset(20f); 
piecPieChart.setExtraRightOffset(20f);

希望它能对某些人有所帮助。

0
Legend l = pieChart.getLegend();
l.setWordWrapEnabled(true);

以上代码是解决方案。希望它适用于所有情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接