我有这个代码在Windows窗体程序中加载图表。它位于按钮单击事件处理程序中。当我单击按钮时,第一次显示的图表确定。但是在第二次点击时它会给我一个错误“在'SeriesCollection'中找不到名为'Series1'的图表元素。”请参阅代码。 我是VB的新手,不提图表,也无法弄清楚如何修复,所以我可以随时点击按钮重新加载图表。非常感谢任何建议。
Dim pp As String = "J:\UCP\ApplicationsProgramming\MainDB.accdb"
Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & pp & ";Persist Security Info=False;"
Dim tblFields As String = "SELECT Type, COUNT([Zone]) as CountZone FROM Faults GROUP BY Type "
Dim conn As New OleDbConnection(strConn)
Dim oCmd As New OleDbCommand(tblFields, conn)
Dim oData As New OleDbDataAdapter(tblFields, conn)
Dim ds As New DataSet
conn.Open()
oData.Fill(ds, "Faults")
conn.Close()
'''''''''''''''''''''''''''''
'~~> SET DATA SOURCE <~~'
'''''''''''''''''''''''''''''
Chart1.DataSource = ds.Tables("Faults")
''''''''''''''''''''''''''''''''
'~~> WORKING WITH CHARTAREA <~~'
''''''''''''''''''''''''''''''''
Dim CArea As ChartArea = Chart1.ChartAreas(0)
CArea.BackColor = Color.White
CArea.ShadowColor = Color.Red
CArea.Area3DStyle.Enable3D = True
'~~> Formatting X Axis
CArea.AxisX.MajorGrid.Enabled = False
CArea.AxisX.LabelStyle.Font = New System.Drawing.Font("Arial", _
10.0F, System.Drawing.FontStyle.Italic)
'~~> Formatting Y Axis
CArea.AxisY.MajorGrid.Enabled = False
CArea.AxisY.LabelStyle.Format = ""
CArea.AxisY.Interval = 0.1
''''''''''''''''''''''''''''
'~~> WORKING WITH TITLE <~~'
''''''''''''''''''''''''''''
'~~> Adding a Title
Dim T As Title = Chart1.Titles.Add("Fault Types")
'~~> Formatting the Title
With T
.ForeColor = Color.Black
.BackColor = Color.White
'~~> Setting Font, Font Size and Bold/Italicizing
.Font = New System.Drawing.Font("Arial", 11.0F, System.Drawing.FontStyle.Bold)
.BorderColor = Color.Black
.BorderDashStyle = ChartDashStyle.NotSet
End With
'''''''''''''''''''''''''''''
'~~> WORKING WITH SERIES <~~'
'''''''''''''''''''''''''''''
////// NEXT LINE WILL GIVE FOLLoWING ERROR:
////// "A chart element with the name 'Series1' could not be found in the 'SeriesCollection'." //////////
Dim Series1 As Series = Chart1.Series("Series1") <<<<<<<<<<
'~~> Setting the series Name
Series1.Name = "Fault Types"
'~~> Assigning values to X and Y Axis
Chart1.Series(Series1.Name).XValueMember = "Type"
Chart1.Series(Series1.Name).YValueMembers = "CountZone"
'~~> Setting Font, Font Size and Bold
Chart1.Series(Series1.Name).Font = New System.Drawing.Font("Arial", 10.0F, System.Drawing.FontStyle.Bold)
'~~> Setting Value Type
Chart1.Series(Series1.Name).YValueType = ChartValueType.Double
'~~> Setting the Chart Type for Display
'Chart1.Series(Series1.Name).ChartType = SeriesChartType.Radar
Chart1.Series(Series1.Name).ChartType = SeriesChartType.Pie
'~~> Display Data Labels
Chart1.Series(Series1.Name).IsValueShownAsLabel = True
'~~> Setting label's Fore Color
Chart1.Series(Series1.Name).LabelForeColor = Color.FloralWhite
'~~> Setting label's Format to %age
Chart1.Series(Series1.Name).LabelFormat = "" '"0.0%"
'~~> Setting the location for the chart
Chart1.Size = New System.Drawing.Size(865, 350)
答案 0 :(得分:4)
第Series1.Name = "Fault Types"
行更改了系列的名称。
因此,点击后,对Dim Series1 As Series = Chart1.Series("Series1")
的下一次调用将失败。
如果您只使用一个系列,则应尝试Dim Series1 As Series = Chart1.Series(0)