Reputation: 449
I'm trying to create a piechart using data i get from my struct pie. In this struct i have 3 counters (r_count, i_count, j_count) and a total number (total_count). I used the code above but the form that comes up is empty. Any idea or solution?
float p1 = pie.r_count / pie.total_instr;
float p2 = pie.i_count / pie.total_instr;
float p3 = pie.j_count / pie.total_instr;
Graphics graphics = pictureBox1.CreateGraphics();
Rectangle rect = new Rectangle(0, 0, 150, 150);
Brush b1 = new SolidBrush(Color.Red);
Brush b2 = new SolidBrush(Color.Blue);
Brush b3 = new SolidBrush(Color.Maroon);
Pen p = new Pen(Color.Black, 2);
graphics.Clear(Form1.DefaultBackColor);
graphics.DrawPie(p, rect, 0, p1);
graphics.FillPie(b1, rect, 0, p1);
graphics.DrawPie(p, rect, p1, p2);
graphics.FillPie(b2, rect, p1, p2);
graphics.DrawPie(p, rect, p2 + p1, p3);
graphics.FillPie(b3, rect, p2 + p1, p3);
Upvotes: 1
Views: 597
Reputation: 449
i finally managed to find a solution to this problem. The code is shown below:
public partial class Form3 : Form
{
public chart pie;
Graphics graphics;
public float d1;
public float d2;
public float d3;
public Form3(chart ch)
{
InitializeComponent();
pie = ch;
graphics = pictureBox1.CreateGraphics();
}
private void Form3_Load(object sender, EventArgs e)
{
d1 = 360 * (((float)pie.r_count)/((float)pie.total_instr));
d2 = 360 * (((float)pie.i_count) / ((float)pie.total_instr));
d3 = 360 * (((float)pie.j_count) / ((float)pie.total_instr));
//comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
SolidBrush b1 = new SolidBrush (Color.Blue);
SolidBrush b2 = new SolidBrush(Color.Red);
SolidBrush b3 = new SolidBrush(Color.LawnGreen);
Rectangle rect = new Rectangle(60,10,200,200);
Pen p = new Pen(Color.Black);
graphics.DrawPie(p, rect, 0, d1);
graphics.FillPie(b1, rect, 0, d1);
graphics.DrawPie(p, rect, d1, d2);
graphics.FillPie(b2, rect, d1, d2);
graphics.DrawPie(p, rect, d1 + d2, d3);
graphics.FillPie(b3, rect, d1+d2, d3);
comboBox1.SelectedIndex = 0;
label1.Text = "R_type (" + (d1 * 100 / 360).ToString() + "%)";
label2.Text = "I_type (" + (d2 * 100 / 360).ToString() + "%)";
label3.Text = "J_type (" + (d3 * 100 / 360).ToString() + "%)";
label5.Enabled = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
label1.Text = "R_type (" + (d1 * 100 / 360).ToString() + "%)";
label2.Text = "I_type (" + (d2 * 100 / 360).ToString() + "%)";
label3.Text = "J_type (" + (d3 * 100 / 360).ToString() + "%)";
label5.Enabled = false;
}
else
{
label1.Text = "R_type (" + (pie.r_count).ToString() + ")";
label2.Text = "I_type (" + (pie.i_count).ToString() + ")";
label3.Text = "J_type (" + (pie.j_count).ToString() + ")";
label5.Text += "Total Instructions = " + pie.total_instr.ToString();
label5.Enabled = true;
}
}
And the result is shown below! Thanks everyone for your help!
Upvotes: 1
Reputation: 53958
You have to cast one of the two integers
to a float
:
float p1 = (float)pie.r_count / pie.total_instr;
or
float p1 = pie.r_count / (float)pie.total_instr;
Otherwise the result would be always 0. Why?
When we divide two integers, let's say a
and b
, then the result would be 0, if a<b
.
As you understand, you have to make the same changes for p2
and p3
.
Update
From the MSDN, we see that the signature of DrawPie
method is the following:
public void DrawPie(
Pen pen,
RectangleF rect,
float startAngle,
float sweepAngle
)
where startAngle
is an angle measured in degrees clockwise from the x-axis to the first side of the pie shape and endAngle
is an angle measured in degrees clockwise from the startAngle parameter to the second side of the pie shape.
That being said, you have to make the following change:
float p1 = ((float)pie.r_count / pie.total_instr)*360;
float p2 = ((float)pie.i_count / pie.total_instr)*360;
float p3 = ((float)pie.j_count / pie.total_instr)*360;
Upvotes: 2
Reputation: 12748
Multiply p1,p2,p3 by 360 before drawing, the DrawPie function takes degrees
float p1 = pie.r_count * 360 / pie.total_instr;
float p2 = pie.i_count * 360 / pie.total_instr;
float p3 = pie.j_count * 360 / pie.total_instr;
Upvotes: 0
Reputation: 9782
if pie.total_instr
is an int
you need this:
float p1 = pie.r_count / (float)pie.total_instr;
to avoid an integer division which is always 0 in your cases
Upvotes: 0