MpAndroidChart的饼图图例在底部中心被裁剪问题

5

我已经附上了我的使用情况图表的截图。红色框中显示了图例,它们切割在饼图中。以下是我的代码:

pieChart.setUsePercentValues(false);
pieChart.getDescription().setEnabled(false);
pieChart.setDragDecelerationFrictionCoef(0.95f);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleColor(Color.WHITE);
pieChart.setTransparentCircleColor(Color.WHITE);
pieChart.setTransparentCircleAlpha(110);
pieChart.setHoleRadius(55f);
pieChart.setTransparentCircleRadius(57f);
pieChart.setDrawCenterText(true);
pieChart.setRotationAngle(0);
// enable rotation of the chart by touch
pieChart.setRotationEnabled(true);
pieChart.setHighlightPerTapEnabled(true);
// add a selection listener
pieChart.setOnChartValueSelectedListener(this);
pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

Legend l = pieChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setForm(Legend.LegendForm.CIRCLE);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
l.setWordWrapEnabled(true);
l.setDrawInside(false);
l.getCalculatedLineSizes();

// entry label styling
pieChart.setEntryLabelColor(Color.WHITE);
pieChart.setEntryLabelTextSize(12f);

有没有人知道答案? 请帮我解决这个问题。提前致谢。

在此输入图片描述

更新:

以下是我的xml文件,用于显示图表。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme"
tools:context="com.visualogyx.app.activity.SettingsActivity$PlaceholderFragment">

<RelativeLayout
    android:id="@+id/linear_change_usage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relative_text_dropdown_arraow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/light_grey">

        <TextView
            android:id="@+id/textview_usage_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="22dp"
            android:layout_marginRight="22dp"
            android:layout_marginTop="16dp"
            android:text="Tasks"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <ImageView
            android:id="@+id/dropdown_arraow"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="16dp"
            android:src="@drawable/ic_arrow_drop_down_black_24dp" />
    </RelativeLayout>

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/relative_text_dropdown_arraow" />

</RelativeLayout>

<ProgressBar
    android:id="@+id/display_chart_progress"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone" />
</RelativeLayout>

问题 - 每当我填充图形时,图例总是自动从底部裁剪。如果将相同的图例放在中心,则正常工作,但问题仅出现在底部。
4个回答

7

你尝试过使用XML属性 android:translationY =“-10dp” 进行自定义吗?

或者 view.setExtraOffsets(...);

你可以相应地管理 dp

更新

l.setYOffset(10f);


3
我已经通过在普通列表视图中添加标题来解决了这个问题。
 ListView list = (ListView) findViewById(R.id.list_chart);
 list.setAdapter(new ChartTextLegendAdapter(this, values, colors));

隐藏Legend中的标题

这是我的布局XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:background="@color/background_color">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <ListView
        android:id="@+id/list_chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/chart1" />

</RelativeLayout>

不要忘记手动传递值和颜色ArrayList。

 for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());


        values.DetailAmount = getIntent().getStringArrayListExtra("amount");
        values.DetailId = getIntent().getIntegerArrayListExtra("id");
        values.DetailName = getIntent().getStringArrayListExtra("names");
        values.DetailEnglishName = getIntent().getStringArrayListExtra("enames");
        month_name = getIntent().getStringExtra("month_name");

希望这也能解决您的问题。

谢谢你的回答。 嗨,values应该是什么,你是如何获取这些values的?能否请你发送更多关于此的代码给我呢? - Grishma Ukani
我已经将值传递给Bundle,它与您在图表中获取它的方式完全相同。请在发布后检查更新。 - AbuQauod
@GrishmaUkani,请发送您的电子邮件。 - AbuQauod

2

很可能是图表偏移量的问题。尝试这样做,应该可以解决您的问题。

pieChart.setExtraOffsets(0, 0, 0, 25);

这个函数设置额外的偏移量(围绕图表视图)以追加到自动计算的偏移量。参数为(左,上,右,下)。尝试调整数值以确定哪些偏移量适合您的需求。


通过使用这段代码,我的饼图可以向上或向下移动。但是我的图例仍然在原位,没有移动。感谢您的回答。 - Grishma Ukani

0

尝试为PieChart设置padding或margin,或者将图例水平对齐。


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