代码之家  ›  专栏  ›  技术社区  ›  Sunisha Guptan

使用mpandroidchart的饼图对齐问题

  •  7
  • Sunisha Guptan  · 技术社区  · 7 年前
    api'com.github.philjay:mpandroidchart:v3.0.3'.

    代码:

    mchart.getDescription().setEnabled(假); mchart.setcentertext(“”); //中心孔半径,以最大半径的百分比表示 l.setdrawinside(假);

    但我面临的问题是对齐问题。附上截图,请看一下:

    graph_1 graph_2

    public static void chartDetails(PieChart mChart, Typeface tf) {
        mChart.animateXY(1400, 1400);
        mChart.getDescription().setEnabled(false);
        mChart.setCenterTextTypeface(tf);
        mChart.setCenterText("");
        mChart.setCenterTextSize(10f);
        mChart.setCenterTextTypeface(tf);
        // radius of the center hole in percent of maximum radius
        mChart.setHoleRadius(45f);
        mChart.setTransparentCircleRadius(50f);
        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        mChart.setTouchEnabled(true);
    }
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   Marc Estrada    7 年前

    例如,您可以实现如下目标:

    这是密码。当覆盖 piechart时,应该使用此代码。如果您没有覆盖它,您应该调整此代码以使用您的 piechart directly,例如 mchart.setdata(); instead of setdata();

    数据集。设置选择换档(5F); dataset.setyValuePosition(piedataset.valueposition.outside_slice); data.setValueTextColor(color.black);

    Outside values

    PieChart .如果不重写它,则应调整此代码以使用 mChart.setData(); setData();

    PieDataSet dataSet = new PieDataSet(entries, null);
    dataSet.setSliceSpace(3f);
    dataSet.setIconsOffset(new MPPointF(0, 40));
    dataSet.setSelectionShift(5f);
    
    // Outside values
    dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
    dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
    dataSet.setValueLinePart1OffsetPercentage(100f); /** When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size */
    dataSet.setValueLinePart1Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of first half of the line */
    dataSet.setValueLinePart2Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of second half of the line */
    setExtraOffsets(0.f, 5.f, 0.f, 5.f); // Ofsets of the view chart to prevent outside values being cropped /** Sets extra offsets (around the chart view) to be appended to the auto-calculated offsets.*/
    
    PieData data = new PieData(dataSet);
    data.setValueTextSize(10f);
    data.setValueTextColor(Color.BLACK);
    setData(data);
    
        2
  •  5
  •   Sunisha Guptan    7 年前
    public static void chart details1(piechart mchart,typeface tf){
    mchart.setcentertext(“”);
    mchart.setcentertext字体(tf);
    麦克哈特.塞特霍尔拉迪厄斯(45F);
    图例L=mchart.getLegend();
    l.设置方向(图例、图例、方向、水平);
    l.setdrawinside(假);
    mchart.setextraoffsets(25.f,0.f,25.f,0.f);
    
    mchart.setdrag减速摩擦系数(0.95f);
    mchart.setholecolor(彩色、白色);
    mchart.settransaparentcirclealpha(110);
    McHart.SetTransparentCircleradius(35华氏度);
    
    mchart.setrotationEnabled(错误);
    
    
    
    arraylist<integer>colors=new arraylist<integer>();
    
    颜色。添加(c);
    
    
    颜色。添加(c);
    
    
    用于(int c:colortemplate.pastel_colors)
    
    colors.add(colortemplate.getholoblue());
    
    }
    dataset.setvaluelinepart2length(0.6f);/**当valueposition为outsideslice时,表示行的后半部分的长度*/
    data.setvaluetextsize(5f);
    data.setValueFormatter(new PercentFormatter());
    }
    
    
    
    piedataset dataset=new piedataset(entries1,空);
    dataset.setcolors(颜色);
    
    
    
  • 实现OnChartValueSelectedListener
  • image

      public static void chartDetails1(PieChart mChart, Typeface tf) {
        mChart.animateXY(1400, 1400);
        mChart.getDescription().setEnabled(false);
        mChart.setCenterTextTypeface(tf);
        mChart.setCenterText("");
        mChart.setCenterTextSize(10f);
        mChart.setCenterTextTypeface(tf);
        // radius of the center hole in percent of maximum radius
        mChart.setHoleRadius(45f);
        mChart.setTransparentCircleRadius(50f);
        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setWordWrapEnabled(true);
        l.setDrawInside(false);
        mChart.setTouchEnabled(true);
        mChart.setDrawEntryLabels(false);
        mChart.setExtraOffsets(25.f, 0.f, 25.f, 0.f);
    
        mChart.setUsePercentValues(true);
        mChart.setDragDecelerationFrictionCoef(0.95f);
        mChart.setDrawHoleEnabled(true);
        mChart.setHoleColor(Color.WHITE);
        mChart.setTransparentCircleColor(Color.WHITE);
        mChart.setTransparentCircleAlpha(110);
        mChart.setTransparentCircleRadius(61f);
        mChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mChart.setHighlightPerTapEnabled(true);
        // mChart.setUnit(" €");
        // mChart.setDrawUnitsInChart(true);
        l.setEnabled(true);
        mChart.highlightValues(null);
        mChart.setUsePercentValues(true);
        /*new*/
        mChart.setHoleRadius(30f);
        mChart.setDrawCenterText(false);
        mChart.setTransparentCircleRadius(35f);
    
        mChart.getDescription().setEnabled(false);
        mChart.setRotationEnabled(false);
    
        mChart.setEntryLabelColor(Color.WHITE);
        mChart.setEntryLabelTextSize(9f);
    
    }
    
    public static ArrayList<Integer> colorsList() {
        ArrayList<Integer> colors = new ArrayList<Integer>();
    
        for (int c : ColorTemplate.MATERIAL_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());
        return colors;
    
    }
    public static PieData pieDatasetSlice(PieDataSet dataSet){
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setValueLinePart1OffsetPercentage(100f); /** When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size */
        dataSet.setValueLinePart1Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of first half of the line */
        dataSet.setValueLinePart2Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of second half of the line */
        dataSet.setSliceSpace(0f);
        PieData data = new PieData(dataSet);
        data.setValueTextSize(5f);
        data.setValueTextColor(Color.BLACK);
        data.setValueFormatter(new PercentFormatter());
        return data;
    }
    

    添加条目后,添加这些行,

        PieDataSet dataSet = new PieDataSet(entries1, null);
        ArrayList<Integer> colors = Constants.colorsList();
        dataSet.setColors(colors);
        PieData data = Constants.pieDatasetSlice(dataSet);