JanT
JanT

Reputation: 2096

How to reload Chart in VB NET

I have this code to load chart in Windows form program. It is placed in button click event handler. When I click the button first time chart is displayed OK. But on second click it will give me error "A chart element with the name 'Series1' could not be found in the 'SeriesCollection'." please see in code. I'm new to VB not to mention charts and cannot figure out how to fix that so I can click the button any time(s) to reload the chart. Thanks a lot for any advice.

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)

Upvotes: 2

Views: 3870

Answers (1)

jbl
jbl

Reputation: 15413

The line Series1.Name = "Fault Types" changes the serie's name.

So, upon click, the next call to Dim Series1 As Series = Chart1.Series("Series1") will fail.

If you use only one serie, you should try Dim Series1 As Series = Chart1.Series(0)

Upvotes: 4

Related Questions