Search code examples
c#chartstabspie-chartgraphic

c# draw a pie chart to a tab


Pretty simple Question, unfortunatly never used draw controls etc in C# so im not sure how to look at this. Ok, im drawing a piechart from a bunch of textboxs for input and running the draw on a button event. i need to draw the chart on one of my tabs instead of the from background. how do i set it? heres my code:

      private void tempButton_Click(object sender, EventArgs e)
    {
        Rectangle tabArea;
        RectangleF tabTextArea;

        Bitmap B = new Bitmap(500, 500);

        tabArea = tabControl1.GetTabRect(0);

        tabTextArea = (RectangleF)tabControl1.GetTabRect(0);

        using (Graphics g = Graphics.FromImage(B))
        {
            int i1 = int.Parse(textBox1.Text);
            int i2 = int.Parse(textBox2.Text);
            int i3 = int.Parse(textBox3.Text);
            int i4 = int.Parse(textBox4.Text);

            float total = i1 + i2 + i3 + i4;
            float deg1 = (i1 / total) * 360;
            float deg2 = (i2 / total) * 360;
            float deg3 = (i3 / total) * 360;
            float deg4 = (i4 / total) * 360;

            Font font = new Font("Arial", 10.0f);
            SolidBrush brush = new SolidBrush(Color.Red);
            Pen p = new Pen(Color.Black, 2);
            p.Width = 0.5f;

            tabArea = new Rectangle(textBox1.Location.X + textBox1.Size.Width + 250, 150, 500, 500);

            Brush b1 = new SolidBrush(Color.Gold);
            Brush b2 = new SolidBrush(Color.Silver);
            Brush b3 = new SolidBrush(Color.DarkOrange);
            Brush b4 = new SolidBrush(Color.Black);

            g.DrawRectangle(p, tabArea);

            g.DrawPie(p, tabTextArea, 0, deg1);
            g.FillPie(b1, tabArea, 0, deg1);
            g.DrawPie(p, tabTextArea, deg1, deg2);
            g.FillPie(b2, tabArea, deg1, deg2);
            g.DrawPie(p, tabTextArea, deg2 + deg1, deg3);
            g.FillPie(b3, tabArea, deg2 + deg1, deg3);
            g.DrawPie(p, tabTextArea, deg3 + deg2 + deg1, deg4);
            g.FillPie(b4, tabArea, deg3 + deg2 + deg1, deg4);

            //set picturebox3 as data source??
            pictureBox3.Image = B;

        }
    }

As you can see when i click the test button it draws the chart but behind my tab area, i need it to draw onto one of my tabs(i got a feeling this is super easy 1line solution, but google is not my friend atm ). Many thanks in advance!


Solution

  • The simplest solution is to create a bitmap of the desired dimensions, create Graphics for this bitmap, do the drawing and then set this bitmap as the image source for an auto-sizing picture box you place on the tab. This is the cleanest way.

    UPDATE
    I noted in my comments that your drawing code is not well thought through. Change the first lines as follows:

        Rectangle tabArea;
        RectangleF tabTextArea;
    
        Bitmap B = new Bitmap(500, 500, PixelFormat.Format32bppArgb);
    
        tabArea = new Rectangle(0, 0, B.Width, B.Height);
        tabTextArea = new RectangleF(0, 0, B.Width, B.Height);
    

    Also: Determining tabArea based on control locations is not a good idea. Lastly: Set the "SizeMode" property to "AutoSize" to have the picture box stretch to the bitmap dimension.