Help erasing previous serie to refresh chart

Vinhermes

Member
Joined
Aug 15, 2012
Messages
7
Programming Experience
Beginner
Dear Sir,

I have added a serie on a chart that comes on a form.

I would like it to update this chart upon request and the way I thought I would do it, is to erase the previous serie if already present on the chart:

[XCODE]'If there is already a serie then we erase it
If Chart1.Series.Count = 0 Then
Chart1.Series.Add("Pressure drop/loss")
Else
Chart1.Series("Pressure drop/loss").Points.Clear()
End If
[/XCODE]

Unfortunately this does not seem to work.

Could you please tell me how you would do so that it refreshes upon request?

This seems to be a recurrent issue wit VB.net but I could not find any universal answer.

Please find below the integrality of the code dedicated to charting if this can help. Please fell free to ask if you have any questions. Thanks by advance, Vincent

[XCODE]'Creation of a new chart
Dim Chart1 As New Chart
' Creation of a new chart area
Dim chartArea1 As New ChartArea()
' Adding chartarea to the chart
Chart1.ChartAreas.Add(chartArea1)
'If there is already a serie then we erase it
If Chart1.Series.Count = 0 Then
Chart1.Series.Add("Pressure drop/loss")
Else
Chart1.Series("Pressure drop/loss").Points.Clear()
End If
'6 datapoints for the serie "Pressuredropgraph": flowrate vs. head losses for each speed from 0 to 6m/s
Dim p0 As New DataPoint
p0.XValue = FlowrateGraph(0)
p0.YValues = {PressureDropGraph(0)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p0)
Dim p1 As New DataPoint
p1.XValue = FlowrateGraph(1)
p1.YValues = {PressureDropGraph(1)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p1)
Dim p2 As New DataPoint
p2.XValue = FlowrateGraph(2)
p2.YValues = {PressureDropGraph(2)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p2)
Dim p3 As New DataPoint
p3.XValue = FlowrateGraph(3)
p3.YValues = {PressureDropGraph(3)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p3)
Dim p4 As New DataPoint
p4.XValue = FlowrateGraph(4)
p4.YValues = {PressureDropGraph(4)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p4)
Dim p5 As New DataPoint
p5.XValue = FlowrateGraph(5)
p5.YValues = {PressureDropGraph(5)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p5)
Dim p6 As New DataPoint
p6.XValue = FlowrateGraph(6)
p6.YValues = {PressureDropGraph(6)}
Chart1.Series(
"Pressure drop/loss").Points.Add(p6)
'"Pressure drop" is on the chart area1
Chart1.Series("Pressure drop/loss").ChartArea = "ChartArea1"
' Chart area position
Chart1.Location = New System.Drawing.Point(2, 511)
' Chart area size
Chart1.Size = New System.Drawing.Size(475, 200)
'Type of graph
Chart1.Series("Pressure drop/loss").ChartType = SeriesChartType.Spline
'Backcolor of graph
Chart1.BackColor = Color.FromKnownColor(KnownColor.Control)
' Chart added to the form
Me.Controls.Add(Chart1)
[/XCODE]
 
Chart1.Series.Clear()

I think I got it with:

[XCODE]
' Creation of a new chart area
Dim chartArea1 As New ChartArea()


'If there is no chart then add one and a serie
If Chart1.ChartAreas.Count = 0 Then
Chart1.ChartAreas.Add(chartArea1)
Chart1.Series.Add("Pressure drop/loss")
Else
'If there is already a chart then erase it, add it, clear series and add a new serie
Chart1.ChartAreas.Clear()
Chart1.ChartAreas.Add(chartArea1)
Chart1.Series.Clear()
Chart1.Series.Add("Pressure drop/loss")
End If
[/XCODE]

Dunfiddin, you saved me twice in two days but I should be OK now. Thank you very much!
 
Back
Top