您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > ZedGraph控件生成饼图、拆线图和柱状图例程
这几天做了个自己觉得还蛮NB的功能,在GRID中选中一块数据,然后右键菜单即可生成三种图形,还可以互相切换,对了,饼图还添加了鼠标点击分离的处理,大致如图:用的控件就是ZedGraph,我把它继承封装了一下,方便调用:1ImportsZedGraph23PublicClassFinexGraph4'用于切换图型的5PrivatedtSourceAsDataTable6(ByValsenderAsZedGraph.ZedGraphControl,ByValeAsSystem.Windows.Forms.MouseEventArgs)AsSystem.BooleanHandlesMyBase.MouseDownEvent89Ife.Button=Windows.Forms.MouseButtons.LeftThen10DimpaneAsGraphPane=sender.GraphPane11DimptAsNewPointF(e.X,e.Y)12DimcurveAsCurveItem=Nothing13Dimi%14Ifpane.FindNearestPoint(pt,curve,i)Then15Ifcurve.IsPieThen16DimpiAsPieItem=CType(curve,PieItem)17Ifpi.Displacement0Then18pi.Displacement=019Else20pi.Displacement=0.321EndIf22sender.Refresh()23EndIf24EndIf25EndIf26ReturnTrue2728EndFunction2930PublicSubSetTitle(ByValtitleAsString)31''Setthepanetitle32GraphPane.Title.Text=title33EndSub34(ByValdtAsDataTable,ByValtitleAsString)3637'为多列一行的数据,按列分块,不支持切换,不推荐使用3839DimdsAsNewDataSet40Dimi,jAsInteger41Dimcc,rcAsInteger4243rc=dt.Rows.Count44cc=dt.Columns.Count45Ifrc=0Orcc=0ThenReturn4647GraphPane.CurveList.Clear()48GraphPane.GraphObjList.Clear()49GraphPane.Legend.IsVisible=True50GraphPane.Legend.Position=LegendPos.Right51GraphPane.Legend.FontSpec.Size=752IfString.IsNullOrEmpty(title)=FalseThen53GraphPane.Title.Text=title54EndIf5556Dimlabels(cc-1)AsString57Dimvalues(cc-1)AsDouble5859Fori=0Tocc-160labels(i)=dt.Columns(i).ColumnName61values(i)=dt.Rows(0).Item(i)62Next63'Dimcolors()AsColor={Color.Red,Color.Blue,Color.Green,Color.Yellow,Color.Purple,Color.Brown,Color.Coral,Color.ForestGreen}64Dimslices()AsPieItem=GraphPane.AddPieSlices(values,labels)65GraphPane.Fill=NewFill(Color.Cornsilk)66GraphPane.Chart.Fill=NewFill(Color.Cornsilk)67'GraphPane.Fill=NewFill(Color.White,Color.Goldenrod,45.0F)6869Fori=0Tocc-170slices(i).LabelType=PieLabelType.Name_Value_Percent71Next7273GraphPane.AxisChange()74Refresh()75dtSource=Nothing76FlowLayoutPanel1.Controls.Clear()7778EndSub7980PublicSubSetPieData(ByValdtAsDataTable,ByValtitleAsString)8182'TABLE内容应为第一列为各块名称,第二列为各块的值,不能只有一列8384DimdsAsNewDataSet85Dimi,jAsInteger86Dimcc,rcAsInteger8788rc=dt.Rows.Count89cc=dt.Columns.Count90Ifrc=0Orcc=1ThenReturn9192GraphPane.CurveList.Clear()93GraphPane.GraphObjList.Clear()94GraphPane.Legend.IsVisible=True95GraphPane.Legend.Position=LegendPos.Right96GraphPane.Legend.FontSpec.Size=797IfString.IsNullOrEmpty(title)=FalseThen98GraphPane.Title.Text=title99EndIf100101Dimlabels(rc-1)AsString102Dimvalues(rc-1)AsDouble103104Forj=1Tocc-1105Fori=0Torc-1106labels(i)=dt.Rows(i).Item(0)107values(i)=dt.Rows(i).Item(j)108Next109'Dimcolors()AsColor={Color.Red,Color.Blue,Color.Green,Color.Yellow,Color.Purple,Color.Brown,Color.Coral,Color.ForestGreen}110Dimslices()AsPieItem=GraphPane.AddPieSlices(values,labels)111GraphPane.Fill=NewFill(Color.Cornsilk)112GraphPane.Chart.Fill=NewFill(Color.Cornsilk)113'GraphPane.Fill=NewFill(Color.White,Color.Goldenrod,45.0F)114115Fori=0Torc-1116slices(i).LabelType=PieLabelType.Name_Value_Percent117Next118Next119120GraphPane.AxisChange()121Refresh()122dtSource=dt123124FlowLayoutPanel1.Controls.Clear()125AddButton(Curve,AddressOfbtnCurve_Click)126AddButton(Bar,AddressOfbtnBar_Click)127128EndSub129130PublicSubSetPieData(ByValdtAsDataTable)131132SetPieData(dt,)133134EndSub135136PublicSubSetCurveData(ByValdtAsDataTable)137SetCurveData(dt,,,)138EndSub139140PublicSubSetCurveData(ByValdtAsDataTable,ByValtitleAsString)141SetCurveData(dt,title,,)142EndSub143144PublicSubSetCurveData(ByValdtAsDataTable,ByValtitleAsString,ByValxAxisTitleAsString,ByValyAxisTitleAsString)145146'TABLE内容应为第一列为X轴值,第二列开始为Y轴数值,除非只有一列147148IfdtIsNothingThenReturn149150DimccAsInteger=dt.Columns.Count151DimrcAsInteger=dt.Rows.Count152153Ifrc=0Orcc=0ThenReturn154155GraphPane.CurveList.Clear()156GraphPane.GraphObjList.Clear()157158IfString.IsNullOrEmpty(title)=FalseThen159GraphPane.Title.Text=title160EndIf161IfString.IsNullOrEmpty(xAxisTitle)Then162GraphPane.XAxis.Title.Text=xAxisTitle163EndIf164IfString.IsNullOrEmpty(yAxisTitle)Then165GraphPane.YAxis.Title.Text=yAxisTitle166EndIf167168GraphPane.Legend.IsVisible=cc1169GraphPane.Legend.Position=LegendPos.Top170171Dimvalues(rc-1)AsDouble172Dimlabels(rc-1)AsString173174Dimi%,j%,k%,b%175DimaloneAsBoolean=cc=1176'只有一列的情况下,X轴为序号177Fori=0Torc-1178IfaloneThen179labels(i)=i+1180Else181labels(i)=dt.Rows(i).Item(0)182EndIf183Next184185Dimcolors()AsColor={Color.Red,Color.Green,Color.Blue,Color.Orange,Color.Purple,Color.Pink}186DimcurveAsLineItem187b=IIf(alone,0,1)188Forj=bTodt.Columns.Count-1189Fori=0Todt.Rows.Count-1190values(i)=dt.Rows(i).Item(j)191Next192k=j-b193Ifk=colors.LengthThen194k=j-b-colors.Length195EndIf196curve=GraphPane.AddCurve(dt.Columns(j).ColumnName,Nothing,values,colors(k),SymbolType.Circle)197curve.Line.Width=2.5198curve.Line.IsAntiAlias=True199curve.Symbol.Fill=NewFill(Color.White)200curve.Symbol.Size=8201202Fori=0Tocurve.Points.Count-1203DimptAsPointPair=curve.Points(i)204DimtextAsNewTextObj(pt.Y.ToString(f2),pt.X
本文标题:ZedGraph控件生成饼图、拆线图和柱状图例程
链接地址:https://www.777doc.com/doc-3123323 .html