Pull to refresh

Рисуем pie график

Reading time3 min
Views7.4K
Как-то понадобилось добавить в проект симпатичный 3-d pie график.
Пока толпа решала на какой компонент потратить денежку, нарисовал вот такой вот график:

image

Кому надо, делюсь кодом прототипа, он минимален:


    private static void Draw3DPie(int[] sectors, PaintEventArgs e)
    {
      const int x = 50;
      const int y = 50;
      int width = 300;
      int height = 120;
      int sideHeight = 40;

      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

      int sectorId = 0;
      float startAngle = 0x0;
      bool hidenSide = false;

      foreach(int sector in sectors) {
        Random rnd = new Random( (int) ( sector + DateTime.Now.Ticks + sectorId++) );
        
        Color color = Color.FromArgb(
          rnd.Next(0, 255),
          rnd.Next(0, 255),
          rnd.Next(0, 255)
          );

        if(!hidenSide) {

          GraphicsPath path = new GraphicsPath();
          GraphicsPath buff = new GraphicsPath();

          float angleBeizier = sector;
          if( startAngle + sector >= 180 ) {
            angleBeizier = 180 - startAngle;
            hidenSide = true;
          }

          path.AddArc(x, y, width, height, startAngle, angleBeizier);
          buff.AddArc(x, y + sideHeight, width, height, startAngle, angleBeizier);
      
          buff.Reverse();
          path.AddPath(buff, true);

          // Drawing 3d sides
          e.Graphics.FillPath(new SolidBrush(color), path);
          e.Graphics.FillPath(new SolidBrush(Color.FromArgb(81,0,0,0)), path);
          e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Silver), 1), path);

        }
      
        // Drawing top side
        e.Graphics.FillPie(new SolidBrush(color), x, y, width, height, startAngle, sector);
        e.Graphics.DrawPie(new Pen(new SolidBrush(Color.Silver), 1), x, y, width, height, startAngle, sector);
        
        startAngle += sector;
      }
      
    }


* This source code was highlighted with Source Code Highlighter.


Берите, не стесняйтесь =)

Tags:
Hubs:
+23
Comments36

Articles