Reputation: 150
I'm drawing a circle in C# and i have divided it into some parts,i want to fill different parts with different colors,is there anyway to do this? and how?i tried using fillpie() but i couldn't get the arguments to work.
here is the code:
int r = 150;
g.DrawEllipse(Pens.Black, 300 - r, 250 - r, 2 * r, 2 * r);
if (p != 0)
g.DrawLine(Pens.Black, 300, 250, 300 + r, 250);
double sum;
sum = 0.0;
for (int j = 0; j < p; j++)
sum += data[j].value;
double angle;
angle = 0.0;
for (int i = 0; i < p; i++)
{
angle += (double)(data[i].value / sum) * 2.0 * Math.PI;
textBox1.Text += sum.ToString() + " : " + angle.ToString() + ":" + Math.Cos(angle).ToString() + "\r\n";
g.DrawLine(Pens.Black, 300, 250, 300 + (int)(Math.Cos(angle) * r), 250 - (int)(Math.Sin(angle) * r));
//g.FillPie(Brushes.Black, 300-r , 250 - r, r, r ,(float)(angle),(float)(angle+ (data[i].value / sum) * 2.0 * Math.PI));
}
this actually divides the circle into different parts,i don't know how to fill them
the commented line is where i
Upvotes: 1
Views: 1575
Reputation: 2523
e.Graphics.FillPie(new SolidBrush(Color.Red,0, 0,45,45,0,30)
Upvotes: 0
Reputation: 25206
Supposing you are using WinForms, the MSDN hase some nice and easy example for the FillPie() method.
public void FillPieRectangle(PaintEventArgs e)
{
// Create solid brush.
SolidBrush redBrush = new SolidBrush(Color.Red);
// Create rectangle for ellipse.
Rectangle rect = new Rectangle(0, 0, 200, 100);
// Create start and sweep angles.
float startAngle = 0.0F;
float sweepAngle = 45.0F;
// Fill pie to screen.
e.Graphics.FillPie(redBrush, rect, startAngle, sweepAngle);
}
EDIT:
It looks like you actually want to draw some kind of pie chart, but your code looks way to complicated. Take a look at this article that might give you some help.
Upvotes: 2