MPAndroidChart PieChart如何更改中心文本以显示不同的颜色和字体大小

时间:2015-09-06 02:43:02

标签: android mpandroidchart

目前我使用GITHUB的MPAndroidChart并绘制饼图以显示两行文本,每行有不同的颜色和不同的字体大小,我检查源代码,中心文本是一个字符串对象,并尝试使用:

PAINT paint = pie_chart.getPaint(Chart.PAINT_CENTER_TEXT);

但看起来不起作用,有人有经验去做并指导我。

4 个答案:

答案 0 :(得分:4)

在研究了源代码之后,我改变了PieChartRenderer.java来改变它 mCenterTextPaint.setColor(Color.parseColor("#333333")); mCenterTextPaint.setTextSize(Utils.convertDpToPixel(20f)); mCenterTextPaint.setTextAlign(Align.CENTER);

然后使用“\ n”作为分割来获取第一行

   int index = centerText.indexOf("\n");

   Spannable tempSpannable = new SpannableString(centerText);
   tempSpannable.setSpan(new RelativeSizeSpan(0.75f), 0, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   tempSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")),
                        0, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

   // If width is 0, it will crash. Always have a minimum of 1
   mCenterTextLayout = new StaticLayout(tempSpannable, 0, centerText.length(),
                        mCenterTextPaint,
                        (int)Math.max(Math.ceil(mCenterTextLastBounds.width()), 1.f),
                        Layout.Alignment.ALIGN_NORMAL, 1.f, 0.f, false);
然后得到我想要的东西 enter image description here

答案 1 :(得分:1)

PieData data = new PieData(dataSet);  
pieChart.setCenterText("Total Questions 5" );        
pieChart.setCenterTextSize(14f);
pieChart.setCenterTextColor(Color.BLUE);

答案 2 :(得分:0)

您可以通过在活动布局中将com.github.mikephil.charting.charts.PieChart组件和中心文本嵌套在RelativeLayout中来修改MPAndroidChart源,从而实现此目的。您可以使用LinearLayout组件格式化中心文本行。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    >
    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/pieChart"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.github.mikephil.charting.charts.PieChart>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">
        <TextView
            android:id="@+id/pieChartMonthlyAverage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/ChartStat"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/ChartStatLabel"
            android:text="@string/charts.label.average"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/ChartStatLabel"
            android:layout_marginTop="-4dp"
            android:text="@string/charts.label.monthly"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/ChartStatLabel"
            android:layout_marginTop="-4dp"
            android:text="@string/charts.label.bill"/>
    </LinearLayout>
</RelativeLayout>

小心将包含中心文字的LinearLayout放在下面的PieChart组件中。这是最终结果。

enter image description here

答案 3 :(得分:0)

在图表上使用public void setCenterText(CharSequence text),并传递SpannableString作为其参数。根据您的内心需求调整SpannableString的风格。

相关问题