昨天需要做一个项目中的投票统计去网上查找了一些代码,原代码用的数据源是xml,我把它修改成了一个List数组,现在贴出来希望对需要的朋友有些用
首先你需要一个能够生成图片的方法能够按照你的需求你给的数据源产生图片
/// <summary>
/// Draw饼图
/// </summary>
/// <param name="list">数据源</param>
/// <param name="iSelect">点击了那块</param>
/// <param name="ImageTitle">图片标题</param>
private void DrawPic(List<Object> list,int iSelect,string ImageTitle)
{
float[] angle = new float[list.Count];
string[] sTxt = new string[list.Count];
float AllCount = 0;
for (int i = 0; i < list.Count; i++)
{
AllCount += Convert.ToSingle(((object[])(list[i]))[1]);
}
for (int i = 0; i < list.Count; i++)
{
object[] o = (object[])list[i];
angle[i] = Convert.ToSingle(o[1]) * 360.0f / AllCount;
sTxt[i] = o[0].ToString();
}
float x = 120.0f, y = 80.0f, d = 200.0f, offset = 15.0f, x1 = 0.0f, y1 = 0.0f;//圆形x,y坐标,半径,偏移距离,x,y方向的偏移值
float curangle = 0.0f;//当前已转的角度
int ox = 0, oy = 0;//圆心坐标
int px1 = 0, py1 = 0, px2 = 0, py2 = 0;//直线的端点
System.Drawing.Image bitmap = new Bitmap(675, 360, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//20项颜色。
Color[] color = new Color[]{Color.Black,Color.Blue,Color.Coral,Color.DarkGoldenrod,Color.Firebrick,Color.Gold,Color.Honeydew,Color.Indigo,Color.Khaki,Color.LemonChiffon,Color.Maroon,Color.Navy,
Color.Orange,Color.PapayaWhip,Color.Red,Color.Salmon,Color.Thistle,Color.Violet,Color.Wheat,Color.YellowGreen};
g.Clear(Color.FromArgb(255, 255, 255));//清屏
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
g.DrawString(DrawTitle, new Font(FontFamily.GenericSansSerif, 12), new SolidBrush(Color.Black), new RectangleF(0.0f, 10.0f, 675.0f, 20.0f), sf);
//画右侧的大矩形
g.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), 482, 122, 80, (angle.Length + 1) * 12);//阴影
g.FillRectangle(new SolidBrush(Color.FromArgb(253, 245, 230)), 480, 120, 80, (angle.Length + 1) * 12);//前景
g.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128), 1), 480, 120, 80, (angle.Length + 1) * 12);//轮廓
/**/
/////////////////////////////////////////////
for (int i = 0; i < angle.Length; i++)
{
sf.Alignment = StringAlignment.Near;
if (i == iSelect - 1)
{
x1 = Convert.ToSingle(offset * Math.Cos((curangle + angle[i] / 2) * Math.PI / 180.0f));
y1 = Convert.ToSingle(offset * Math.Sin((curangle + angle[i] / 2) * Math.PI / 180.0f));
ox = Convert.ToInt32(x + d / 2 + x1);
oy = Convert.ToInt32(y + d / 2 + y1);
px1 = Convert.ToInt32((d / 2 + 20) * Math.Cos((curangle + angle[i] / 2) * Math.PI / 180.0f)) + ox;
py2 = py1 = Convert.ToInt32((d / 2 + 20) * Math.Sin((curangle + angle[i] / 2) * Math.PI / 180.0f)) + oy;
if ((curangle + angle[i] / 2) >= 270 || (curangle + angle[i] / 2) < 90) px2 = px1 + 20;
else px2 = px1 - 20;
g.DrawLine(new Pen(Color.FromArgb(120, 120, 120), 1), new Point(ox, oy), new Point(px1, py1));//画直线-标释用。
g.DrawLine(new Pen(Color.FromArgb(120, 120, 120), 1), new Point(px1, py1), new Point(px2, py2));//直线
if ((curangle + angle[i] / 2) >= 270 || (curangle + angle[i] / 2) < 90) //写文字
{
g.DrawString(sTxt[i], new Font(FontFamily.GenericSansSerif, 8), new SolidBrush(Color.FromArgb(120, 120, 120)), px2, py2 - 4, sf);
}
else
{
g.DrawString(sTxt[i], new Font(FontFamily.GenericSansSerif, 8), new SolidBrush(Color.FromArgb(120, 120, 120)), px2 - sTxt[i].Length * 12 - 2, py2 - 4, sf);
}
g.FillPie(new SolidBrush(Color.FromArgb(128, 128, 128)), x + x1 + 2, y + y1 + 2, d, d, curangle, angle[i]);//画饼图的阴影
g.FillPie(new SolidBrush(color[i]), x + x1, y + y1, d, d, curangle, angle[i]); //画饼图
g.DrawPie(new Pen(Color.FromArgb(128, 128, 128), 1), x + x1, y + y1, d, d, curangle, angle[i]); //画轮廓
}
else
{
ox = Convert.ToInt32(x + d / 2);
oy = Convert.ToInt32(y + d / 2);
px1 = Convert.ToInt32((d / 2 + 20) * Math.Cos((curangle + angle[i] / 2) * Math.PI / 180.0f)) + ox;
py2 = py1 = Convert.ToInt32((d / 2 + 20) * Math.Sin((curangle + angle[i] / 2) * Math.PI / 180.0f)) + oy;
if ((curangle + angle[i] / 2) >= 270 || (curangle + angle[i] / 2) < 90) px2 = px1 + 20;
else px2 = px1 - 20;
g.DrawLine(new Pen(Color.FromArgb(120, 120, 120), 1), new Point(ox, oy), new Point(px1, py1));
g.DrawLine(new Pen(Color.FromArgb(120, 120, 120), 1), new Point(px1, py1), new Point(px2, py2));
if ((curangle + angle[i] / 2) >= 270 || (curangle + angle[i] / 2) < 90)
{
g.DrawString(sTxt[i], new Font(FontFamily.GenericSansSerif, 8), new SolidBrush(Color.FromArgb(120, 120, 120)), px2, py2 - 4, sf);
}
else
{
g.DrawString(sTxt[i], new Font(FontFamily.GenericSansSerif, 8), new SolidBrush(Color.FromArgb(120, 120, 120)), px2 - sTxt[i].Length * 12 - 2, py2 - 4, sf);
}
g.FillPie(new SolidBrush(Color.FromArgb(128, 128, 128)), x + 2, y + 2, d, d, curangle, angle[i]);
g.FillPie(new SolidBrush(color[i]), x, y, d, d, curangle, angle[i]);
g.DrawPie(new Pen(Color.FromArgb(128, 128, 128), 1), x, y, d, d, curangle, angle[i]);
}
curangle += angle[i];
//画右侧的小矩形,与文字
g.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), 492, i * 12 + 6 + 120 + 2, 20, 8);//阴影
g.FillRectangle(new SolidBrush(color[i]), 490, i * 12 + 6 + 120, 20, 8);//前景
g.DrawRectangle(new Pen(Color.FromArgb(120, 120, 120), 1), 490, i * 12 + 6 + 120, 20, 8);//轮廓
g.DrawString(sTxt[i], new Font(FontFamily.GenericSansSerif, 7), new SolidBrush(Color.FromArgb(80, 80, 80)), 515, i * 12 + 6 + 120, sf);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}这个方法当你给出它所要的参数之后就可以生产出png格式的饼图图片。然后你所要做的是构造他所要的数据源
//产生数据
public List<object> GetDrawData(string drawWhat)
{
int type = 1;
switch (drawWhat)
{
case "traffic":
type = 1;
DrawTitle = "交通";
break;
case "peripheral":
type = 2;
DrawTitle = "周边环境";
break;
case "price":
type = 3;
DrawTitle = "门票价格";
break;
case "services":
type = 4;
DrawTitle = "服务质量";
break;
default:
break;
}
BLL.TourVillageVote bll = new CnLife.Tour.BLL.TourVillageVote();
string strWhere = "VillageID=1 and type=" + type;
Model.TourVillageVote v = bll.GetList("*", strWhere, "VillageID").ToArray()[0];
Model.TourVillageVote vote = new CnLife.Tour.Model.TourVillageVote();
List<object> list = new List<object>();
int[] inter = new int[] { v.Value1, v.Value2, v.Value3, v.Value4, v.Value5, v.Value6 };
string[] str = new string[] { "差", "一般", "还行", "好", "很好", "非常好" };
for (int j = 0; j < 6; j++)
{
object[] st = new object[] { str[j], inter[j] };
list.Add(st);
}
return list;
}经过这些之后就可以产生所要的东西了,但是还有些不完整,剩下的放到下篇文章中描述