使用3个整数的值创建饼图

时间:2015-01-17 16:34:12

标签: c# .net pie-chart

我尝试使用从结构饼中获取的数据来创建饼图。在这个结构中,我有3个计数器(r_count,i_count,j_count)和一个总数(total_count)。我使用了上面的代码但是出现的表单是空的。任何想法或解决方案?

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);

4 个答案:

答案 0 :(得分:2)

您必须将两个integers中的一个投放到float

float p1 = (float)pie.r_count / pie.total_instr;

float p1 = pie.r_count / (float)pie.total_instr;

否则结果将始终为0.为什么?

当我们划分两个整数时,让我们说ab,那么结果将是0,如果a<b

根据您的理解,您必须对p2p3进行相同的更改。

<强>更新

MSDN,我们看到DrawPie方法的签名如下:

public void DrawPie(
    Pen pen,
    RectangleF rect,
    float startAngle,
    float sweepAngle
)

其中startAngle是从饼形的x轴到第一边顺时针测量的角度,endAngle是从startAngle参数到顺时针的度数测量的角度馅饼形状的第二面

话虽如此,你必须做出以下改变:

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;

答案 1 :(得分:1)

我终于找到了解决这个问题的方法。代码如下所示:

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;
        }
    }

结果如下所示!谢谢大家的帮助!

enter image description here

答案 2 :(得分:0)

如果pie.total_instrint你需要这个:

float p1 = pie.r_count / (float)pie.total_instr;

避免在你的情况下始终为0的整数除法

答案 3 :(得分:0)

在绘制前将p1,p2,p3乘以360,DrawPie函数取度

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;