c#画饼图文档格式.docx
《c#画饼图文档格式.docx》由会员分享,可在线阅读,更多相关《c#画饼图文档格式.docx(16页珍藏版)》请在冰豆网上搜索。
graphics.SmoothingMode=SmoothingMode.HighQuality;
//高质量的文字
graphics.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
//像素均偏移0.5个单位,以消除锯齿
graphics.PixelOffsetMode=PixelOffsetMode.Half;
//第一个色块的原点位置
PointFbasePoint=newPointF(10,20);
//色块的大小
SizeFtheSize=newSizeF(45,16);
//第一个色块的说明文字的位置
PointFtextPoint=newPointF(basePoint.X+50,basePoint.Y);
foreach(varitemindata)
{
RectangleFbaseRectangle=newRectangleF(basePoint,theSize);
//画代表色块
graphics.FillRectangle(newSolidBrush(getColor(item.Key.ToString())),baseRectangle);
graphics.DrawString(item.Key.ToString(),newFont(familyName,11),Brushes.Black,textPoint);
basePoint.Y+=30;
textPoint.Y+=30;
}
//扇形区所在边框的原点位置
PointcirclePoint=newPoint(Convert.ToInt32(textPoint.X+90),35);
//总比初始值
floattotalRate=0;
//起始角度Y周正方向
floatstartAngle=30;
//当前比初始值
floatcurrentRate=0;
//圆所在边框的大小
SizecicleSize=newSize(r*2,r*2);
//圆所在边框的位置
RectanglecircleRectangle=newRectangle(circlePoint,cicleSize);
foreach(varitemindata)totalRate+=float.Parse(item.Value.ToString());
foreach(varitemindata)
currentRate=float.Parse(item.Value.ToString())/totalRate*360;
graphics.DrawPie(Pens.White,circleRectangle,startAngle,currentRate);
graphics.FillPie(newSolidBrush(getColor(item.Key.ToString())),circleRectangle,startAngle,currentRate);
//至此扇形图已经画完,下面是在扇形图上写上说明文字
//当前圆的圆心相对图片边框原点的坐标
PointFcPoint=newPointF(circlePoint.X+r,circlePoint.Y+r);
//当前圆弧上的点
//cos(弧度)=X轴坐标/r
//弧度=角度*π/180
doublerelativeCurrentX=r*Math.Cos((360-startAngle-currentRate/2)*Math.PI/180);
doublerelativecurrentY=r*Math.Sin((360-startAngle-currentRate/2)*Math.PI/180);
doublecurrentX=relativeCurrentX+cPoint.X;
doublecurrentY=cPoint.Y-relativecurrentY;
//内圆上弧上的浮点型坐标
PointFcurrentPoint=newPointF(float.Parse(currentX.ToString()),float.Parse(currentY.ToString()));
//外圆弧上的点
doublelargerR=r+25;
doublerelativeLargerX=largerR*Math.Cos((360-startAngle-currentRate/2)*Math.PI/180);
doublerelativeLargerY=largerR*Math.Sin((360-startAngle-currentRate/2)*Math.PI/180);
doublelargerX=relativeLargerX+cPoint.X;
doublelargerY=cPoint.Y-relativeLargerY;
//外圆上弧上的浮点型坐标
PointFlargerPoint=newPointF(float.Parse(largerX.ToString()),float.Parse(largerY.ToString()));
//将两个点连起来
//graphics.DrawLine(Pens.Black,currentPoint,largerPoint);
//外圆上说明文字的位置
PointFcircleTextPoint=newPointF(float.Parse(largerX.ToString()),float.Parse(largerY.ToString()));
//在外圆上的点的附近合适的位置写上说明
if(largerX>
=0&
&
largerY>
=0)//第1象限实际第二象限
//circleTextPoint.Y-=15;
circleTextPoint.X-=35;
}
if(largerX<
=0)//第2象限实际第三象限
//circleTextPoint.Y-=15;
//circleTextPoint.X-=65;
}
largerY<
=0)//第3象限实际第四象限
//circleTextPoint.X-=45;
circleTextPoint.Y+=30;
}
if(largerX>
=0)//第4象限实际第一象限
circleTextPoint.X-=15;
//circleTextPoint.Y+=5;
}
//象限差异解释:
在数学中二维坐标轴中右上方全为正,在计算机处理图像时,右下方全为正。
相当于顺时针移了一个象限序号
graphics.DrawString(item.Key.ToString()+"
"
+(currentRate/360).ToString("
p2"
),newFont(familyName,11),Brushes.Black,circleTextPoint);
startAngle+=currentRate;
returnbitmap;
2、获得配色的代码:
ColorgetColor(stringscoreLevel){
Colorc=Color.White;
if(scoreLevel.Contains("
优秀"
))
c=Color.FromArgb(57,134,155);
良好"
c=Color.FromArgb(70,161,185);
if(scoreLevel.Contains("
一般"
c=Color.FromArgb(124,187,207);
不及格"
c=Color.FromArgb(181,212,224);
returnc;
3、画条形图的代码
///<
///获得柱状图
PaperScore"
publicBitmapGetBargraph(intwidth,intheigh,stringfamilyName,Dictionary<
data,intPaperScore)
{if(data!
=null)
{
Bitmapbitmap=newBitmap(width,heigh);
Graphicsgraphics=Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode=SmoothingMode.HighQuality;
graphics.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
graphics.PixelOffsetMode=PixelOffsetMode.Half;
doublemaxCount=0;
//以最多人数为基准
foreach(varitemindata)if(Convert.ToDouble(item.Value.ToString())>
maxCount)
maxCount=Convert.ToDouble(item.Value.ToString());
//在距离底部25像素的地方画上分数线并标上四个分数
//该分数线的宽度是图片宽度的85%
//分数线原点位置
PointFscoreLineStartPoint=newPointF(width*0.15f/2f,heigh-25f);
//分数线终点位置
PointFscoreLineEndPoint=newPointF(scoreLineStartPoint.X+width*0.85f,scoreLineStartPoint.Y);
graphics.DrawLine(Pens.Black,scoreLineStartPoint,scoreLineEndPoint);
//定义:
最多人数的那项占图片高度的85%
//每项*高度的85%/maxCount=该项的高度
floatcurrentX=width*0.85f*(1f/9)//直线上的1/9处为起点
+width*0.15f/2//图片的两边空15%,每边空15%的一半;
floatperWidth=width*0.85f*(1f/9);
foreach(varitemindata)
//当前等级的原点描述:
Height-25-(人数*0.9/maxcount)
floatcurrentHeight=(float)item.Value*0.85f*heigh/(float)maxCount;
//颜色全部用淡蓝色
graphics.FillRectangle(newSolidBrush(Color.FromArgb(70,161,187)),currentX,heigh-25-currentHeight-1,perWidth,currentHeight);
//画当前区间的左边的线
graphics.DrawLine(Pens.Black,currentX,scoreLineStartPoint.Y,currentX,scoreLineStartPoint.Y+3);
//在上面5像素处写上人数
graphics.DrawString(item.Value.ToString()+"
(人)"
newFont(familyName,11),Brushes.Black,currentX-4,heigh-25-currentHeight-1-18);
graphics.DrawString(item.Key.ToString(),newFont(familyName,11),Brushes.Black,currentX,scoreLineStartPoint.Y+3);
currentX+=perWidth;
//向右移一个柱形宽度单位
//画当前区间的右边的线
//向右移一个柱形宽度单位}
graphics.DrawString("
(等级)"
newFont(familyName,11),Brushes.Black,currentX-perWidth+3f,scoreLineStartPoint.Y+3);
}elsereturnnull;
4、调用柱状图的一般处理程序(饼图类似)
stringdatas=context.Request.QueryString["
];
ScoreStatisticss=newScoreStatistics();
data=s.TransferToObject<
Dictionary<
(datas);
MemoryStreammem=newMemoryStream();
Bitmapchart=scoreStaticsBLL.GetBargraph(450,280,"
宋体"
data,120);
chart.Save(mem,ImageFormat.Jpeg);
context.Response.ContentType="
image/jpeg"
;
context.Response.BinaryWrite(mem.ToArray());
调用图片的页面代码就是一个图片控件,src的地址为上面的一般处理程序的相对网站地址
方法二
//饼图dt数据结构为columndata(数据)columnname(文本)这两列
privatevoidViewChart(DataTable_dt,string_title)
{
this.ct_coll.Series.Clear();
this.ct_coll.Legends.Clear();
this.ct_coll.Series.Add(newSeries("
Data"
));
this.ct_coll.Legends.Add(newLegend("
Stores"
//右边标签列
this.ct_coll.Series["
].ChartType=SeriesChartType.Pie;
]["
PieLabelStyle"
]="
Inside"
//Inside数值显示在圆饼内Outside数值显示在圆饼外Disabled不显示数值
PieLineColor"
Black"
//this.ct_coll.Series["
].IsValueShownAsLabel=true;
].IsVisibleInLegend=true;
].ShadowOffset=1;
//阴影偏移量
].ToolTip="
#VAL{D}人"
//鼠标移动到上面显示的文字
].BackSecondaryColor=Color.DarkCyan;
].BorderColor=Color.DarkOliveGreen;
].LabelBackColor=Color.Transparent;
foreach(DataRowdrin_dt.Rows)
intptIdx=this.ct_coll.Series["
].Points.AddY(Convert.ToDouble(dr["
columndata"
].ToString()));
DataPointpt=this.ct_coll.Series["
].Points[ptIdx];
pt.LegendText=dr["
columnname"
].ToString()+"
+"
#PERCENT{P2}"
["
]"
//右边标签列显示的文字
pt.Label=dr["
+"
//圆饼外显示的信息
//pt.LabelToolTip="
//pt.LabelBorderColor=Color.Red;
//文字背景色
//this.ct_coll.Series["
].Label="
//
].Font=newFont("
SegoeUI"
8.0f,FontStyle.Bold);
].Legend="
//右边标签列显示信息
this.ct_coll.Legends["
].Alignment=StringAlignment.Center;
].HeaderSeparator=System.Windows.Forms.DataVisualization.Charting.LegendSeparatorStyle.ThickLine;
this.ct_coll.Titles[0].Text=_title;
this.ct_coll.ChartAreas["
ChartArea1"
].AxisX.IsMarginVisible=false;
].Area3DStyle.Enable3D=true;
intint_count=_dt.AsEnumerable().Select(t=>
t.Field<
int>
("
)).Sum();
this.ct_coll.Titles[1].Text="
总人数:
+int_count.ToString("
0.00"
)+"
人"
/*
#VALX显示当前图例的X轴的对应文本(或数据)
#VAL,#VALY,显示当前图例的Y轴的对应文本(或数据)
#VALY2,#VALY3,显示当前图例的辅助Y轴的对应文本(或数据)
#SER:
显示当前图例的名称
#LABEL显示当前图例的标签文本
#INDEX显示当前图例的索引
#P