自定义Android AChartEngine PieChart(在图表中显示文字)

时间:2016-12-07 11:40:24

标签: android achartengine

有没有办法更新饼图内的文本(通过achartEngine API生成)。我想在那里显示百分比。在下图中,我手动更新(即60%,30%和10%)以向您显示。

Image1

我在SimpleSeriesRenderer类中找不到相关方法。这是我正在使用的代码。

CategorySeries categorySeries = new CategorySeries("Chart");

for (int i = 0; i < series.size(); i++) {
    categorySeries.add(series.get(i), series_data.get(i));
}

DefaultRenderer defaultRenderer = new DefaultRenderer();

defaultRenderer.setBackgroundColor(Color.BLACK);
defaultRenderer.setShowLabels(true);
defaultRenderer.setShowLegend(true);
defaultRenderer.setShowTickMarks(true);
defaultRenderer.setLabelsTextSize(40f);
defaultRenderer.setLegendTextSize(40f);
defaultRenderer.setApplyBackgroundColor(true);

for (int i = 0; i < series_data.size(); i++) {

    SimpleSeriesRenderer simpleSeriesRenderer = new SimpleSeriesRenderer();
    simpleSeriesRenderer.setColor(  chartColor[i]);
    defaultRenderer.addSeriesRenderer(simpleSeriesRenderer);
}

1 个答案:

答案 0 :(得分:0)

您可以创建自定义类,其扩展PieChart并覆盖它的drawLabel()(可能是draw())方法:

public class AdvancedPieChart extends PieChart {

   @Override
   protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer, List<RectF> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line, boolean display) {
      // do what You want with Label text, e.g. just
      canvas.drawText(labelText, xLabel, yLabel, paint);
   }

}

并使用您的AdvancedPieChart课程代替“标准”PieChart

<强> 更新

例如,这个实现:

public class AdvancedPieChart extends PieChart {

    public AdvancedPieChart(CategorySeries dataset, DefaultRenderer renderer) {
        super(dataset, renderer);
    }


    @Override
    protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer, List<RectF> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line, boolean display) {
        super.drawLabel(canvas, labelText, renderer, prevLabelsBounds, centerX, centerY, shortRadius, longRadius, currentAngle, angle, left, right, color, paint, line, display);

        String additionalText = "Hello";
        Paint textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(18 * Resources.getSystem().getDisplayMetrics().densityDpi / 160f);
        textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
        Rect bounds = new Rect();
        textPaint.getTextBounds(additionalText, 0, additionalText.length(), bounds);
        canvas.drawText(additionalText, this.mCenterX - bounds.width() / 2, this.mCenterY - bounds.height() / 3, textPaint);
    }
}

在图表中心绘制文字“Hello”:

AdvancedPieChart screenshot

布局(activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ua.com.omelchenko.chartapplication.MainActivity">

    <RelativeLayout
        android:id="@+id/chart_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RelativeLayout>

</RelativeLayout>

(当然你不需要嵌套布局)

MainActivity.java是:

public class MainActivity extends AppCompatActivity {

    private RelativeLayout mPieChartHolder;
    private GraphicalView mChartView;
    private PieChart mPieChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] names = new String[] { "Category 1", "Category 2", "Category 3" };
        double[] values = { 20, 30, 50 };
        int[] colors = { Color.RED, Color.GREEN, Color.BLUE };

        CategorySeries categorySeries = new CategorySeries("Pie Chart");
        for (int i = 0; i < names.length; i++) {
            categorySeries.add(names[i], values[i]);
        }

        DefaultRenderer defaultRenderer = new DefaultRenderer();
        for (int i = 0; i < values.length; i++) {
            SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
            seriesRenderer.setColor(colors[i]);
            defaultRenderer.setBackgroundColor(Color.BLACK);
            defaultRenderer.setLabelsTextSize(20f);
            defaultRenderer.setLegendTextSize(20f);
            defaultRenderer.setApplyBackgroundColor(true);
            defaultRenderer.addSeriesRenderer(seriesRenderer);
        }

        mPieChart = new AdvancedPieChart(categorySeries, defaultRenderer);
        mChartView = new GraphicalView(this, mPieChart);

        mPieChartHolder = (RelativeLayout) findViewById(R.id.chart_holder);
        mPieChartHolder.addView(mChartView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    }

}

(当然这只是演示)。

如果您在AndroidStudio 2.0及更高版本中鼠标指针位于Ctrl+LeftMouseButton字词时按PieChart您可以找到许多有关PieChart内部组织的有趣信息,例如馅饼中心的坐标等