Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:45:33+00:00 2026-05-30T13:45:33+00:00

I have used to draw pie chart using canvas.. There are approximately 10 arcs

  • 0

I have used to draw pie chart using canvas..
There are approximately 10 arcs in pie chart..i want to perform click event on each arc.
Is there any way to do this? or any other way?

This is my pie chart view..

MyView.java

package android.piechart;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {

private Paint p;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Integer> values;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    p = new Paint();
    p.setColor(Color.BLUE);
    p.setAntiAlias(true);

    colors = new ArrayList<Integer>();
    values = new ArrayList<Integer>();

    startX = 320 / 4;
    startY = 480 / 8;
    radius = 320 / 2;

    colors.add(Color.GREEN);
    colors.add(Color.CYAN);
    colors.add(Color.MAGENTA);
    colors.add(Color.BLUE);
    colors.add(Color.RED);

    values.add(0);
    values.add(1);
    values.add(3);
    values.add(0);
    values.add(2);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Log.e("", "onDraw() is called...");

    float offset = 0;
    float sum = 0;
    for (int a = 0; a < values.size(); a++) {
        sum += values.get(a);
    }

    float angle = (float) (360 / sum);

    Log.e("angle", "" + angle);

    RectF rectF = new RectF();
    rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
            getStartY() + getRadius());

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

        p.setColor(colors.get(i));

        if (i == 0) {
            canvas.drawArc(rectF, 0, values.get(i) * angle, true, p);
        } else {
            canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
        }

        offset += (values.get(i) * angle);
    }

    canvas.save();
}

public int getStartX() {
    return startX;
}

public void setStartX(int startX) {
    this.startX = startX;
}

public int getStartY() {
    return startY;
}

public void setStartY(int startY) {
    this.startY = startY;
}

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}

public ArrayList<Integer> getColors() {
    return colors;
}

public void setColors(ArrayList<Integer> colors) {
    this.colors = colors;
}

public ArrayList<Integer> getValues() {
    return values;
}

public void setValues(ArrayList<Integer> values) {
    this.values = values;
}
}

Thanks in advance..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:45:35+00:00Added an answer on May 30, 2026 at 1:45 pm

    I solved my question myself…

    MyView.java

    public class MyView extends View {
    
    private Paint p;
    private int startX;
    private int startY;
    private int radius;
    private ArrayList<Integer> colors;
    private ArrayList<Float> values;
    Bitmap bitmap;
    Context mContext;
    
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        mContext = context;
    
        p = new Paint();
        p.setAntiAlias(true);
    
        colors = new ArrayList<Integer>();
        values = new ArrayList<Float>();
    
        startX = 320 / 4;
        startY = 480 / 8;
        radius = 320 / 2;
    
        colors.add(Color.GREEN);
        colors.add(Color.CYAN);
        colors.add(Color.MAGENTA);
        colors.add(Color.BLUE);
        colors.add(Color.RED);
    
        values.add(5f);
        values.add(1f);
        values.add(3f);
        values.add(5f);
        values.add(2f);
    
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                Bitmap.Config.ARGB_8888);
    
        Canvas c = new Canvas(bitmap);
    
        Log.e("", "onDraw() is called...");
    
        float offset = 0;
        float sum = 0;
        for (int a = 0; a < values.size(); a++) {
            sum += values.get(a);
        }
    
        float angle = (float) (360 / sum);
    
        Log.e("angle", "" + angle);
    
        RectF rectF = new RectF();
        rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
                getStartY() + getRadius());
    
        for (int i = 0; i < values.size(); i++) {
    
            p.setColor(colors.get(i));
    
            if (i == 0) {
                canvas.drawArc(rectF, 0, values.get(i) * angle, true, p);
                c.drawArc(rectF, 0, values.get(i) * angle, true, p);
            } else {
                canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
                c.drawArc(rectF, offset, values.get(i) * angle, true, p);
            }
    
            offset += (values.get(i) * angle);
        }
    
        canvas.save();
    
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        int color = bitmap.getPixel((int) event.getX(), (int) event.getY());
    
        Log.e("", "" + color);
    
        if (colors.contains(color)) {
            Log.e("", "is matching");
            if (color == Color.RED) {
                Toast.makeText(mContext, "Is Red", Toast.LENGTH_SHORT).show();
            }
    
            if (color == Color.CYAN) {
                Toast.makeText(mContext, "Is Cyan", Toast.LENGTH_SHORT).show();
            }
    
            if (color == Color.MAGENTA) {
                Toast.makeText(mContext, "Is MAGENTA", Toast.LENGTH_SHORT)
                        .show();
            }
            if (color == Color.BLUE) {
                Toast.makeText(mContext, "Is BLUE", Toast.LENGTH_SHORT).show();
            }
            if (color == Color.GREEN) {
                Toast.makeText(mContext, "Is GREEN", Toast.LENGTH_SHORT).show();
            }
        }
    
        return super.onTouchEvent(event);
    }
    
    public int getStartX() {
        return startX;
    }
    
    public void setStartX(int startX) {
        this.startX = startX;
    }
    
    public int getStartY() {
        return startY;
    }
    
    public void setStartY(int startY) {
        this.startY = startY;
    }
    
    public int getRadius() {
        return radius;
    }
    
    public void setRadius(int radius) {
        this.radius = radius;
    }
    
    public ArrayList<Integer> getColors() {
        return colors;
    }
    
    public void setColors(ArrayList<Integer> colors) {
        this.colors = colors;
    }
    
    public ArrayList<Float> getValues() {
        return values;
    }
    
    public void setValues(ArrayList<Float> values) {
        this.values = values;
    }
    
    }
    

    I hope it’s useful to others…

    piechart

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I draw such a graph using Swing? I have used a JFreeChart
I have used getopt in Python and was hoping there would be something similar
T have used checkbox column in gridview. On click of a linkbutton, it should
I have used checkbox column in gridview. I want to check status of that
I am using phonegap to develop my android application.I want to show google chart
I want to draw a shape(many circles particularly) into a Specific Bitmap. I have
I have never used Can draw concurrently option mentioned in iterface builder. See image
As you can see, Y! messenger 11 have used some techniques to draw a
I am a beginner at using openGL. I have used a program which I
I have used this code to draw a set of ellipses in C# but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.