Click here to Skip to main content
15,887,477 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem with app.config and myfile.exe.config Pin
Heath Stewart2-May-04 6:37
protectorHeath Stewart2-May-04 6:37 
GeneralC#/C++ interoperability Pin
sharonz1-May-04 20:38
sharonz1-May-04 20:38 
GeneralRe: C#/C++ interoperability Pin
Heath Stewart2-May-04 6:34
protectorHeath Stewart2-May-04 6:34 
GeneralTransparent Controls Pin
DougW481-May-04 19:24
DougW481-May-04 19:24 
GeneralRe: Transparent Controls Pin
Heath Stewart2-May-04 6:29
protectorHeath Stewart2-May-04 6:29 
GeneralDesigner Defaults Pin
gUrM33T1-May-04 19:10
gUrM33T1-May-04 19:10 
GeneralRe: Designer Defaults Pin
leppie1-May-04 22:38
leppie1-May-04 22:38 
GeneralHelp with this PieChart control Pin
Alan Zhao1-May-04 16:51
Alan Zhao1-May-04 16:51 
hi,

I've been coding this PieChart control all day now, just couldn't get it to work, if you know how to write a custom control, plese help me.

The main problem I have here is at the OnPaint() the PieChart doesn't draw all the pies except the first pie. What am I missing on writing a custom control here?

Complete codes are below or you can download the project here

PieChart.cs

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Drawing.Design; 
using System.Data; 
using System.Windows.Forms; 

namespace ColorControl.Controls 
{ 
    public class PieChart : System.Windows.Forms.Control 
    { 
        // collection 
        private PieChartItemCollection _Items = new PieChartItemCollection(); 

        public PieChart() 
        { 
            base.Size = new Size(200, 200); 
            SetStyle( 
                ControlStyles.AllPaintingInWmPaint | 
                ControlStyles.ResizeRedraw | 
                ControlStyles.DoubleBuffer | 
                ControlStyles.UserPaint, 
                true 
                ); 
        } 
         
        public PieChartItemCollection Items 
        { 
            get 
            { 
                return _Items; 
            } 
            set 
            { 
                _Items = value; 
                this.Invalidate(); 
            } 
        } 

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
        { 
            int valueSum = 0; 
            int startDegree = 0; 
            int sweepDegree; 

            // sum of values 
            foreach (PieChartItem item in _Items) 
            { 
                valueSum += (int)item.Value; 
            } 

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 
            Rectangle rect = new Rectangle(ClientRectangle.X, ClientRectangle.Y, 
                ClientRectangle.Width - 1, ClientRectangle.Height - 1); 

            if (valueSum != 0) 
            { 
                // draw each pie 
                foreach (PieChartItem item in _Items) 
                { 
                    // degree of each pie 
                    sweepDegree = (int)(item.Value / valueSum * 360); 

                    SolidBrush brush = new SolidBrush(item.Color); 
                    e.Graphics.FillPie(brush, rect, startDegree, sweepDegree); 
                    if (_Items.Count > 1) 
                    { 
                        e.Graphics.DrawPie(new Pen(Color.Black), rect, startDegree, sweepDegree); 
                    } 
                    startDegree += sweepDegree; 
                } 
            } 
            // draw border 
            e.Graphics.DrawEllipse(new Pen(Color.Black, 2), rect); 
        } 
    } 
}


PieChartItem.cs

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Windows.Forms; 

namespace ColorControl.Controls 
{ 
    [DefaultProperty("Value")] 
    public class PieChartItem 
    { 
        private Color _Color; 
        private string _Text; 
        private int _Value; 

        public PieChartItem() 
        { 
            _Color = Color.Red; 
            _Text = "Item"; 
            _Value = 0; 
        } 
     
        public Color Color 
        { 
            get 
            { 
                return _Color; 
            } 
            set 
            { 
                _Color = value; 
            } 
        } 

        public string Text 
        { 
            get 
            { 
                return _Text; 
            } 
            set 
            { 
                _Text = value; 
            } 
        } 

        public int Value 
        { 
            get 
            { 
                return _Value; 
            } 
            set 
            { 
                _Value = value; 
            } 
        } 
    } 
}


