.NET pie chart: how to add text to slicers and rotate a graph

The code below creates a circular pie chart. Like me:

  • Add text labels to each slice a la Wheel of Fortune.
  • Rotate a pie chart? I want it to spin like the Wheel of Fortune.

    private void DrawPieChart()
    {
        Graphics g = this.CreateGraphics();
        g.Clear(this.BackColor);
        Rectangle rect = new Rectangle(0, 0, 300, 300);
        float angle = 0;
        Random random = new Random();
        int sectors = 24;
        int sweep = 360 / sectors;
    
         for(int i=0; i<24;i++)
        {
            Color clr = Color.FromArgb(random.Next(0, 255),random.Next(0, 255), random.Next(0, 255));
            g.FillPie(new SolidBrush(clr), rect, angle, sweep);
            angle += sweep;
        }
        g.Dispose();
    }
    
+3
source share
1 answer

To add text labels, call g.DrawString.

. , Graphics angle + sweep / 2 . , yopu ; , vy chaaracter g.MeasureString, , .

, g.RotateTransform . EDIT: :

    private void DrawPieChart()
    {
        Graphics g = this.CreateGraphics();
        g.Clear(this.BackColor);
        Rectangle rect = new Rectangle(0, 0, 300, 300);
        float angle = 0;
        Random random = new Random();
        int sectors = 24;
        int sweep = 360 / sectors;

         g.RotateTransform(90);        //Rotates by 90 degrees
         for(int i=0; i<24;i++)
        {
            Color clr = Color.FromArgb(random.Next(0, 255),random.Next(0, 255), random.Next(0, 255));
            g.FillPie(new SolidBrush(clr), rect, angle, sweep);
            angle += sweep;
        }
        g.Dispose();
    }

, , g.RotateTransform.

, Paint e.Graphics. , , Invalidate. , this.SetStyle(ControlStyles.DoubleBuffer, true); .

+2

Source: https://habr.com/ru/post/1711038/


All Articles