none
How to prevent from this message in chart preview: "A chart element with the name ' ' already exists in the series collection"? RRS feed

  • Question

  • I have the X-axis and also legends in my chart which are linked whit text-boxes. every text-box is under randomize value and name means don't have special or static name of number it is depend to user inserted data. Some time when there is same name or numbers in two or more then tow text-boxes after debugging VB form the chart show message (A chart element with the name ' ' already exists in the series collection) the ting is i have to include those text-boxes which have same name, means i cannot ignore them because every text-box data is impotent. So please how can i prevent this message by using code. thanks 
    Monday, October 28, 2019 5:17 PM

All replies

  • I have a chart in my windows form which X-axis linked whit text box, containing names of elements and Y-axis linked whit text-boxes, containing numeric values.
    when I debug the form columns (bars) are close together and from 5 columns just one column takes a name.
    Also when the same names come in text-boxes the program give message " A chart element with the name "......." already exists in the SeriseCollection". how to solve this issue?


    I have tried the following code:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Me.Chart1.Series.Clear()
                Me.Chart1.Series.Add(Form3.TextBox6.Text)
                Me.Chart1.Series.Add(Form3.TextBox17.Text)
                Me.Chart1.Series.Add(Form3.TextBox23.Text)
                Me.Chart1.Series.Add(Form3.TextBox29.Text)
                Me.Chart1.Series.Add(Form3.TextBox35.Text)

                Me.Chart1.Series(Form3.TextBox6.Text).Points.AddXY(Form3.TextBox6.Text, Val(Form3.txtpi1.Text))
                Me.Chart1.Series(Form3.TextBox17.Text).Points.AddXY(Form3.TextBox17.Text, Val(Form3.txtpi2.Text))
                Me.Chart1.Series(Form3.TextBox23.Text).Points.AddXY(Form3.TextBox23.Text, Val(Form3.txtpi3.Text))
                Me.Chart1.Series(Form3.TextBox29.Text).Points.AddXY(Form3.TextBox29.Text, Val(Form3.txtpi4.Text))
                Me.Chart1.Series(Form3.TextBox35.Text).Points.AddXY(Form3.TextBox35.Text, Val(Form3.txtpi5.Text))

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                Beep()
            End Try
        End Sub

    Monday, October 28, 2019 6:28 AM
  • For the chart you describe you need to make just one series and add it to the chart. In the example one series named Scores is used.

    Then the  (x,y) points are added to the Score series to create one data series with names and values. In the example two lists were made one list with names and one list with the values. The lists represent your textboxes.

    If that is not the result you want then show a picture of exactly what you want.

    But first get this samples application. It is a c# program you run in visual studio. It has lots of pictures and is the best docs for the chart.

    https://code.msdn.microsoft.com/Samples-Environments-for-b01e9c61

    Here is a working example. Note how I used the Inset Code Block button at the top of the forum post editor to insert the code into a nice formatted code block.

    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Sample Chart"
            Chart1.Dock = DockStyle.Fill
    
            'setup the chart
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Name"
                .AxisY.Title = "Score"
            End With
    
            Chart1.Series.Clear()
            Chart1.Series.Add("Scores")
    
            With Chart1.Series("Scores")
                .IsVisibleInLegend = False
                .ChartType = DataVisualization.Charting.SeriesChartType.Column
    
                Dim yvals As List(Of Integer) = New List(Of Integer)({6, 8, 3, 5, 7})
                Dim xvals As String() = {"Peter", "Andrew", "Julie", "Mary", "Dave"}
    
                .Points.DataBindXY(xvals, yvals)
    
            End With
        End Sub
    End Class





    Monday, October 28, 2019 11:36 AM
  • please can tell me how can add the legends also the color to every column and show them in legends?

    Monday, October 28, 2019 5:08 PM
  • Hi,

    With tommytwotrain's code, you can add legends and add color to column as following code:

    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form2
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Sample Chart"
            Chart1.Dock = DockStyle.Fill
    
            'setup the chart
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Name"
                .AxisY.Title = "Score"
            End With
    
            Chart1.Series.Clear()
            Chart1.Series.Add("Scores")
    
            With Chart1.Series("Scores")
                .IsVisibleInLegend = True 'Set Legend visible
    
                .ChartType = SeriesChartType.Column
                Dim yvals As List(Of Integer) = New List(Of Integer)({6, 8, 3, 5, 7})
                Dim xvals As String() = {"Peter", "Andrew", "Julie", "Mary", "Dave"}
    
                .Points.DataBindXY(xvals, yvals)
    
            End With
    
            'Mark column by circle with different color
            Me.Chart1.Series(0).MarkerStyle = MarkerStyle.Circle
            Me.Chart1.Series(0).MarkerSize = 2
            For Each point As DataPoint In Me.Chart1.Series(0).Points
    
                If Convert.ToInt16(point.YValues(0)) > 7 Then
                    point.MarkerColor = Color.Green
                ElseIf Convert.ToInt16(point.YValues(0)) >= 6 Then
                    point.MarkerColor = Color.Yellow
                Else
                    point.MarkerColor = Color.Red
                End If
            Next
    
            'Change column's color
            For Each point As DataPoint In Me.Chart1.Series(0).Points
    
                If Convert.ToInt16(point.YValues(0)) > 7 Then
                    point.Color = Color.Green
                ElseIf Convert.ToInt16(point.YValues(0)) >= 6 Then
                    point.Color = Color.Yellow
                Else
                    point.Color = Color.Red
                End If
            Next
    
            'Implement the 3D effect of the chart
            Chart1.ChartAreas(0).Area3DStyle.Enable3D = True
    
            'Add Label
            Chart1.Series(0).IsValueShownAsLabel = True
        End Sub
    End Class

    As to your second question, someone has encountered the same situation, you can refer to his solution.

    Hope it help to you.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 29, 2019 7:00 AM
    Moderator
  • THANKS A LOT!

    Amranullah

    Tuesday, October 29, 2019 12:19 PM
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 30, 2019 5:33 AM
    Moderator