在VB.NET中创建基本饼图
我在尝试解决图表控件无法正常工作的原因时遇到问题。我以前从未使用过饼图,而且我无法得到一个基本的饼图来填充。最后,我尝试创建一个带有百分比的分解饼图(如果可能的话) 到目前为止,我的代码是:在VB.NET中创建基本饼图,vb.net,charts,pie-chart,Vb.net,Charts,Pie Chart,我在尝试解决图表控件无法正常工作的原因时遇到问题。我以前从未使用过饼图,而且我无法得到一个基本的饼图来填充。最后,我尝试创建一个带有百分比的分解饼图(如果可能的话) 到目前为止,我的代码是: chrtRegisterAvailability.Series.Clear() chrtRegisterAvailability.Series.Add("Series1") chrtRegisterAvailability.Series("Series1").Points.AddXY("Online", 6
chrtRegisterAvailability.Series.Clear()
chrtRegisterAvailability.Series.Add("Series1")
chrtRegisterAvailability.Series("Series1").Points.AddXY("Online", 60)
chrtRegisterAvailability.Series("Series1").Points.AddXY("Offline", 40)
chrtRegisterAvailability.Series("Series1").ChartType = SeriesChartType.Pie
chrtRegisterAvailability.Series("Series1")("PieLabelStyle") = "Outside"
chrtRegisterAvailability.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
chrtRegisterAvailability.Series.Add("Series1")
毫无疑问,我错过了一些东西,因为我尝试在网上找到了各种各样的例子,但没有运气。感谢您的帮助。请尝试此代码。可能是因为没有清理图表才是问题所在
With Me.chrtRegisterAvailability
.Legends.Clear()
.Series.Clear()
.ChartAreas.Clear()
End With
Dim areas1 As ChartArea = Me.chrtRegisterAvailability.ChartAreas.Add("Areas1")
With areas1
End With
Dim series1 As Series = Me.chrtRegisterAvailability.Series.Add("Series1")
With series1
.ChartArea = areas1.Name
.ChartType = SeriesChartType.Pie
.Points.AddXY("Online", 60)
.Points.AddXY("Offline", 40)
End With
Dim legends1 As Legend = Me.chrtRegisterAvailability.Legends.Add("Legends1")
Public子图()
Title.Text=标题名
Title.Left=639-Int(Title.Width/2)
绘图图()
()
端接头
公共子标签()
ReDim标签(Data.Length-1,3)
暗度按双精度
对于x=0到LabelNames.GetLength(0)-1
Dim l作为新标签
l、 左=20
l、 顶部=50+(40*x)
l、 宽度=240
l、 高度=24
l、 Font=New System.Drawing.Font(“Microsoft无衬线”,14.0!,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,CType(0,字节))
l、 Text=标签名称(x)
l、 Name=“L0”和x
标签(x,0)=l
面板2.控件。添加(l)
将l1调暗为新标签
l1.左=275
l1.顶部=50+(40*x)
l1.宽度=20
l1.高度=24
l1.背景色=颜色(x)
l1.Font=New System.Drawing.Font(“Microsoft Sans Serif”,14.0!,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,CType(0,字节))
l1.Text=“”
l1.Name=“l1”&x
标签(x,1)=l1
面板2.控件。添加(l1)
Dim l2作为新标签
l2.左=315
l2.顶部=50+(40*x)
l2.宽度=90
l2.高度=24
l2.Font=New System.Drawing.Font(“Microsoft Sans Serif”,14.0!,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,CType(0,字节))
per=(尺寸(x)/360)*100
l2.Text=格式(每“#####.####”号和“%”
l2.Name=“l2”&x
标签(x,2)=l2
面板2.控件。添加(l2)
尺寸l3为新标签
l3.左=425
l3.顶部=50+(40*x)
l3.宽度=140
l3.高度=24
l3.Font=新的System.Drawing.Font(“Microsoft Sans Serif”,14.0!,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,CType(0,字节))
per=(尺寸(x)/252)*100
l3.Text=格式(数据(x),“$”、“$”、“$”、“.00”)
l3.Name=“l3”&x
标签(x,3)=l3
面板2.控件。添加(l3)
下一个
端接头
私有函数PiceSize(RawData()作为Double)作为数组
将Dsum设置为双精度
将ps设置为整数=RawData.Length-1
尺寸计数,z,尺寸0(ps),尺寸1(ps),尺寸2(ps),尺寸3(ps),最大值为整数
作为阵列的尺寸(2)
对于x=0到RawData.GetLength(0)-1
Dsum=Dsum+RawData(x)
下一个
对于x=0到ps
尺寸0(x)=整数((原始数据(x)/Dsum)*360)
如果((原始数据(x)/Dsum)*360)-size0(x)>0.49999,则size0(x)=size0(x)+1
计数=计数+大小0(x)
下一个
z=-1
如果计数小于360,则
对于x=计数+1到360
z=z+1
如果z>size.GetLength(0),则z=0
尺寸0(z)=尺寸0(z)+1
计数=计数+1
下一个
如果结束
如果计数>360,则
对于x=计数-1到360步-1
z=z+1
如果z>size.GetLength(0),则z=0
尺寸0(z)=尺寸0(z)-1
计数=计数-1
下一个
如果结束
计数=0
对于x=0到ps
如果x=0,则尺寸1(x)=0
如果x=0,则尺寸2(x)=尺寸0(x)
如果x=0,则size3(x)=Int(size0(x)/2)
如果x 0,则大小1(x)=计数
如果x为0,则尺寸2(x)=计数+尺寸0(x)
如果x为0,那么size3(x)=size1(x)+Int(size0(x)/2)
如果x=ps,则尺寸2(x)=尺寸2(x)
计数=计数+大小0(x)
下一个
SArc=尺寸1
EArc=尺寸2
CArc=尺寸3
PiceSize=size0
端函数
私有子图()
尺寸x1、y1、z、p为整数
Dim大小(Data.GetLength(0)-1)为整数
将ps设置为整数=数据。GetLength(0)-1
尺寸Ecenter1(ps)作为点
PieSize=PiceSize(数据)
z=1
sc.高度=600
sc.宽度=600
sc.Top=74
sc.左=608
对于num As Double=0到359
x1=转换为32(半径*数学系数(度数))+Center.X)
y1=转换为32(半径*数学Sin(度数(num))+Center.Y)
如果num=CArc(p),则
Ecenter1(p).X=Convert.ToInt32(50*Math.Cos(Degree(num))+Center.X)
Ecenter1(p).Y=Convert.ToInt32(50*Math.Sin(Degree(num))+Center.Y)
如果结束
将线暗显为新线型
Line.X1=中心.X
Line.Y1=中心.Y
直线X2=x1
第2行Y2=y1
Line.BorderColor=颜色(p)
线条宽度=5
Line.Visible=True
Line.Name=“Line”&Chr(p+65)&z
如果z=PieSize(p),则
p=p+1
z=0
如果结束
z=z+1
GLine(num)=行
AddHandler Line.MouseHover,CicrleHover的地址
AddHandler Line.MouseLeave,CicrleLeave的地址
AddHandler行。单击,地址为
AddHandler Line.DoubleClick,地址为ChangeColor
sc.Shapes.Add(行)
下一个
ECenter=Ecenter1
面板1.控件。添加(sc)
端接头
末端区域
区域“活动”
Protected Sub-CicrleHover(发送方作为对象,e作为System.EventArgs)
尺寸o作为线型=发送器
尺寸n为字符串=o.名称
作为整数的Dim s=Asc(Mid(n,5,1))-65
对于x=0到3
如果x1那么
标签(s,x)。背景色=颜色。黑色
Public Colors() As Color
Public Data(), Degree(359) As Double
Public EArc(), CArc(), Parts(), PieSize(), Radius, SArc() As Integer
Public Labels(0, 3) As Label
Public Center, ECenter() As Point
Public sc As New ShapeContainer
Private GLine(360) As LineShape
Public LabelNamess(), TitleName As String
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Radius = 246
Center.X = 300
Center.Y = 300
Dim count As Integer = -1
For x = 270 To 359
count = count + 1
Degree(count) = 0.01745 * (x)
Next
For x = 0 To 269
count = count + 1
Degree(count) = 0.01745 * (x)
Next
Dim c(12) As Color
c(0) = Color.Black
c(1) = Color.Red
c(2) = Color.DarkSlateBlue
c(3) = Color.Yellow
c(4) = Color.Green
c(5) = Color.Blue
c(6) = Color.Purple
c(7) = Color.Navy
c(8) = Color.Brown
c(9) = Color.Cyan
c(10) = Color.DarkGreen
c(11) = Color.DarkRed
c(12) = Color.HotPink
Colors = c
Dim p(12) As Double
p(0) = 5696
p(1) = 5495.99
p(2) = 6016.99
p(3) = 5168.99
p(4) = 5421.99
p(5) = 6030.99
p(6) = 4810.99
p(7) = 5199.99
p(8) = 4716.0
p(9) = 4095.95
p(10) = 3299.99
p(11) = 4562.19
p(12) = 5267.38
Data = p
Dim s(12) As String
s(0) = "January 2014"
s(1) = "Febuary 2014"
s(2) = "March 2014"
s(3) = "April 2014"
s(4) = "May 2014"
s(5) = "June 2014"
s(6) = "July 2014"
s(7) = "August 2014"
s(8) = "September 2014"
s(9) = "October 2014"
s(10) = "November 2014"
s(11) = "December 2014"
s(12) = "January 2015"
LabelNamess = s
TitleName = "Monthly Graph for 2014"
CreatePieGraph()
End Sub
Public Sub CreatePieGraph()
Title.Text = TitleName
Title.Left = 639 - Int(Title.Width / 2)
DrawGraph()
PieLabels()
End Sub
Public Sub PieLabels()
ReDim Labels(Data.Length - 1, 3)
Dim per As Double
For x = 0 To LabelNamess.GetLength(0) - 1
Dim l As New Label
l.Left = 20
l.Top = 50 + (40 * x)
l.Width = 240
l.Height = 24
l.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
l.Text = LabelNamess(x)
l.Name = "L0" & x
Labels(x, 0) = l
Panel2.Controls.Add(l)
Dim l1 As New Label
l1.Left = 275
l1.Top = 50 + (40 * x)
l1.Width = 20
l1.Height = 24
l1.BackColor = Colors(x)
l1.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
l1.Text = ""
l1.Name = "L1" & x
Labels(x, 1) = l1
Panel2.Controls.Add(l1)
Dim l2 As New Label
l2.Left = 315
l2.Top = 50 + (40 * x)
l2.Width = 90
l2.Height = 24
l2.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
per = (PieSize(x) / 360) * 100
l2.Text = Format(per, "###.###") & "%"
l2.Name = "L2" & x
Labels(x, 2) = l2
Panel2.Controls.Add(l2)
Dim l3 As New Label
l3.Left = 425
l3.Top = 50 + (40 * x)
l3.Width = 140
l3.Height = 24
l3.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
per = (PieSize(x) / 252) * 100
l3.Text = Format(Data(x), "$#,###,###,###.00")
l3.Name = "L3" & x
Labels(x, 3) = l3
Panel2.Controls.Add(l3)
Next
End Sub
Private Function PiceSize(RawData() As Double) As Array
Dim Dsum As Double
Dim ps As Integer = RawData.Length - 1
Dim count, z, size0(ps), size1(ps), size2(ps), size3(ps), max As Integer
Dim sizes(2) As Array
For x = 0 To RawData.GetLength(0) - 1
Dsum = Dsum + RawData(x)
Next
For x = 0 To ps
size0(x) = Int((RawData(x) / Dsum) * 360)
If ((RawData(x) / Dsum) * 360) - size0(x) > 0.49999 Then size0(x) = size0(x) + 1
count = count + size0(x)
Next
z = -1
If count < 360 Then
For x = count + 1 To 360
z = z + 1
If z > sizes.GetLength(0) Then z = 0
size0(z) = size0(z) + 1
count = count + 1
Next
End If
If count > 360 Then
For x = count - 1 To 360 Step -1
z = z + 1
If z > sizes.GetLength(0) Then z = 0
size0(z) = size0(z) - 1
count = count - 1
Next
End If
count = 0
For x = 0 To ps
If x = 0 Then size1(x) = 0
If x = 0 Then size2(x) = size0(x)
If x = 0 Then size3(x) = Int(size0(x) / 2)
If x <> 0 Then size1(x) = count
If x <> 0 Then size2(x) = count + size0(x)
If x <> 0 Then size3(x) = size1(x) + Int(size0(x) / 2)
If x = ps Then size2(x) = size2(x)
count = count + size0(x)
Next
SArc = size1
EArc = size2
CArc = size3
PiceSize = size0
End Function
Private Sub DrawGraph()
Dim x1, y1, z, p As Integer
Dim sizes(Data.GetLength(0) - 1) As Integer
Dim ps As Integer = Data.GetLength(0) - 1
Dim Ecenter1(ps) As Point
PieSize = PiceSize(Data)
z = 1
sc.Height = 600
sc.Width = 600
sc.Top = 74
sc.Left = 608
For num As Double = 0 To 359
x1 = Convert.ToInt32(Radius * Math.Cos(Degree(num)) + Center.X)
y1 = Convert.ToInt32(Radius * Math.Sin(Degree(num)) + Center.Y)
If num = CArc(p) Then
Ecenter1(p).X = Convert.ToInt32(50 * Math.Cos(Degree(num)) + Center.X)
Ecenter1(p).Y = Convert.ToInt32(50 * Math.Sin(Degree(num)) + Center.Y)
End If
Dim Line As New LineShape
Line.X1 = Center.X
Line.Y1 = Center.Y
Line.X2 = x1
Line.Y2 = y1
Line.BorderColor = Colors(p)
Line.BorderWidth = 5
Line.Visible = True
Line.Name = "line" & Chr(p + 65) & z
If z = PieSize(p) Then
p = p + 1
z = 0
End If
z = z + 1
GLine(num) = Line
AddHandler Line.MouseHover, AddressOf CicrleHover
AddHandler Line.MouseLeave, AddressOf CicrleLeave
AddHandler Line.Click, AddressOf ExplodePice
AddHandler Line.DoubleClick, AddressOf ChangeColor
sc.Shapes.Add(Line)
Next
ECenter = Ecenter1
Panel1.Controls.Add(sc)
End Sub
Protected Sub CicrleHover(sender As Object, e As System.EventArgs)
Dim o As LineShape = sender
Dim n As String = o.Name
Dim s As Integer = Asc(Mid(n, 5, 1)) - 65
For x = 0 To 3
If x <> 1 Then
Labels(s, x).BackColor = Color.Black
Labels(s, x).ForeColor = Color.White
End If
Next
End Sub
Protected Sub CicrleLeave(sender As Object, e As System.EventArgs)
Dim o As LineShape = sender
Dim n As String = o.Name
Dim s As Integer = Asc(Mid(n, 5, 1)) - 65
For x = 0 To 3
If x <> 1 Then
Labels(s, x).BackColor = Color.White
Labels(s, x).ForeColor = Color.Black
End If
Next
End Sub
Protected Sub ExplodePice(Sender As Object, e As System.EventArgs)
Dim o As LineShape = Sender
Dim n As String = o.Name
Dim s As Integer = Asc(Mid(n, 5, 1)) - 65
Dim x1, y1 As Integer
Dim cpoint As Point
If o.X1 = Center.X Then cpoint = ECenter(s) Else cpoint = Center
For x = SArc(s) To EArc(s) - 1
x1 = Convert.ToInt32(Radius * Math.Cos(Degree(x)) + cpoint.X)
y1 = Convert.ToInt32(Radius * Math.Sin(Degree(x)) + cpoint.Y)
GLine(x).X2 = x1
GLine(x).Y2 = y1
GLine(x).X1 = cpoint.X
GLine(x).Y1 = cpoint.Y
Next
End Sub
Protected Sub ChangeColor(Sender As Object, e As System.EventArgs)
Dim o As LineShape = Sender
Dim n As String = o.Name
Dim s As Integer = Asc(Mid(n, 5, 1)) - 65
ColorDialog1.ShowDialog()
For x = SArc(s) To EArc(s) - 1
GLine(x).BorderColor = ColorDialog1.Color
Next
Labels(s, 1).BackColor = ColorDialog1.Color
End Sub