开发者

Draw Pie Chart in Android?

开发者 https://www.devze.com 2023-01-30 05:34 出处:网络
I need to draw Pie Chart (Dynamic Values).How to create the Chart without using 3rd开发者_开发百科 party API.Below shows simple pie-chart and you have to extend for more features...values[] and colors

I need to draw Pie Chart (Dynamic Values).How to create the Chart without using 3rd开发者_开发百科 party API.


Below shows simple pie-chart and you have to extend for more features...values[] and colors array should be equal....

public class Demo extends Activity {
    /** Called when the activity is first created. */
    float values[]={300,400,100,500};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout linear=(LinearLayout) findViewById(R.id.linear);
        values=calculateData(values);
        linear.addView(new MyGraphview(this,values));

    }
    private float[] calculateData(float[] data) {
        // TODO Auto-generated method stub
        float total=0;
        for(int i=0;i<data.length;i++)
        {
            total+=data[i];
        }
        for(int i=0;i<data.length;i++)
        {
        data[i]=360*(data[i]/total);            
        }
        return data;

    }
    public class MyGraphview extends View
    {
        private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        private float[] value_degree;
        private int[] COLORS={Color.BLUE,Color.GREEN,Color.GRAY,Color.CYAN,Color.RED};
        RectF rectf = new RectF (10, 10, 200, 200);
        int temp=0;
        public MyGraphview(Context context, float[] values) {

            super(context);
            value_degree=new float[values.length];
            for(int i=0;i<values.length;i++)
            {
                value_degree[i]=values[i];
            }
        }
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);

            for (int i = 0; i < value_degree.length; i++) {//values2.length; i++) {
                if (i == 0) {
                    paint.setColor(COLORS[i]);
                    canvas.drawArc(rectf, 0, value_degree[i], true, paint);
                } 
                else
                {
                        temp += (int) value_degree[i - 1];
                        paint.setColor(COLORS[i]);
                        canvas.drawArc(rectf, temp, value_degree[i], true, paint);
                }
            }
        }

    }
}


Basic function to draw a pie chart, input is an array of colors, and an array of values. They arrays must have the same size. The slices are callculated based on the values from each slide and the sum of all values.Ofcourse you can also change to float values. This solution is provided as an lightweight helper function. It's easy to use, and you do not need to define a class for it.

public static void drawPieChart(Bitmap bmp, int[] colors, int[] slices){
    //canvas to draw on it
    Canvas canvas = new Canvas(bmp);
    RectF box = new RectF(2, 2,bmp.getWidth()-2 , bmp.getHeight()-2);

    //get value for 100%
    int sum = 0;
    for (int slice : slices) {
        sum += slice;
    }
    //initalize painter
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(1f);
    paint.setStyle(Style.FILL_AND_STROKE);
    float start = 0;
    //draw slices
    for(int i =0; i < slices.length; i++){
        paint.setColor(colors[i]);
        float angle;
        angle = ((360.0f / sum) * slices[i]);
        canvas.drawArc(box, start, angle, true, paint);
        start += angle;
    }
}


The answer by Ramesh works almost fine. However the white glitch at the end is caused by rounding errors from casting float to int. Simply change the type of "temp" to float, and remove the cast to (int) at the end when "temp += value_degree[i-1]" and it's perfect.


I have a fairly simple library that is open source which you could most likely use to accomplish your purposes (https://github.com/saulpower/ExpandablePieChart).


You can use aChartEngine's org.achartengine.chart.PieChart which extends roundChart.
For more details you can go to achartengine-0.7.0-javadocs/org/achartengine/chart/PieChart.html

This will be available when you download the javadocs from aChartEngine.org

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号