简体   繁体   English

在饼图上显示数字

[英]display numbers on pie chart

I've developed an android application to display a pie chart.我开发了一个 android 应用程序来显示饼图。 I have used the achartengine library to do this.我已经使用了achartengine库来做到这一点。 With this code below I get the pie chart output with 3 portions labeled as mentioned in categorySeries.使用下面的代码,我得到饼图 output,其中 3 个部分标记为 categorySeries 中提到的。 I want to display the percentage values on the pie diagram.我想在饼图上显示百分比值。 How can i do this?我怎样才能做到这一点?

public static final GraphicalView getPieChartView(Context context,
             CategorySeries dataset, DefaultRenderer renderer) {
         checkParameters(dataset, renderer);
         PieChart chart = new PieChart(dataset, renderer);

         return new GraphicalView(context, chart);
     }

     private static void checkParameters(CategorySeries dataset,
             DefaultRenderer renderer) {
         if (dataset == null
             || renderer == null
             || dataset.getItemCount() != renderer
                .getSeriesRendererCount()) {
             throw new IllegalArgumentException(
                     "Dataset and renderer should be not null and the dataset number of items should be equal to the number of series renderers");
         }
     }     

and this is my onCreate method这是我的 onCreate 方法

 Bundle bundle = getIntent().getExtras();
 int highc = bundle.getInt("high");
 int lowc = bundle.getInt("low");
 int medc = bundle.getInt("medium");



RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativestatsLayout);
TextView message = (TextView) findViewById(R.id.statText);
message.setText("\n\n\tTickets Summary");
message.setGravity(50);
//layout.addView(message);
//setContentView(message);


int[] colors = new int[] { Color.rgb(255, 0, 0),Color.rgb(220, 51,51), Color.rgb(255, 191, 0) };
DefaultRenderer renderer = buildCategoryRenderer(colors);

CategorySeries categorySeries = new CategorySeries("Tickets Chart");
categorySeries.getTitle();
categorySeries.add("critical", highc);
categorySeries.add("major ", medc);
categorySeries.add("minor ", lowc);



layout.addView(getPieChartView(this, categorySeries, renderer));
}

protected DefaultRenderer buildCategoryRenderer(int[] colors) {
    DefaultRenderer renderer = new DefaultRenderer();
    for (int color : colors) {
       SimpleSeriesRenderer r = new SimpleSeriesRenderer();
       r.setColor(color);

       renderer.addSeriesRenderer(r);
    }
    return renderer;
}

It is possible using achartengine.可以使用 achartengine。 I have faced the same problem and found the answer now.我遇到了同样的问题,现在找到了答案。 Use the 1.1.0-rc2 version of achart engine.使用 1.1.0-rc2 版本的 chart 引擎。 You can download it here: http://www.achartengine.org/download/你可以在这里下载: http://www.achartengine.org/download/

This tutorial ( http://wptrafficanalyzer.in/blog/android-drawing-pie-chart-using-achartengine/ ) helps you to create pie chart using achartengine.本教程( http://wptrafficanalyzer.in/blog/android-drawing-pie-chart-using-achartengine/ )帮助您使用 achartengine 创建饼图。

Add this line to your code to display the %of distribution in the Pie chart将此行添加到您的代码中以在饼图中显示 %of 分布

defaultRenderer.setDisplayValues(true);

Reference: How to set different values for Legend and Labels in piechart While I am using Chart Engine in android参考: 当我在 android 中使用图表引擎时,如何在饼图中为图例和标签设置不同的值

Im not sure if you still have this issue.我不确定你是否还有这个问题。 But if your pie chart is fairly simple, you should consider drawing it your self rather using a library for it.但是,如果您的饼图相当简单,您应该考虑自己绘制它,而不是使用库。 This might help a bit.这可能会有所帮助。

https://github.com/blessenm/PieChartDemo https://github.com/blessenm/PieChartDemo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM