【问题标题】:MPAndroidChart PieChart how to change the center text to display the different color and font sizeMPAndroidChart PieChart 如何更改中心文本以显示不同的颜色和字体大小
【发布时间】:2015-09-06 02:43:02
【问题描述】:

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

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

但看起来行不通,有人有经验可以指导我吗?

【问题讨论】:

    标签: android mpandroidchart


    【解决方案1】:

    研究了源代码后,我更改了 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);
    

    然后得到我想要的是

    【讨论】:

    • 如何设置文字居中?
    • @KaranKhurana 您可以使用以下方法动态设置中心文本:使用 chart.setOnChartValueSelectedListener 方法。
    【解决方案2】:
    PieData data = new PieData(dataSet);  
    pieChart.setCenterText("Total Questions 5" );        
    pieChart.setCenterTextSize(14f);
    pieChart.setCenterTextColor(Color.BLUE);
    

    【讨论】:

      【解决方案3】:

      在图表上使用public void setCenterText(CharSequence text) 并传递SpannableString 作为其参数。随心所欲地设计您的SpannableString

      【讨论】:

        【解决方案4】:

        您可以在不修改 MPAndroidChart 源代码的情况下通过将 com.github.mikephil.charting.charts.PieChart 组件和中心文本嵌套在活动布局中的 RelativeLayout 中来完成此操作。您可以使用 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 组件下方。这是最终结果。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-23
          • 2014-07-03
          • 1970-01-01
          • 2016-07-03
          • 1970-01-01
          • 2015-02-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多