C#円餅分析図を描く
Code:
呼び出し:
using System;
using System.Drawing;
namespace aa {
public class Pie
{
private Graphics objGraphics; //Graphics
private Bitmap objBitmap; //
private int m_Width = 700; //
private int m_Height = 400; //
private string m_Title = "";
private string m_Unit = " ";
//
private string[] m_Keys = new string[]{" "," "," "," "," "," "," "," "," "," "," "," "};
//
private float[] m_Values = new float[]{21.5f,36.2f,48.7f,55.4f,21.6f,12.8f,99.5f,36.4f,78.2f,56.4f,45.8f,66.4f};
private Color m_BgColor = Color.Snow; //
private Color m_TextColor = Color.Black; //
private Color m_BorderColor = Color.Black; //
//Pie
private Color[] m_PieColor = new Color[]{Color.Green,Color.Red,Color.Yellow,Color.Blue,Color.Orange,Color.Aqua,Color.SkyBlue,Color.DeepPink,Color.Azure,Color.Brown};
public int Width
{
set
{
if(value < 300)
{
m_Width = 300;
}
else
{
m_Width = value;
}
}
get
{
return m_Width;
}
}
public int Height
{
set
{
if(value < 300)
{
m_Height = 300;
}
else
{
m_Height = value;
}
}
get
{
return m_Height;
}
}
public string Title
{
set
{
m_Title = value;
}
get
{
return m_Title;
}
}
public string Unit
{
set
{
m_Unit = value;
}
get
{
return m_Unit;
}
}
public string[] Keys
{
set
{
m_Keys = value;
}
get
{
return m_Keys;
}
}
public float[] Values
{
set
{
m_Values = value;
}
get
{
return m_Values;
}
}
public Color BgColor
{
set
{
m_BgColor = value;
}
get
{
return m_BgColor;
}
}
public Color TextColor
{
set
{
m_TextColor = value;
}
get
{
return m_TextColor;
}
}
public Color BorderColor
{
set
{
m_BorderColor = value;
}
get
{
return m_BorderColor;
}
}
public Color[] PieColor
{
set
{
m_PieColor = value;
}
get
{
return m_PieColor;
}
}
private Color GetColor(int i) // Pie
{
try
{
if( 0 < i && i < PieColor.Length )
{
return PieColor[i];
}
else if( i >= PieColor.Length )
{
int j = i % PieColor.Length;
return PieColor[j];
}
else
{
return Color.Green;
}
}
catch
{
return Color.Green;
}
}
// bmp
public Bitmap CreateImage()
{
InitializeGraph();
DrawRight(ref objGraphics);
DrawContent(ref objGraphics);
return objBitmap;
}
// , ,
private void InitializeGraph()
{
//
objBitmap = new Bitmap(Width,Height);
// objBitmap objGraphics ( objBitmap )
objGraphics = Graphics.FromImage(objBitmap);
// (LightGray) ( )
objGraphics.DrawRectangle(new Pen(BorderColor,1),0,0,Width,Height);
objGraphics.FillRectangle(new SolidBrush(BgColor),1,1,Width-2,Height-2);
//
CreateTitle(ref objGraphics);
}
//
private void DrawRight(ref Graphics objGraphics)
{
objGraphics.DrawString(String.Format(" :{0}",Unit),new Font(" ",10),new SolidBrush(TextColor),Width-95,30);
Point KeysPoint = new Point(Width-120,50);
Point KeysTextPoint = new Point(Width-90,50);
for(int i=0;i < Keys.Length;i++)
{
objGraphics.DrawRectangle(new Pen(BorderColor,1),KeysPoint.X,KeysPoint.Y,21,11);
objGraphics.FillRectangle(new SolidBrush(GetColor(i)),KeysPoint.X,KeysPoint.Y,20,10);
objGraphics.DrawString(String.Format("{0} {1}",Keys[i],Values[i]),new Font(" ",10),new SolidBrush(TextColor),KeysTextPoint);
KeysPoint.Y +=15;
KeysTextPoint.Y += 15;
}
}
private void DrawContent(ref Graphics objGraphics)
{
float Sum = 0;
float StartAngle = 0;
float SweepAngle = 0;
float CircleHeight = 0;
float CircleX = 0;
float CircleY = 0;
for(int i=0;i<Values.Length;i++)
{
Sum += Values[i];
}
if(Width > Height)
{
CircleHeight = Height - 100;
}
else
{
CircleHeight = Width - 150;
}
CircleX = (Width - 150)/2 - CircleHeight/2;
CircleY = Height - 50 - CircleHeight;
for(int i=0;i<Values.Length;i++)
{
SweepAngle = (Values[i]/Sum)*360;
objGraphics.DrawPie (new Pen(BorderColor,1),CircleX,CircleY,CircleHeight,CircleHeight,StartAngle,SweepAngle);
objGraphics.FillPie (new SolidBrush(GetColor(i)),CircleX,CircleY,CircleHeight,CircleHeight,StartAngle,SweepAngle);
StartAngle += SweepAngle;
}
}
//
private void CreateTitle(ref Graphics objGraphics)
{
objGraphics.DrawString(Title,new Font(" ",16),new SolidBrush(TextColor),new Point(5,5));
}
}
}
呼び出し:
Pie pie = new Pie(); //
DataTable dt = ssih.GetList("1=1 order by dataDate desc");
string[] kzKeys = {" "," "," "};
pie.Keys = kzKeys;
float[] kzValues = {float.Parse(Convert.ToString(Convert.ToDouble(dt.Rows[0]["zxyc1"].ToString())*100)),float.Parse(Convert.ToString(Convert.ToDouble(dt.Rows[0]["zxyc2"].ToString())*100)),float.Parse(Convert.ToString(Convert.ToDouble(dt.Rows[0]["zxyc3"].ToString())*100))};
pie.Values = kzValues;
Bitmap bmp = pie.CreateImage();
bmp.Save(@"D:\work\cnqsq\Pie.jpg",ImageFormat.Jpeg);
//D:\work\cnqsq\Pie.jpg asp_net