2
이 코드는 Windows 폼 프로그램에서 차트를로드하는 데 사용됩니다. 버튼 클릭 이벤트 핸들러에 배치됩니다. 버튼을 처음 클릭하면 차트가 정상적으로 표시됩니다. 하지만 두 번째 클릭하면 오류가 발생합니다 " 'Series1'이라는 이름의 차트 요소를 'SeriesCollection'에서 찾을 수 없습니다." 코드를 참조하십시오. 저는 VB에 익숙하지 않아 차트를 언급 할 수 없으며이를 수정하여 차트를 다시로드 할 수 있습니다. 어떤 조언을 해줘서 고마워.VB NET에서 차트를 다시로드하는 방법
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)
이 전체 클릭 핸들러를 시도해야한다? 차트 개체를 초기화하고 Series1을 처음 추가 한 위치는 어디입니까? 나는 어떤 코드가이 핸들러 외부에서 Chart1을 지우고 그 다음 일련 번호에 대한 초기화 코드가 없다고 말할 것이다. – jbl
예 이것은 완전한 코드이다. 전설을 설정하기위한 부분을 삭제했지만 carefuly를 점검 한 차트를 다루는 다른 코드는 없다. .. – johnyTee
이 줄 : Series1.Name = "오류 유형"은 시리즈 이름을 변경합니다. 클릭 한 후,이 선 Chart1.Series ("Series1")은 Chart1.Series ("오류 유형")이어야합니다 ... ;-) 제 생각에 Chart1.Series (0)는 세리에 하나만 있으면 좋을 것 같습니다 – jbl