C#NET WinForm 绘制饼图直方图曲线图 详细代码Word文档下载推荐.docx
《C#NET WinForm 绘制饼图直方图曲线图 详细代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C#NET WinForm 绘制饼图直方图曲线图 详细代码Word文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。
usingSystem.Security.Cryptography;
usingSystem.Drawing.Imaging;
usingSystem.Collections;
publicclassFunc
{
privatestaticAChartchart=newAChart();
privatestaticObject[]ChartColor={Color.Red,Color.Blue,Color.Orange,Color.Green,Color.Cyan,Color.Purple,Color.Coral,Color.Chocolate,Color.Gray,Color.Gold,Color.Lavender,Color.Linen,Color.Magenta,Color.Moccasin,Color.Navy,Color.Olive,Color.Peru,Color.Plum,Color.Purple,Color.Salmon,Color.Sienna,Color.Silver,Color.Tan,Color.Tomato,Color.Violet,Color.Turquoise,Color.Transparent};
///<
summary>
///填充饼图、直方图、曲线图到容器里
/summary>
paramname="
chartTitle"
>
标题<
/param>
control"
容器(Panel,Form,TabPage)<
dataSet"
对Table[0]进行操作,饼图取最前两列,第一列为名字,第二列为值。
单数据直方图取最前两列,第一列为横轴每列名称,第二列为值。
多数据直方图第一列为横轴父项名称,然后依次取前一列为横轴每列名称,后一列为值。
曲线图取第一列为横轴每列名称,往后每列都代表一条曲线,列名为曲线名称。
<
chartType"
图表类型<
minNumber"
刻度最小值,此参数对饼图无效<
maxNumber"
刻度最大值,此参数对饼图无效<
scale"
刻度值,此参数对饼图无效<
unit"
值的单位,此参数对饼图无效<
publicstaticvoidDrawingChart(stringchartTitle,Controlcontrol,DataSetdataSet,ChartTypechartType,intminNumber,intmaxNumber,intscale,stringunit)
DrawingChartInclude(chartTitle,control,dataSet,chartType,minNumber,maxNumber,scale,unit);
}
///填充饼图到容器里
对Table[0]进行操作,取最前两列,第一列为名字,第二列为值<
publicstaticvoidDrawingChart(stringchartTitle,Controlcontrol,DataSetdataSet)
DrawingChartInclude(chartTitle,control,dataSet,ChartType.Piegraph,0,0,0,"
"
);
privatestaticvoidDrawingChartInclude(stringchartTitle,Controlcontrol,DataSetdataSet,ChartTypechartType,intminNumber,intmaxNumber,intscale,stringunit)
if(control.Height<
230||control.Width<
230)
Func.Msg("
容器宽度或长度设置过小!
(宽度和长度必须>
=230)"
return;
if(control.GetType().Name.Equals("
Form"
)||control.GetType().Name.Equals("
TabPage"
GroupBox"
Panel"
))
for(inti=0;
i<
chart.Count;
i++)
if(control==(Control)chart[i].crtObj)
chart.RemoveChart(i);
break;
chart.AddChart(chartTitle,control,chartType,dataSet,minNumber,maxNumber,scale,unit);
control.Paint+=newPaintEventHandler(control_Paint);
control.Disposed+=newEventHandler(control_Disposed);
control.Refresh();
else
该容器不是Form,TabPage,GroupBox,Panel类型!
staticvoidcontrol_Paint(objectsender,PaintEventArgse)
try
if(chart[i].crtType==ChartType.None)
elseif(chart[i].crtObj!
=sender)
continue;
intx=0;
inty=0;
intdiameter=Math.Min(((Control)chart[i].crtObj).Height,((Control)chart[i].crtObj).Width)-100;
if(((Control)chart[i].crtObj).Width>
=((Control)chart[i].crtObj).Height)
x=(((Control)chart[i].crtObj).Width-((Control)chart[i].crtObj).Height)/2+50;
y=50;
x=50;
y=(((Control)chart[i].crtObj).Height-((Control)chart[i].crtObj).Width)/2+50;
Penline=newPen(Color.Black,2);
SolidBrushlinesb=newSolidBrush(Color.DarkGreen);
Rectanglerect=newRectangle(x,y,diameter,diameter);
Pentitle=newPen(Color.Blue,2);
SolidBrushB=newSolidBrush(Color.Blue);
StringFormatLl=newStringFormat();
Ll.Alignment=System.Drawing.StringAlignment.Near;
intheight=((Control)chart[i].crtObj).Height-20;
RectangleFRectTitle=newRectangleF(5,5,diameter,diameter);
e.Graphics.DrawString(chart[i].crtTitle,newFont("
Arial"
14),B,RectTitle,Ll);
#region饼图
if(chart[i].crtType==ChartType.Piegraph)
floattotal=0;
floatfontWidth=0;
for(intj=0;
j<
chart[i].crtDataSet.Tables[0].Rows.Count;
j++)
total+=(float)Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString());
if(fontWidth<
e.Graphics.MeasureString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(),newFont("
10)).Width)
fontWidth=e.Graphics.MeasureString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(),newFont("
10)).Width;
floatnext=0;
SolidBrushsb=newSolidBrush((Color)ChartColor[j%ChartColor.Length]);
floattmp=(float)(Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString())/total*100*3.6);
e.Graphics.FillPie(sb,rect,next,tmp);
RectTitle=newRectangleF(5,height,diameter,diameter);
B=newSolidBrush(Color.Black);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(),newFont("
10),B,RectTitle,Ll);
RectTitle=newRectangleF(5+(int)fontWidth+2,height,10,12);
e.Graphics.FillRectangle(sb,RectTitle);
RectTitle=newRectangleF(x+diameter+2,height,10,12);
RectTitle=newRectangleF(x+diameter+14,height,diameter,diameter);
e.Graphics.DrawString(Convert.ToString(Math.Round((Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString())/total*100),0))+"
%"
newFont("
height-=20;
next+=tmp;
#endregion
#region直方图
elseif(chart[i].crtType==ChartType.Histogram)
e.Graphics.DrawLine(line,x,y,x,y+diameter);
height=y+diameter-12;
intscaleBS=diameter/((chart[i].crtMaxNumber-chart[i].crtMinNumber)/chart[i].crtScale);
floatfontWidth=e.Graphics.MeasureString(chart[i].crtMaxNumber.ToString(),newFont("
for(intj=chart[i].crtMinNumber;
=chart[i].crtMaxNumber;
j+=chart[i].crtScale)
RectangleFRectScaleHeightLine=newRectangleF(x-fontWidth-1,height,diameter,diameter);
e.Graphics.DrawString(j.ToString(),newFont("
10),linesb,RectScaleHeightLine,Ll);
height-=scaleBS;
height=y+diameter;
intwidth=x+10;
if(chart[i].crtDataSet.Tables[0].Columns.Count<
=2)
RectangleFRectScale=newRectangleF(width,height,diameter,diameter);
10),sb,RectScale,Ll);
doubleaa=Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString())/chart[i].crtScale*scaleBS;
RectScale=newRectangleF(width,y+diameter-(int)aa,20,(int)aa);
e.Graphics.FillRectangle(sb,RectScale);
RectScale=newRectangleF(width,y+diameter-(int)aa-15,diameter,diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString()+chart[i].crtUnit,newFont("
10),sb,RectScale);
intsbnum=0;
RectangleFRectScale=newRectangleF(width,height+24,diameter,diameter);
10),B,RectScale,Ll);
for(intk=1;
k<
chart[i].crtDataSet.Tables[0].Columns.Count;
k+=2)
SolidBrushsb=newSolidBrush((Color)ChartColor[sbnum%ChartColor.Length]);
sbnum++;
RectScale=newRectangleF(width,height+(sbnum%2)*12,diameter,diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][k].ToString(),newFont("
8),sb,RectScale,Ll);
doubleaa=Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][k+1].ToString())/chart[i].crtScale*scaleBS;
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][k+1].ToString()+chart[i].crtUnit,newFont("
8),sb,RectScale);
width+=30;
if((fontWidth+e.Graphics.MeasureString(chart[i].crtUnit,newFont("
10)).Width)>
=30)
width+=(int)fontWidth+(int)e.Graphics.MeasureString(chart[i].crtUnit,newFont("
10)).Width+10;
e.Graphics.DrawLine(line,x,y+diameter,width,y+diameter);
#region曲线图
elseif(chart[i].crtType==ChartType.Polygram)
intwidth=x;
intscaleWidthBS=diameter/chart[i].crtDataSet.Tables[0].Rows.Count;
height=y+diameter-((chart[i].crtMaxNumber-chart[i].crtMinNumber)/chart[i].crtScale)*scaleBS;
=chart[i].crtDataSet.Tables[0].Rows.Count;
e.Graphics.DrawLine(line,width,height,width,y+diameter);
width+=scaleWidthBS;
j+