asp.net怎么绘圆饼图

w161134025 2008-11-01 10:39:55
asp.net怎么绘圆饼图,简单点的,我还不会画
...全文
240 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
CloneCenter 2008-11-01
  • 打赏
  • 举报
回复
可以用 Office Web Component 组件试试看,可以直接支持很多类型的图表。
mzx87 2008-11-01
  • 打赏
  • 举报
回复
private void CreateImage()
{
..........................

//获取男员工数目
int man = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());

//获取女员工数目
int woman = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());

int Sum = man +woman ;

//创建画图对象
int width=400,height=450;
Bitmap bitmap = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bitmap);

try
{
//清空背景色
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);
Brush brush1 = new SolidBrush(Color.YellowGreen);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Brown);
Font font1 = new Font("Courier New",16,FontStyle.Bold);
Font font2 = new Font("Courier New", 8);

g.FillRectangle(brush1, 0, 0, width, height); //绘制背景图

g.DrawString("公司男女比例饼形图", font1, brush2, new Point(80, 20)); //书写标题

int piex = 100, piey = 100, piew = 150, pieh = 150;
//男员工在圆中分配的角度
float angle1 = Convert.ToSingle((360 / Convert.ToSingle(Sum)) * Convert.ToSingle(man));
//女员工在圆中分配的角度
float angle2 = Convert.ToSingle((360 / Convert.ToSingle(Sum)) * Convert.ToSingle(woman));

g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1); //绘制男员工所占比例
g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2); //绘制女员工所占比例

//绘制标识
g.DrawRectangle(pen1, 50, 300, 300, 100); //绘制范围框
g.FillRectangle(brush2, 90, 320, 20, 10); //绘制小矩形
g.DrawString("男员工占公司总人数比例:"+ Convert.ToSingle(man)*100 / Convert.ToSingle(Sum) + "%", font2, brush2, 120, 320);
g.FillRectangle(brush3, 90, 360, 20, 10);
g.DrawString("女员工占公司总人数比例:" + Convert.ToSingle(woman) *100/ Convert.ToSingle(Sum) + "%", font2, brush3, 120, 360);
}
catch(Exception md)
{
Response.Write(md.Message);
}

System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
evjen 2008-11-01
  • 打赏
  • 举报
回复
 /// <summary>
// 绘制柱形图方法
/// </summary>

public static string BarChar(string title, string subTitle, int width, int height, BarChartValue[] Mydata)
{
const int SIDE_LENGTH = 400;
const int CHART_TOP = 75;
const int CHART_HEIGHT = 200;
const int CHART_LEFT = 50;
const int CHART_WIDTH = 300;
      float highPoint = 0;
for (int i = 0; i < Mydata.Length; i++)
{
if (highPoint < Convert.ToSingle(Mydata[i].MValue))
{
highPoint = Convert.ToSingle(Mydata[i].MValue);
}
}

Bitmap bm = new Bitmap(width, height);

Graphics g = Graphics.FromImage(bm);
g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.White);
g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);

g.DrawString(title, new Font("Tahoma", 14), Brushes.Black, new PointF(5, 5));

g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));

float barWidth = CHART_WIDTH / (Mydata.Length * 2);
PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2), 0);
float barHeight = Mydata.Length;
for (int i = 0; i < Mydata.Length; i++)
{
barHeight = Convert.ToSingle(Mydata[i].MValue) * 200 / highPoint * 1;
barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), barOrigin.X, barOrigin.Y, barWidth + 5, barHeight);



g.DrawString(Mydata[i].Name, new Font("Tahoma",12), Brushes.Black, new PointF(barOrigin.X - 3, 277));



barOrigin.X = barOrigin.X + (barWidth * 2);
}

g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP-45), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT));
g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT), new Point(CHART_LEFT + CHART_WIDTH, CHART_TOP + CHART_HEIGHT));

g.DrawRectangle(new Pen(Color.Black, 1), 50,300, 299, 110);
g.DrawString("图表说明", new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(5, 290));

PointF boxOrigin = new PointF(60, 310);
PointF textOrigin = new PointF(80, 306);
for (int i = 0; i < Mydata.Length; i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 2, 1);
g.DrawString(Mydata[i].Name.ToString() + "月份" + " - " + Mydata[i].MValue.ToString("0.00"), new Font("Tahoma", 10), Brushes.Black, textOrigin);
if (i < 5)
{
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
else if (i == 5)
{
boxOrigin.X = 208;
boxOrigin.Y = 310;
textOrigin.X = 230;
textOrigin.Y = 306;

}
else
{
boxOrigin.Y += 15;
textOrigin.Y += 15;
}

}

