ZedGraph控件生成饼图、拆线图和柱状图例程_第1页
ZedGraph控件生成饼图、拆线图和柱状图例程_第2页
ZedGraph控件生成饼图、拆线图和柱状图例程_第3页
ZedGraph控件生成饼图、拆线图和柱状图例程_第4页
ZedGraph控件生成饼图、拆线图和柱状图例程_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、这几天做了个自己觉得还蛮NB的功能,在GRID中选中一块数据,然后右键菜单即可生成三种图形,还可以互相切换,对了,饼图还添加了鼠标点击分离的处理,大致如图:用的控件就是ZedGraph,我把它继承封装了一下,方便调用: 1 Imports ZedGraph 2 3 Public Class FinexGraph 4 用于切换图型的 5 Private dtSource As DataTable 6 7 Private Function FinexGraph_MouseDownEvent(ByVal sender As ZedGraph.ZedGraphC

2、ontrol, ByVal e As System.Windows.Forms.MouseEventArgs) As System.Boolean Handles MyBase.MouseDownEvent 8 9 If e.Button = Windows.Forms.MouseButtons.Left Then 10 Dim pane As GraphPane = sender.GraphPane 11 Dim pt As New PointF(e.X, e.Y) 12 Dim curve As CurveItem = Nothing 13 Dim i% 14 If pane.FindNe

3、arestPoint(pt, curve, i) Then 15 If curve.IsPie Then 16 Dim pi As PieItem = CType(curve, PieItem) 17 If pi.Displacement 0 Then 18 pi.Displacement = 0 19 Else 20 pi.Displacement = 0.3 21 End If 22 sender.Refresh() 23 End If 24 End If 25 End If 26 Return True 27 28 End Function 29 30 Public Sub SetTit

4、le(ByVal title As String) 31 Set the pane title 32 GraphPane.Title.Text = title 33 End Sub 34 35 Public Sub SetPieDataB(ByVal dt As DataTable, ByVal title As String) 36 37 为多列一行的数据,按列分块,不支持切换,不推荐使用 38 39 Dim ds As New DataSet 40 Dim i, j As Integer 41 Dim cc, rc As Integer 42 43 r

5、c = dt.Rows.Count 44 cc = dt.Columns.Count 45 If rc = 0 Or cc = 0 Then Return 46 47 GraphPane.CurveList.Clear() 48 GraphPane.GraphObjList.Clear() 49 GraphPane.Legend.IsVisible = True 50 GraphPane.Legend.Position = LegendPos.Right 51 GraphPane.Legend.FontSpec.Size = 7 52 If String.IsNullOrEmpty(title

6、) = False Then 53 GraphPane.Title.Text = title 54 End If 55 56 Dim labels(cc - 1) As String 57 Dim values(cc - 1) As Double 58 59 For i = 0 To cc - 1 60 labels(i) = dt.Columns(i).ColumnName 61 values(i) = dt.Rows(0).Item(i) 62 Next 63 Dim colors() As Color = Color.Red, Color.Blue, Color.Green, Color

7、.Yellow, Color.Purple, Color.Brown, Color.Coral, Color.ForestGreen 64 Dim slices() As PieItem = GraphPane.AddPieSlices(values, labels) 65 GraphPane.Fill = New Fill(Color.Cornsilk) 66 GraphPane.Chart.Fill = New Fill(Color.Cornsilk) 67 GraphPane.Fill = New Fill(Color.White, Color.Goldenrod, 45.0F) 68

8、69 For i = 0 To cc - 1 70 slices(i).LabelType = PieLabelType.Name_Value_Percent 71 Next 72 73 GraphPane.AxisChange() 74 Refresh() 75 dtSource = Nothing 76 FlowLayoutPanel1.Controls.Clear() 77 78 End Sub 79 80 Public Sub SetPieData(ByVal dt As DataTable, ByVal title As String) 81 82 TABLE内容应为第一列为各块名称

