这是我的PieChart,我想将这些数据移动到想要的位置,那么我该怎么做?

java - MPAndroidChart PieChart设置-LMLPHP

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MainChart();
    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.cons);
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mChart.isDrawHoleEnabled())
            { mChart.setDrawHoleEnabled(false);
                mChart.setDrawCenterText(false);}
            else {
                mChart.setDrawHoleEnabled(true);
                mChart.setDrawCenterText(true);
            }
            mChart.invalidate();
        }
    });
}
private void setData() {
    float[] yData = { 20,30,50 };
    String[] xData = { "January", "February", "January" };


    ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

    // NOTE: The order of the entries when being added to the entries array determines their position around the center of
    // the chart.
    for (int i = 0; i < yData.length ; i++) {
        entries.add(new PieEntry(yData[i],
                xData[i % xData.length],
                getResources().getDrawable(R.drawable.star)));
    }

    PieDataSet dataSet = new PieDataSet(entries, null);

    dataSet.setDrawIcons(false);

    dataSet.setSliceSpace(3f);
    dataSet.setIconsOffset(new MPPointF(0, 40));
    dataSet.setSelectionShift(5f);

    // add a lot of 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);
    //dataSet.setSelectionShift(0f);

    PieData data = new PieData(dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.WHITE);
    data.setValueTypeface(mTfLight);
    mChart.setData(data);

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

    mChart.invalidate();
}
private void MainChart()
{
    mTfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
    mTfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");

    mChart = (PieChart) findViewById(R.id.piechart);
    mChart.setUsePercentValues(true);
    mChart.getDescription().setEnabled(false);
    mChart.setExtraOffsets(5, 10, 5, 5);

    mChart.setDragDecelerationFrictionCoef(0.95f);

    mChart.setCenterTextTypeface(mTfLight);
    mChart.setCenterText(generateCenterSpannableText());


    mChart.setDrawHoleEnabled(true);
    mChart.setHoleColor(Color.WHITE);

    mChart.setTransparentCircleColor(Color.WHITE);
    mChart.setTransparentCircleAlpha(110);

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

    mChart.setDrawCenterText(true);

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

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

    // add a selection listener


    setData();

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

    Legend l = mChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);

    // entry label styling
    mChart.setEntryLabelColor(Color.WHITE);
    mChart.setEntryLabelTypeface(mTfRegular);
    mChart.setEntryLabelTextSize(12f);

    //Turn Off xData
    mChart.setDrawEntryLabels(!mChart.isDrawEntryLabelsEnabled());
    mChart.invalidate();
}
private SpannableString generateCenterSpannableText() {

    SpannableString s = new SpannableString("Owner's Name\nMKV\n Description");
    s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
    s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
    s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
    s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
    s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
    s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
    return s;
}

最佳答案

在代码中添加以下几行:

    pieChart.getLegend().setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);
    pieChart.getLegend().setOrientation(Legend.LegendOrientation.VERTICAL);

关于java - MPAndroidChart PieChart设置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49515950/

10-09 01:58