Hi @José Carlos ,
If you define s
and s1
as global variables, then every time the code snippet containing adding data points is called, data points are repeatedly added to these global variables and added to the chart again. This will result in an increased amount of data in the chart.
To avoid this, you can define s and s1 as local variables, so that each time the snippet is called new local variables are created and data points are added to these local variables. This ensures that each call will use a new series object and data points, without adding data repeatedly.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton4.Checked Then
Chart1.ChartAreas(0).Area3DStyle.Enable3D = True
Else
Chart1.ChartAreas(0).Area3DStyle.Enable3D = False
End If
Chart1.Series.Clear()
Dim s As New Series()
s.Name = "Vendas"
With s
.IsValueShownAsLabel = True
.BorderWidth = 4
.Color = Color.DarkGreen
.LegendText = "actual sales"
.Points.AddXY("Janeiro", 150)
.Points.AddXY("Fevereiro", 140)
.Points.AddXY("Março", 110)
.Points.AddXY("Abril", 125)
.Points.AddXY("Maio", 140)
.Points.AddXY("Junho", 155)
End With
Chart1.Series.Add(s)
Dim s1 As New Series()
s1.Name = "previsao"
With s1
.BorderWidth = 4
.Color = Color.FromArgb(255, 155, 0) ' amarelo
.LegendText = "Sales forecast"
.IsValueShownAsLabel = True
.Points.AddXY("Janeiro", 130)
.Points.AddXY("Fevereiro", 145)
.Points.AddXY("Março", 100)
.Points.AddXY("Abril", 120)
.Points.AddXY("Maio", 120)
.Points.AddXY("Junho", 140)
End With
Chart1.Series.Add(s1)
End Sub
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.