9、,第二列为各块的值,不能只有一列 83 84 Dim ds As New DataSet 85 Dim i, j As Integer 86 Dim cc, rc As Integer 87 88 rc = dt.Rows.Count 89 cc = dt.Columns.Count 90 If rc = 0 Or cc 1169 GraphPane.Legend.Position = LegendPos.Top170 171 Dim values(rc - 1) As Double172 Dim labels(rc - 1) As String173 174 Dim i%, j%, k%,

10、b%175 Dim alone As Boolean = cc = 1176 只有一列的情况下,X轴为序号177 For i = 0 To rc - 1178 If alone Then179 labels(i) = i + 1180 Else181 labels(i) = dt.Rows(i).Item(0)182 End If183 Next184 185 Dim colors() As Color = Color.Red, Color.Green, Color.Blue, Color.Orange, Color.Purple, Color.Pink186 Dim curve As Lin

11、eItem187 b = IIf(alone, 0, 1)188 For j = b To dt.Columns.Count - 1189 For i = 0 To dt.Rows.Count - 1190 values(i) = dt.Rows(i).Item(j)191 Next192 k = j - b193 If k = colors.Length Then194 k = j - b - colors.Length195 End If196 curve = GraphPane.AddCurve(dt.Columns(j).ColumnName, Nothing, values, col

12、ors(k), SymbolType.Circle)197 curve.Line.Width = 2.5198 curve.Line.IsAntiAlias = True199 curve.Symbol.Fill = New Fill(Color.White)200 curve.Symbol.Size = 8201 202 For i = 0 To curve.Points.Count - 1203 Dim pt As PointPair = curve.Points(i)204 Dim text As New TextObj(pt.Y.ToString(f2), pt.X, pt.Y + G

13、raphPane.YAxis.Scale.Max * 0.02, CoordType.AxisXYScale, AlignH.Left, AlignV.Center)205 text.ZOrder = ZOrder.A_InFront206 text.FontSpec.Size = 9207 text.FontSpec.Border.IsVisible = False208 text.FontSpec.Fill.IsVisible = False209 text.FontSpec.Fill = new Fill( Color.FromArgb( 100, Color.White ) )210

14、text.FontSpec.IsItalic = True211 text.FontSpec.Angle = 30 字体倾斜度212 GraphPane.GraphObjList.Add(text)213 Next214 215 Next216 GraphPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(255, Color.ForestGreen), 45.0F)217 218 GraphPane.XAxis.Scale.TextLabels = labels219 GraphPane.XAxis.Type = AxisType.T

15、ext220 GraphPane.XAxis.Scale.FontSpec.Size = 9221 If labels.Length 4 And labels(0).Length 4 Then222 GraphPane.XAxis.Scale.FontSpec.Angle = 30223 End If224 GraphPane.XAxis.IsVisible = True225 GraphPane.YAxis.IsVisible = True226 GraphPane.YAxis.MajorGrid.IsVisible = True227 228 GraphPane.AxisChange()2

16、29 Refresh()230 dtSource = dt231 232 FlowLayoutPanel1.Controls.Clear()233 AddButton(Pie, AddressOf btnPie_Click)234 AddButton(Bar, AddressOf btnBar_Click)235 236 End Sub237 238 Public Sub SetBarData(ByVal dt As DataTable)239 SetBarData(dt, Nothing, Nothing, Nothing)240 End Sub241 242 Public Sub SetB

17、arData(ByVal dt As DataTable, ByVal title As String)243 SetBarData(dt, title, Nothing, Nothing)244 End Sub245 246 Public Sub SetBarData(ByVal dt As DataTable, ByVal title As String, ByVal xAxisTitle As String, ByVal yAxisTitle As String)247 248 TABLE内容应为第一列为X轴值,第二列开始为Y轴数值,除非只有一列249 250 Dim cc As Int

