Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help with this PieChart control

Help with this PieChart control

Scheduled Pinned Locked Moved C#
graphicshelpcomdesigntutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Zhao
    wrote on last edited by
    #1

    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);
    
    L 1 Reply Last reply
    0
    • A Alan Zhao

      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);
      
      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You should use Matrix transforms to rotate the drawing "canvas".

      top secret AdvancedTextBox

      A 1 Reply Last reply
      0
      • L leppie

        You should use Matrix transforms to rotate the drawing "canvas".

        top secret AdvancedTextBox

        A Offline
        A Offline
        Alan Zhao
        wrote on last edited by
        #3

        Can you explain please, I really want this control to work. Thank!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups