这是工作的时候总结的,用的时候,只要传ZedGraph控件的对象、图的标题、点的数组、和标签数组进来就可以了.当然先要添加这个控件和引用.
#Region "三种绘图函数" Dim ArColor As New ArrayList '颜色数组 Private Sub InitColor() '初始化颜色数组 ArColor.Clear() ArColor.Add(Color.Gold) ArColor.Add(Color.Green) ArColor.Add(Color.Red) ArColor.Add(Color.Indigo) ArColor.Add(Color.Blue) ArColor.Add(Color.Brown) ArColor.Add(Color.Yellow) ArColor.Add(Color.Cornsilk) ArColor.Add(Color.Pink) ArColor.Add(Color.YellowGreen) ArColor.Add(Color.Firebrick) ArColor.Add(Color.DimGray) ArColor.Add(Color.Aqua) ArColor.Add(Color.Indigo) ArColor.Add(Color.DarkSeaGreen) End Sub ''' <summary> ''' 绘制饼图 ''' </summary> ''' <param name="zgc">绘图控件</param> ''' <param name="strTitle">图标题</param> ''' <param name="strValue">Y轴值数组</param> ''' <param name="strLabel">X轴标签数组</param> ''' <returns></returns> ''' <remarks></remarks> Public Function CreateGraph_Pie(ByVal zgc As ZedGraphControl, ByVal strTitle As String, ByVal strValue() As Double, ByVal strLabel() As String) As Boolean Try InitColor() Dim myPane As New GraphPane myPane = zgc.GraphPane '清空画布 myPane.CurveList.Clear() myPane.GraphObjList.Clear() '设置图例 myPane.Legend.Position = LegendPos.Right myPane.Legend.FontSpec.Size = 12.0F myPane.Legend.FontSpec.FontColor = Color.Navy myPane.Legend.IsHStack = True '饼图标题 myPane.Title.FontSpec.IsBold = True myPane.Title.FontSpec.FontColor = Color.Navy myPane.Title.Text = strTitle Dim m As Integer = CInt(strValue.Length) Dim j As Integer = 0 Dim segment(m - 1) As PieItem For i As Integer = 0 To m - 1 segment(i) = myPane.AddPieSlice(strValue(i), ArColor(j), Color.White, 45.0F, 0, strLabel(i)) segment(i).LabelType = PieLabelType.Name_Value_Percent segment(i).LabelDetail.FontSpec.Size = 10.0F segment(i).LabelDetail.FontSpec.FontColor = Color.Blue segment(i).LabelDetail.FontSpec.IsBold = True '考虑到分类数可能超过颜色数组的维数 j = j + 1 If j > ArColor.Count - 1 Then j = 0 End If Next i zgc.AxisChange() zgc.Refresh() Return True Catch ex As Exception Return False End Try End Function ''' <summary> ''' 绘制折线图 ''' </summary> ''' <param name="zgc">绘图控件</param> ''' <param name="strTitle">图标题</param> ''' <param name="strXTitle">X轴标题</param> ''' <param name="strYTitle">Y轴标题</param> ''' <param name="strValue">值的二维数组</param> ''' <param name="strXLabel">X轴标签数组</param> ''' <param name="strLineLabel">线的标签数组</param> ''' <returns></returns> ''' <remarks></remarks> Public Function CreateGraph_Line(ByVal zgc As ZedGraphControl, ByVal strTitle As String, ByVal strXTitle As String, ByVal strYTitle As String, ByVal strValue(,) As Double, ByVal strXLabel() As String, ByVal strLineLabel() As String, Optional ByVal strFlag As String = "P") As Boolean Try InitColor() Dim myPane As New GraphPane myPane = zgc.GraphPane myPane.CurveList.Clear() myPane.GraphObjList.Clear() '设置图例 myPane.Legend.Position = LegendPos.Top myPane.Legend.FontSpec.Size = 10.0F myPane.Legend.FontSpec.FontColor = Color.Red myPane.Legend.IsHStack = True myPane.Title.FontSpec.IsBold = True myPane.Title.FontSpec.FontColor = Color.Orange myPane.Title.Text = strTitle myPane.XAxis.MajorGrid.IsVisible = True myPane.YAxis.MajorGrid.IsVisible = True Dim FLen As Integer = strValue.GetLength(0) '一维的长度 Dim SLen As Integer = strValue.GetLength(1) '二维的长度 '生成随机颜色 Dim rd As New Random For i As Integer = 0 To SLen - 1 Dim list1 As New PointPairList For j As Integer = 0 To FLen - 1 list1.Add(j + 1, strValue(j, i)) Next Dim gy(0) As LineItem gy(0) = myPane.AddCurve(strLineLabel(i), list1, Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255)), SymbolType.Diamond) '设置线宽 gy(0).Line.Width = 2.0F gy(0).Line.IsAntiAlias = True gy(0).Symbol.Fill = New Fill(Color.White) gy(0).Symbol.Size = 7 '在每个点上显示具体的值 For m As Integer = 0 To FLen - 1 Dim pt As New PointPair pt = gy(0).Points(m) Dim txt As New TextObj(pt.Y, pt.X, pt.Y * 1.01, CoordType.AxisXYScale, AlignH.Left, AlignV.Center) txt.ZOrder = ZOrder.A_InFront txt.FontSpec.Border.IsVisible = False txt.FontSpec.Fill.IsVisible = False myPane.GraphObjList.Add(txt) Next Next '设置背景 myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(255, Color.ForestGreen), 45.0F) myPane.XAxis.Type = AxisType.Text myPane.XAxis.Title.FontSpec.Size = 15.0F myPane.XAxis.Title.FontSpec.FontColor = Color.Blue myPane.YAxis.Title.FontSpec.FontColor = Color.Blue myPane.Fill.IsScaled = True myPane.XAxis.Title.Text = strXTitle myPane.YAxis.Title.Text = strYTitle myPane.XAxis.Scale.IsPreventLabelOverlap = False myPane.XAxis.Scale.FontSpec.Size = 8.0F myPane.XAxis.Scale.FontSpec.FontColor = Color.Red myPane.XAxis.Scale.TextLabels = strXLabel myPane.XAxis.Scale.FormatAuto = True zgc.AxisChange() zgc.Refresh() Return True Catch ex As Exception Return False End Try End Function ''' <summary> ''' 绘制柱状图 ''' </summary> ''' <param name="zgc">绘图控件</param> ''' <param name="strTitle">图标题</param> ''' <param name="strXTitle">X轴标题</param> ''' <param name="strYTitle">Y轴标题</param> ''' <param name="strValue">值的二维数组</param> ''' <param name="strXLabel">X轴标签数组</param> ''' <param name="strBarLabel">柱的标签数组</param> ''' <returns></returns> ''' <remarks></remarks> Public Function CreateGraph_Bar(ByVal zgc As ZedGraphControl, ByVal strTitle As String, ByVal strXTitle As String, ByVal strYTitle As String, ByVal strValue(,) As Double, ByVal strXLabel() As String, ByVal strBarLabel() As String) As Boolean Try Dim myPane As New GraphPane myPane = zgc.GraphPane myPane.CurveList.Clear() myPane.GraphObjList.Clear() '设置图例 myPane.Legend.Position = LegendPos.Top myPane.Legend.FontSpec.Size = 10.0F myPane.Legend.FontSpec.FontColor = Color.Red myPane.Legend.IsHStack = True myPane.Title.FontSpec.IsBold = True myPane.Title.FontSpec.FontColor = Color.Orange myPane.Title.Text = strTitle myPane.XAxis.MajorGrid.IsVisible = True myPane.YAxis.MajorGrid.IsVisible = True Dim FLen As Integer = strValue.GetLength(0) '一维的长度 Dim SLen As Integer = strValue.GetLength(1) '二维的长度 For i As Integer = 0 To SLen - 1 Dim list1 As New PointPairList For j As Integer = 0 To FLen - 1 list1.Add(j + 1, strValue(j, i)) Next Dim gy(0) As BarItem gy(0) = myPane.AddBar(strBarLabel(i), list1, ArColor(i)) Next '设置背景 myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(255, Color.ForestGreen), 45.0F) myPane.XAxis.IsAxisSegmentVisible = False myPane.XAxis.Type = AxisType.Text myPane.XAxis.Title.Text = strXTitle myPane.YAxis.Title.Text = strYTitle myPane.YAxis.IsAxisSegmentVisible = True myPane.XAxis.Scale.TextLabels = strXLabel myPane.XAxis.Scale.FontSpec.Size = 9.0F myPane.XAxis.Scale.FontSpec.FontColor = Color.Blue myPane.BarSettings.Type = BarType.Cluster BarItem.CreateBarLabels(myPane, False, "0") '在柱状图顶部显示数值 myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(255, Color.ForestGreen), 45.0F) zgc.AxisChange() zgc.Refresh() Return True Catch ex As Exception Return False End Try End Function #End Region