18、eger = dt.Columns.Count251 Dim rc As Integer = dt.Rows.Count252 253 If cc = 0 Or rc = 0 Then Return254 255 Dim x(rc - 1) As Double256 Dim values(rc - 1) As Double257 Dim labels(rc - 1) As String258 259 GraphPane.CurveList.Clear()260 GraphPane.GraphObjList.Clear()261 If String.IsNullOrEmpty(title) =

19、False Then262 GraphPane.Title.Text = title263 End If264 If String.IsNullOrEmpty(xAxisTitle) Then265 GraphPane.XAxis.Title.Text = xAxisTitle266 End If267 If String.IsNullOrEmpty(yAxisTitle) Then268 GraphPane.YAxis.Title.Text = yAxisTitle269 End If270 GraphPane.Legend.IsVisible = cc 1271 GraphPane.Leg

20、end.Position = LegendPos.Top272 273 Dim i%, j%, k%, b%274 Dim alone As Boolean = cc = 1275 Dim bi As BarItem276 277 只有一列的情况下,X轴为序号278 For i = 0 To rc - 1279 If alone Then280 labels(i) = i + 1281 Else282 labels(i) = dt.Rows(i).Item(0)283 End If284 Next285 286 If alone Then287 For i = 0 To rc - 1288 x

21、(i) = i + 1289 values(i) = dt.Rows(i).Item(0)290 Next291 bi = GraphPane.AddBar(dt.Columns(0).ColumnName, x, values, Color.Blue)292 bi.Bar.Fill = New Fill(Color.Blue, Color.White, Color.Green)293 Else294 Dim colors() As Color = Color.Red, Color.Green, Color.Blue, Color.Orange, Color.Purple, Color.Pin

22、k295 296 For j = 1 To dt.Columns.Count - 1297 For i = 0 To rc - 1298 values(i) = dt.Rows(i).Item(j)299 Next300 k = j - 1301 If k = colors.Length Then302 k = j - 1 - colors.Length303 End If304 bi = GraphPane.AddBar(dt.Columns(j).ColumnName, Nothing, values, colors(k)305 bi.Bar.Fill = New Fill(colors(

23、k), Color.White, colors(k)306 Next307 308 For i = 0 To bi.Points.Count - 1309 Dim pt As PointPair = bi.Points(i)310 Dim text As New TextObj(pt.Y.ToString(f2), pt.X, pt.Y + GraphPane.YAxis.Scale.Max * 0.02, CoordType.AxisXYScale, AlignH.Left, AlignV.Center)311 text.ZOrder = ZOrder.A_InFront312 text.F

24、ontSpec.Size = 9313 text.FontSpec.Border.IsVisible = False314 text.FontSpec.Fill.IsVisible = False315 text.FontSpec.IsItalic = True316 GraphPane.GraphObjList.Add(text)317 Next318 319 End If320 321 GraphPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(255, Color.ForestGreen), 45.0F)322 323 Grap

25、hPane.XAxis.Scale.TextLabels = labels324 GraphPane.XAxis.Type = AxisType.Text325 GraphPane.XAxis.Scale.FontSpec.Size = 9326 If labels.Length 4 And labels(0).Length 4 Then327 GraphPane.XAxis.Scale.FontSpec.Angle = 30328 End If329 GraphPane.XAxis.IsVisible = True330 GraphPane.YAxis.IsVisible = True331

26、 GraphPane.YAxis.MajorGrid.IsVisible = True332 333 GraphPane.AxisChange()334 Refresh()335 dtSource = dt336 337 FlowLayoutPanel1.Controls.Clear()338 AddButton(Pie, AddressOf btnPie_Click)339 AddButton(Curve, AddressOf btnCurve_Click)340 341 End Sub342 343 Private Sub AddButton(ByVal text As String, ByVal clickEvent As EventHandler)344 Dim btn As New Button345 btn.Text = text346 AddHandler btn.Click, clickEvent347 FlowLayoutPanel1.Controls.Add(btn)348 End Su

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论