zedgraph控件 饼图中显示每个饼块的百分比的标签出现遮挡
时间:2011-12-13
来源:互联网
饼块遮挡该怎么解决呢?
作者: woto123 发布时间: 2011-12-13
1、首先下载ZedGraph.dll和ZedGraph.Web.dll文件,引用到项目中,在选择项添加ZedGraphWeb控件。将ZedGraphWeb控件拖到页面
如果程序报错无法加载,检查页面是否有<%@ Register Assembly="ZedGraph.Web" Namespace="ZedGraph.Web" TagPrefix="cc1" %>注册代码,并将dll从项目删除,重新引用。多试几次
2、页面加载控件无误后:
protected void Page_Load(object sender, EventArgs e)
{
ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(ZedGraphWeb1_RenderGraph);//注册事件
}
//创建饼图
private void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
{
graphPane = pane[0]; // 在此之前定义全局变量GraphPane graphPane;
grap = g; // 在此之前定义全局变量System.Drawing.Graphics grap;
graphPane.Title.Text = “数据统计”; //给饼图设置title
DrawPie(type); //具体画图的方法
graphPane.AxisChange(g);
}
//对饼图的具体操作
private void DrawPie(int num)
{
DataTable dtyear = new DataTable(); //用于绑定ZedGraphWeb数据
double huikuan = new double();
int zonge = 0;
。。。。。。。。。。。。。。。。。 给dtyear 赋值省略
graphPane.Title.Text = titlehehe;
graphPane.CurveList.Clear();
graphPane.GraphObjList.Clear();
// Set the GraphPane title
graphPane.Title.FontSpec.IsItalic = true;
graphPane.Title.FontSpec.Size = 24f;
graphPane.Title.FontSpec.Family = "Times New Roman";
// Fill the pane background with a color gradient
graphPane.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f);
// No fill for the chart background
graphPane.Chart.Fill.Type = FillType.None;
// Set the legend to an arbitrary location
graphPane.Legend.Position = LegendPos.Float;
graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction,
AlignH.Right, AlignV.Top); //设置饼图显示位置
graphPane.Legend.FontSpec.Size = 10f; //设置字体大小
graphPane.Legend.IsHStack = false;
TextObj text = new TextObj("", 0.18F, 0.40F, CoordType.ChartFraction);
graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f);
graphPane.Legend.Position = LegendPos.Float;
graphPane.Legend.Location = new Location(0.99f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
graphPane.Legend.FontSpec.Family = "宋体";
graphPane.Legend.FontSpec.Size = 10;
graphPane.Legend.IsHStack = false;
具体按数据画图
double y = 0.00;
string name1 = "";
for (int i = 0; i < dtyear.Rows.Count; i++)
{
y = Convert.ToDouble(string.IsNullOrEmpty(dtyear.Rows[i]["金额"].ToString()) == true ? "0" : dtyear.Rows[i]["金额"].ToString());
name1 = dtyear.Rows[i]["日期"].ToString() + "," + dtyear.Rows[i]["金额"].ToString();
graphPane.AddPieSlice(y, GetRandomColor(), Color.White, 45f, 0, name1);
}
dtyear.Rows.Add(new object[] { "总额", huikuan.ToString() });
GridView1.DataSource = dtyear; //如果要求下面显示列表数据时,给GridView1绑定数据
GridView1.DataBind();
dtyear = null;
CurveList curves = graphPane.CurveList;
double total = 0;
for (int x = 0; x < curves.Count; x++)
total += ((PieItem)curves[x]).Value;
// Make a text label to highlight the total value
text.Text = "总额" + huikuan.ToString();
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
text.FontSpec.StringAlignment = StringAlignment.Center;
graphPane.GraphObjList.Add(text);
// Create a drop shadow for the total value text item
TextObj text2 = new TextObj(text);
text2.FontSpec.Fill = new Fill(Color.Black);
text2.Location.X += 0.008f;
text2.Location.Y += 0.01f;
graphPane.GraphObjList.Add(text2);
graphPane.AxisChange(grap);
}
3、创建随机颜色,设置饼图大小
// 创建随机颜色
private Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(RandomNum_First.Next(150));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(RandomNum_Sencond.Next(150));
Random RandomNum_3 = new Random((int)DateTime.Now.Ticks);
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = RandomNum_3.Next(256);
// 为了在白色背景上显示,尽量生成深色
//int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
//int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
//设置大小及字体
private void SetSize()
{
//保留一个小的页面空白在控件的周围
ZedGraphWeb1.Width = 1000;
ZedGraphWeb1.Height = 500;
ZedGraphWeb1.FontSpec.Family = "宋体";
ZedGraphWeb1.FontSpec.Size = 12;
}
如果程序报错无法加载,检查页面是否有<%@ Register Assembly="ZedGraph.Web" Namespace="ZedGraph.Web" TagPrefix="cc1" %>注册代码,并将dll从项目删除,重新引用。多试几次
2、页面加载控件无误后:
protected void Page_Load(object sender, EventArgs e)
{
ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(ZedGraphWeb1_RenderGraph);//注册事件
}
//创建饼图
private void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
{
graphPane = pane[0]; // 在此之前定义全局变量GraphPane graphPane;
grap = g; // 在此之前定义全局变量System.Drawing.Graphics grap;
graphPane.Title.Text = “数据统计”; //给饼图设置title
DrawPie(type); //具体画图的方法
graphPane.AxisChange(g);
}
//对饼图的具体操作
private void DrawPie(int num)
{
DataTable dtyear = new DataTable(); //用于绑定ZedGraphWeb数据
double huikuan = new double();
int zonge = 0;
。。。。。。。。。。。。。。。。。 给dtyear 赋值省略
graphPane.Title.Text = titlehehe;
graphPane.CurveList.Clear();
graphPane.GraphObjList.Clear();
// Set the GraphPane title
graphPane.Title.FontSpec.IsItalic = true;
graphPane.Title.FontSpec.Size = 24f;
graphPane.Title.FontSpec.Family = "Times New Roman";
// Fill the pane background with a color gradient
graphPane.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f);
// No fill for the chart background
graphPane.Chart.Fill.Type = FillType.None;
// Set the legend to an arbitrary location
graphPane.Legend.Position = LegendPos.Float;
graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction,
AlignH.Right, AlignV.Top); //设置饼图显示位置
graphPane.Legend.FontSpec.Size = 10f; //设置字体大小
graphPane.Legend.IsHStack = false;
TextObj text = new TextObj("", 0.18F, 0.40F, CoordType.ChartFraction);
graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f);
graphPane.Legend.Position = LegendPos.Float;
graphPane.Legend.Location = new Location(0.99f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
graphPane.Legend.FontSpec.Family = "宋体";
graphPane.Legend.FontSpec.Size = 10;
graphPane.Legend.IsHStack = false;
具体按数据画图
double y = 0.00;
string name1 = "";
for (int i = 0; i < dtyear.Rows.Count; i++)
{
y = Convert.ToDouble(string.IsNullOrEmpty(dtyear.Rows[i]["金额"].ToString()) == true ? "0" : dtyear.Rows[i]["金额"].ToString());
name1 = dtyear.Rows[i]["日期"].ToString() + "," + dtyear.Rows[i]["金额"].ToString();
graphPane.AddPieSlice(y, GetRandomColor(), Color.White, 45f, 0, name1);
}
dtyear.Rows.Add(new object[] { "总额", huikuan.ToString() });
GridView1.DataSource = dtyear; //如果要求下面显示列表数据时,给GridView1绑定数据
GridView1.DataBind();
dtyear = null;
CurveList curves = graphPane.CurveList;
double total = 0;
for (int x = 0; x < curves.Count; x++)
total += ((PieItem)curves[x]).Value;
// Make a text label to highlight the total value
text.Text = "总额" + huikuan.ToString();
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
text.FontSpec.StringAlignment = StringAlignment.Center;
graphPane.GraphObjList.Add(text);
// Create a drop shadow for the total value text item
TextObj text2 = new TextObj(text);
text2.FontSpec.Fill = new Fill(Color.Black);
text2.Location.X += 0.008f;
text2.Location.Y += 0.01f;
graphPane.GraphObjList.Add(text2);
graphPane.AxisChange(grap);
}
3、创建随机颜色,设置饼图大小
// 创建随机颜色
private Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(RandomNum_First.Next(150));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(RandomNum_Sencond.Next(150));
Random RandomNum_3 = new Random((int)DateTime.Now.Ticks);
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = RandomNum_3.Next(256);
// 为了在白色背景上显示,尽量生成深色
//int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
//int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
//设置大小及字体
private void SetSize()
{
//保留一个小的页面空白在控件的周围
ZedGraphWeb1.Width = 1000;
ZedGraphWeb1.Height = 500;
ZedGraphWeb1.FontSpec.Family = "宋体";
ZedGraphWeb1.FontSpec.Size = 12;
}
作者: hefeng_aspnet 发布时间: 2011-12-13
程序很好,但是我的问题还是没有得到解决,就是当饼图中的小块很多时,所显示的标签依然会重叠在一起,能不能有一种方法使得这些标签可以自动错开?
作者: woto123 发布时间: 2011-12-13
应该不能,试试放大你的图。
作者: KimmKing 发布时间: 2011-12-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28