g.Dispose();
System.Drawing.Image MyImage = (System.Drawing.Image)bm;
string TruePath = System.Web.HttpContext.Current.Server.MapPath("~/images");
string TrueName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond;
MyImage.Save(TruePath + "\\" + TrueName + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
return "~/images/" + TrueName + ".jpg";


}
}

  /// <summary>
///颜色设置
/// </summary>
public class ChartUtil
{
public ChartUtil()
{
}
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch (itemIndex)
{
case 0:
selectedColor =Color.YellowGreen;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.PaleVioletRed;
break;
case 4:
selectedColor = Color.Aqua;
break;
case 5:
selectedColor = Color.Aquamarine;
break;
case 6:
selectedColor = Color.Azure;
break;
case 7:
selectedColor = Color.SteelBlue;
break;
case 8:
selectedColor = Color.RoyalBlue;
break;
case 9:
selectedColor = Color.Blue;
break;
case 10:
selectedColor = Color.Coral;
break;
case 11:
selectedColor = Color.DarkCyan;
break;
case 12:
selectedColor = Color.Green;
break;
default:
selectedColor = Color.DarkViolet;
break;
}
return selectedColor;
}
}
/// <summary>
// 饼形图构造函数
/// </summary>
public class Pievalue
{
public Pievalue()
{

}
public Pievalue(string MyName, float Myvalue)
{
Name = MyName;
MValue = Myvalue;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

private float mvalue;
public float MValue
{
get
{
return mvalue;
}
set
{
mvalue = value;
}
}
}
/// <summary>
// 柱形图构造函数
/// </summary>
public class BarChartValue
{
public BarChartValue()
{

}
public BarChartValue(string MyName, float Myvalue)
{
Name = MyName;
MValue = Myvalue;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

private float mvalue;
public float MValue
{
get
{
return mvalue;
}
set
{
mvalue = value;
}
}
}




技术在于交流
magicbacon 2008-11-01
  • 打赏
  • 举报
回复
可以用控件画,免费的有FusionChartsFree,这个有flash动态效果,很漂亮,但可能需要自己用一个用户控件来把它封装起来才比较好用;还有就是dundas chart有一个评估版,也是免费的,只要到网站上注册就可以下载,用起来简单,但是在不绘图的时候会有一个评估版水印,但不影响使用~

lz也可以找找其他绘图控件,很多的~
evjen 2008-11-01
  • 打赏
  • 举报
回复
 /// <summary>
// 绘制柱形图方法
/// </summary>

public static string BarChar(string title, string subTitle, int width, int height, BarChartValue[] Mydata)
{
const int SIDE_LENGTH = 400;
const int CHART_TOP = 75;
const int CHART_HEIGHT = 200;
const int CHART_LEFT = 50;
const int CHART_WIDTH = 300;
      float highPoint = 0;
for (int i = 0; i < Mydata.Length; i++)
{
if (highPoint < Convert.ToSingle(Mydata[i].MValue))
{
highPoint = Convert.ToSingle(Mydata[i].MValue);
}
}

Bitmap bm = new Bitmap(width, height);

Graphics g = Graphics.FromImage(bm);
g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.White);
g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);

g.DrawString(title, new Font("Tahoma", 14), Brushes.Black, new PointF(5, 5));

g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));

float barWidth = CHART_WIDTH / (Mydata.Length * 2);
PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2), 0);
float barHeight = Mydata.Length;
for (int i = 0; i < Mydata.Length; i++)
{
barHeight = Convert.ToSingle(Mydata[i].MValue) * 200 / highPoint * 1;
barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), barOrigin.X, barOrigin.Y, barWidth + 5, barHeight);



g.DrawString(Mydata[i].Name, new Font("Tahoma",12), Brushes.Black, new PointF(barOrigin.X - 3, 277));



barOrigin.X = barOrigin.X + (barWidth * 2);
}

g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP-45), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT));
g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT), new Point(CHART_LEFT + CHART_WIDTH, CHART_TOP + CHART_HEIGHT));

g.DrawRectangle(new Pen(Color.Black, 1), 50,300, 299, 110);
g.DrawString("图表说明", new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(5, 290));

PointF boxOrigin = new PointF(60, 310);
PointF textOrigin = new PointF(80, 306);
for (int i = 0; i < Mydata.Length; i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 2, 1);
g.DrawString(Mydata[i].Name.ToString() + "月份" + " - " + Mydata[i].MValue.ToString("0.00"), new Font("Tahoma", 10), Brushes.Black, textOrigin);
if (i < 5)
{
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
else if (i == 5)
{
boxOrigin.X = 208;
boxOrigin.Y = 310;
textOrigin.X = 230;
textOrigin.Y = 306;

}
else
{
boxOrigin.Y += 15;
textOrigin.Y += 15;
}

}

