zedgraph控件 饼图中显示每个饼块的百分比的标签出现遮挡

woto123 2011-12-13 03:39:33
饼块遮挡该怎么解决呢?
...全文
306 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kimmking 2011-12-13
  • 打赏
  • 举报
回复
应该不能,试试放大你的图。
woto123 2011-12-13
  • 打赏
  • 举报
回复
程序很好,但是我的问题还是没有得到解决,就是当饼图中的小块很多时,所显示的标签依然会重叠在一起,能不能有一种方法使得这些标签可以自动错开?
csdn_aspnet 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;
}



ZedGraph饼图、条形图和饼图Demo源码 ZedGraphV515是C#编写的.NET类库,提供了用户控件和web控件。它可以创建2D的线性图、条形图和饼图。 它功能完整且有详细的功能自定义。 基于LGPL协议开源,.NET 2.0 C#源代码)它的思路清淅,所以非常容易就上手. 几个注意点: 图片的保存路径设置:RenderedImagePath属性设置,程序对该文件夹应该是有写和修改权限的 图片的输出格式:OutputFormat属性设置,Png的推荐,比较清晰。 Chart ChartBorder 图表区域的边框设置 ChartFill 图表区域的背景填充 Legend 图表的注释标签显示设置项目,一组数据对应一种颜色的注释 IsHStack 当有多个显示项的时候设置Y轴数据是叠加的还是分开的 Xaxis 图表区域的X轴相关信息设置 AxisColor 坐标轴颜色 Cross 坐标的原点,可以设置坐标的偏移程度 CrossAuto 原点自动设置:True的话Cross的设置就无效了。 FontSpec X轴标题字体相关信息 Angle X轴标题字体显示时候的角度,0为水平 90为垂直 Fill X轴标题字体填充信息 ColorOpacity 透明度 IsScaled 设置X轴标题字体显示大小是否根据图的比例放大缩小 RangeMax 填充时候的最大倾斜度(有过渡色,没试过) RangeMin 填充时候的最小倾斜度(有过渡色,没试过) StringAlignment X轴标题字体排列(不清楚,没试过) IsOmitMag 是否显示指数幂(10次方,没试过,似乎与IsUseTenPower有关系) IsPreventLabelOverlap 坐标值显示是否允许重叠,如果False的话,控件会根据坐标值长度自动消除部分坐标值的显示状态 IsShowTitle X轴标题是否显示 IsTicsBetweenLabels 两个坐标值之间是否自动显示分隔标志 IsUseTenPower 是否使用10次幂指数 IsVisible 是否显示X轴

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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