java - MPAndroidChart 饼图设置

标签 java android

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

PieChart

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 饼图设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515950/

相关文章:

Java 默认浏览器名称

java - 我需要添加哪个 .jar 文件以删除 netbeans 中的 thease 行中的错误

java - 保存关闭/打开介绍部分之间的状态

java - 合并两个 .m4a 文件或将 .wav 文件转换为 m4a

android - 以编程方式切换应用程序范围的主题?

java - 从 Java 1.4 升级到 JAVA 6 SE

java - JBOSS 7+ servlet 可以监听/接管另一个子部署的请求吗?

java - 带有 SurfaceView 和逻辑单元的 Android 游戏

android - drawRect单元问题android

android - 将 App 与 Epub 格式相关联