PieChartItemCollection.cs

using System; 
using System.Collections; 

namespace ColorControl.Controls 
{ 
    public class PieChartItemCollection : CollectionBase 
    { 
        public PieChartItemCollection() : base() 
        {} 

        public PieChartItem this[int index] 
        { 
            get 
            { 
                if (index >= 0 && index < base.Count) 
                    return (base.List[index] as PieChartItem); 
                else 
                    return null; 
            } 
            set 
            { 
                base.List[index] = value; 
            } 
        } 

        public void Add(PieChartItem item) 
        { 
            base.List.Add(item as object); 
        } 

        public void AddRange(PieChartItem[] items) 
        { 
            foreach (PieChartItem item in items) 
            { 
                Add(item); 
            } 
        } 

        public void Insert(int index, PieChartItem item) 
        { 
            if (index >= 0) 
            { 
                base.List.Insert(index, item as object); 
            } 
        } 

        public void Remove(PieChartItem item) 
        { 
            base.List.Remove(item as object); 
        } 

        public void RemoveAt(int index) 
        { 
            if (index >= 0 && index < base.Count) 
            { 
                base.RemoveAt(index); 
            } 
        } 

        public void RemoveAll() 
        { 
            for (int index = 0; index < base.Count; index++) 
            { 
                base.List.RemoveAt(index); 
            } 
        } 

        public bool Contains(PieChartItem item) 
        { 
            return base.List.Contains(item as object); 
        } 


        public int IndexOf(PieChartItem item) 
        { 
            return base.List.IndexOf(item as object); 
        } 
    } 
}


There is also a small problem is that all "Pies" of PieChart have the same color, I will solve this as soon as I solve the previous problem.

Thank!!!Smile | :)
GeneralRe: Help with this PieChart control Pin
leppie1-May-04 22:31
leppie1-May-04 22:31 
GeneralRe: Help with this PieChart control Pin
Alan Zhao2-May-04 13:19
Alan Zhao2-May-04 13:19 
GeneralDeployment to Quick Launch Toolbar Pin
betterc1-May-04 13:18
betterc1-May-04 13:18 
GeneralRe: Deployment to Quick Launch Toolbar Pin
gUrM33T1-May-04 18:07
gUrM33T1-May-04 18:07 
GeneralRe: Deployment to Quick Launch Toolbar Pin
betterc2-May-04 6:08
betterc2-May-04 6:08 
GeneralRe: Deployment to Quick Launch Toolbar Pin
Heath Stewart2-May-04 6:26
protectorHeath Stewart2-May-04 6:26 
GeneralRe: Deployment to Quick Launch Toolbar Pin
betterc2-May-04 6:36
betterc2-May-04 6:36 
GeneralRe: Deployment to Quick Launch Toolbar Pin
Heath Stewart2-May-04 6:56
protectorHeath Stewart2-May-04 6:56 
GeneralRe: Deployment to Quick Launch Toolbar Pin
betterc2-May-04 7:00
betterc2-May-04 7:00 
GeneralRe: Deployment to Quick Launch Toolbar Pin
Heath Stewart2-May-04 7:04
protectorHeath Stewart2-May-04 7:04 
GeneralRe: Deployment to Quick Launch Toolbar Pin
betterc2-May-04 7:16
betterc2-May-04 7:16 
GeneralRe: Deployment to Quick Launch Toolbar Pin
Heath Stewart2-May-04 6:20
protectorHeath Stewart2-May-04 6:20 
QuestionFull Screen / Change Reso? Pin
Acidon1-May-04 12:05
Acidon1-May-04 12:05 
AnswerRe: Full Screen / Change Reso? Pin
Meysam Mahfouzi1-May-04 16:37
Meysam Mahfouzi1-May-04 16:37 
GeneralRe: Full Screen / Change Reso? Pin
Acidon1-May-04 17:37
Acidon1-May-04 17:37 
GeneralRe: Full Screen / Change Reso? Pin
Heath Stewart2-May-04 6:19
protectorHeath Stewart2-May-04 6:19 
GeneralRe: Full Screen / Change Reso? Pin
Acidon2-May-04 9:35
Acidon2-May-04 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.