Graph doubling, tripling the series. VB .NET

José Carlos 886 Reputation points
2023-07-09T20:35:17.28+00:00

I'm having a hard time solving this problem. After many attempts, I couldn't solve it.

I have a form that has a Chart and two radiobuttons and a Button. In Radiobutton I have 2D and 3D options. When I select for example 3D the graph is being drawn. If I then choose the 2D option, the graph is drawn in 2D, but the data is being duplicated, tripled and so on each time I ask to draw the graph. I used the Chart1.Series.Clear() command but it is not working. Follow the code. The graph has two series. At the beginning of the program I define the two series:

        Public s As New Series ' column
        Public s1 As New Series 'bar

        If RadioButton4.Checked Then             
			Chart1.ChartAreas(0).Area3DStyle.Enable3D = True         
		Else             
			Chart1.ChartAreas(0).Area3DStyle.Enable3D = False         
		End If

        Chart1.Series.Clear()
        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) 'add the series to the chart

        
        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) 'add the series to the chart
        ```



Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-07-10T07:47:25.6333333+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.