Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi I create a Dynamic pie chart ,how i can draw percent or other string in parts of chart?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int i1;
        int i2;
        int i3;
        int i4;
       
        float total;
        float deg1 ;
        float deg2 ;
        float deg3 ;
        float deg4;
        
        private void Form1_Load(object sender, EventArgs e)
        {
        
        }

        private void button1_Click(object sender, EventArgs e)
        {

            i1 = int.Parse(textBox1.Text);
            i2 = int.Parse(textBox2.Text);
            i3 = int.Parse(textBox3.Text);
            i4 = int.Parse(textBox4.Text);


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


            Pen p = new Pen(Color.Black, 2);

            Graphics g = this.CreateGraphics();
          
            Rectangle rec = new Rectangle(textBox1.Location.X + textBox1.Size.Width + 10, 12, 150, 150);


            Brush b1 = new SolidBrush(Color.Red);
            Brush b2 = new SolidBrush(Color.Blue);
            Brush b3 = new SolidBrush(Color.Black);
            Brush b4 = new SolidBrush(Color.BlueViolet);

            SolidBrush BlackBrush = new SolidBrush(Color.Black);

            g.Clear(Form1.DefaultBackColor);
            g.DrawPie(p, rec, 0, deg1);
            g.FillPie(b1, rec, 0, deg1);
            


            g.DrawPie(p, rec, deg1, deg2);
            g.FillPie(b2, rec, deg1, deg2);
            g.DrawPie(p, rec, deg2 + deg1, deg3);
            g.FillPie(b3, rec, deg2 + deg1, deg3);
            g.DrawPie(p, rec, deg3 + deg2 + deg1, deg4);
            g.FillPie(b4, rec, deg3 + deg2 + deg1, deg4);

        }

    }
}
Posted
Updated 12-May-11 18:29pm
v2
Comments
Sergey Alexandrovich Kryukov 13-May-11 0:27am    
Please, any expert willing to help OP, pay attention!

Before doing so, please read my answer and then examine some of the 71 past questions asked Mehdi on similar topics. Please think do you want to waste your time on advices which are not taken.
--SA
Sergey Alexandrovich Kryukov 13-May-11 0:28am    
Wrong tag, Mehdi! Why ASP.NET?!!!!! Forms!!!!!!!
--SA

1 solution

Mehdi, you have been told million of times: do not create the instance of Graphics, do not draw on Click. What you do will disappear as soon as your form is masked by some other windows or edge of the screen or minimized and then shown again.

Stop doing it! How many more answers do you need to get it? Why asking any questions if you don't follow the answers?!



You should render graphics in only in the handler of the event Paint or overridden method OnPaint and only use the instance of Graphics passed in an event arguments parameter.

All you click should only change data used in rendering and call Control.Invalidate. This is the only way you can change your graphics.


Stop asking questions here at CodeProject if you did not learn to follow the advices after you asked 70+ questions. Get some book and do elementary programming exercises.



—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900