g.Dispose();
System.Drawing.Image MyImage = (System.Drawing.Image)bm;
string TruePath = System.Web.HttpContext.Current.Server.MapPath("~/images");
string TrueName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond;
MyImage.Save(TruePath + "\\" + TrueName + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
return "~/images/" + TrueName + ".jpg";


}
}

  /// <summary>
///颜色设置
/// </summary>
public class ChartUtil
{
public ChartUtil()
{
}
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch (itemIndex)
{
case 0:
selectedColor =Color.YellowGreen;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.PaleVioletRed;
break;
case 4:
selectedColor = Color.Aqua;
break;
case 5:
selectedColor = Color.Aquamarine;
break;
case 6:
selectedColor = Color.Azure;
break;
case 7:
selectedColor = Color.SteelBlue;
break;
case 8:
selectedColor = Color.RoyalBlue;
break;
case 9:
selectedColor = Color.Blue;
break;
case 10:
selectedColor = Color.Coral;
break;
case 11:
selectedColor = Color.DarkCyan;
break;
case 12:
selectedColor = Color.Green;
break;
default:
selectedColor = Color.DarkViolet;
break;
}
return selectedColor;
}
}
/// <summary>
// 饼形图构造函数
/// </summary>
public class Pievalue
{
public Pievalue()
{

}
public Pievalue(string MyName, float Myvalue)
{
Name = MyName;
MValue = Myvalue;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

private float mvalue;
public float MValue
{
get
{
return mvalue;
}
set
{
mvalue = value;
}
}
}
/// <summary>
// 柱形图构造函数
/// </summary>
public class BarChartValue
{
public BarChartValue()
{

}
public BarChartValue(string MyName, float Myvalue)
{
Name = MyName;
MValue = Myvalue;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

private float mvalue;
public float MValue
{
get
{
return mvalue;
}
set
{
mvalue = value;
}
}
}




技术在于交流
evjen 2008-11-01
  • 打赏
  • 举报
回复
我这里只有柱状图 饼图 楼主看看 也许对你又帮助的哦

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
namespace AspNet.GDI
{
/// <summary>
// GDI绘图类
/// </summary>


public class GDIpictrue
{

public GDIpictrue()
{

}
/// <summary>
// 绘制饼形图方法
/// </summary>

public static string pievalue(string title, string subTitle, int width, int height, Pievalue[] Mydata)
{
const int SIDE_LENGTH = 400;
const int PIE_DIAMETER = 200;
float sumData = 0;
for (int i = 0; i < Mydata.Length; i++)
{
sumData += Convert.ToSingle(Mydata[i].MValue);
}

Bitmap bm = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bm);

g.Clear(Color.White);
g.DrawRectangle(Pens.Black,0,0, SIDE_LENGTH -50, SIDE_LENGTH -1);

g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(5, 5));

g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));

float curAngle = 0;
float totalAngle = 0;
for (int j = 0; j < Mydata.Length; j++)
{
curAngle = Convert.ToSingle(Mydata[j].MValue) / sumData * 360;

g.FillPie(new SolidBrush(ChartUtil.GetChartItemColor(j)), 100, 55, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
g.DrawPie(Pens.Black, 100, 55, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
totalAngle += curAngle;
}

g.DrawRectangle(Pens.Black, 350, 90, 399, 299);
g.DrawString("图表说明", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(350,70));

PointF boxOrigin = new PointF(360,100);
PointF textOrigin = new PointF(385, 96);
float percent = 0;
for (int k = 0; k < Mydata.Length; k++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(k)), boxOrigin.X, boxOrigin.Y, 20, 10);
g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
percent = Convert.ToSingle(Mydata[k].MValue) / sumData * 100;
g.DrawString(Mydata[k].MValue.ToString("0.00") + " - " + Mydata[k].Name.ToString() + " (" + percent.ToString("0.00") + "%)", new Font("Tahoma", 12), Brushes.Black, textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}

g.Dispose();

System.Drawing.Image MyImage = (System.Drawing.Image)bm;
string TruePath = System.Web.HttpContext.Current.Server.MapPath("~/images");
string TrueName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond;
MyImage.Save(TruePath + "\\" + TrueName + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
return "~/images/" + TrueName + ".jpg";
}
}
wuyq11 2008-11-01
  • 打赏
  • 举报
回复
http://www.cnblogs.com/andy6888/archive/2006/09/04/494084.aspx

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