円グラフを描く

どういうわけか、素敵な3D円グラフをプロジェクトに追加する必要がありました。

群衆がどのコンポーネントにお金を使うかを決定している間に、私はそのようなグラフを描きました。



画像



必要な人は誰でも、プロトタイプコードを共有します。最小限です。





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 .








それを取り、恥ずかしがらないでください=)






All Articles