2

我有这段代码可以在 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)
4

1 回答 1

4

该行Series1.Name = "Fault Types"更改了系列的名称。

因此,单击后,下一次调用Dim Series1 As Series = Chart1.Series("Series1")将失败。

如果您只使用一个系列,您应该尝试Dim Series1 As Series = Chart1.Series(0)

于 2012-11-23T20:54:29.003 回答