Error "A graph element named '0' already exists under 'Seriescollection'"

Asked

Viewed 188 times

0

I’m doing a program to do the image limiarization, and I wanted to show a graph with which grayscale most appear in the image. The first time the program works normal, but the second time it gives this error:

"A graph element named '0' already exists in 'Seriescollection'"

I’ve tried using the Grafico.Series.Clear(); and several other types of code to clean the graph but nothing helped.

This is my current main:

    private Image imagem;
    private Image cinza;
    Series seri;
    Color corlabel;
    Ocorrencia cla = new Ocorrencia();
    bool rad;


    public Form1()
    {
        InitializeComponent();
    }

    private void salvarImagemToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Images|*.bmp;";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string ext = System.IO.Path.GetExtension(sfd.FileName);

                PBS.Image.Save(sfd.FileName);
            }
        }
        catch
        {
            MessageBox.Show("Erro ao salvar a imagem tente novamente");
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void carregarImagemToolStripMenuItem_Click(object sender, EventArgs e)
    {
        corlabel = Color.FromArgb(255, 0, 0);

        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Title = "Selecione um arquivo de imagem";
        dialog.InitialDirectory = "C:\\";
        dialog.Filter = "Imagens(*.bmp; *.jpg; *.gif)|*.bmp; *.jpg; *.gif | Todos os Arquivos(*.*)| *.*";
        dialog.RestoreDirectory = false;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            imagem = Image.FromFile(dialog.FileName);
            PBC.Image = Image.FromFile(dialog.FileName);

            /// Fazendo imagem em tons de cinza 
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        label2.ForeColor = corlabel;
        label2.Text = "Em processo";

        cinza = (Image)imagem.Clone();

        Bitmap img = new Bitmap(cinza);

        Bitmap imgc = cla.Monocromatico(img);

        PBM.Image = imgc;

        this.Refresh();

        List<Ocorrencia> lista = cla.ContarOcorrencias(img);

        /// jogando no grafico

        lista = lista.OrderBy(x => x.Quantidade).ToList();

        Grafico.Legends[0].LegendStyle = LegendStyle.Table;


        int[] seriesArray = new int[lista.Count];

        int[] pointsArray = new int[lista.Count];


        int i = 0;
        foreach (Ocorrencia o in lista)
        {
            seriesArray[i++] = o.Cor;
        }

        i = 0;
        foreach (Ocorrencia o in lista)
        {
            pointsArray[i++] = o.Quantidade;
        }

            Grafico.Series.Clear();

        for (int ib = 0; ib < lista.Count; ib++)
        {
            // ERRO -> linha em que o erro aparece
            seri = Grafico.Series.Add(Convert.ToString(seriesArray[ib])); 
            seri.Points.Add((pointsArray[ib]));
        }

        int limiar = cla.CalcularLimiar(lista);

        LBLimiar.Text = Convert.ToString(limiar);

        if (radioButton1.Checked == true) {
            rad = true;
        } else {
            rad = false;
        }

        Bitmap imgl = cla.Limiarizacao(img, limiar,rad);

        PBS.Image = img;

        corlabel = Color.FromArgb(50, 205, 50);
        label2.ForeColor = corlabel;
        label2.Text = "Concluido";
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
    }
}

}

  • That one SeriesCollection error is the class Chart.SeriesCollection (Excel? )? This class Series is yours? If so, what is her code?

  • @Pedrogaspar I’m not sure, but when I researched to fix this mistake I found people relating the two. This Class Series is from c itself#.

1 answer

1


I was able to identify that you are using the control Chart from the Windows Forms library (documentation), and saw that the method SeriesCollection.Add() that you’re using gets a String with the object name Series that is being added (documentation).

You are traversing the array int[] seriesArray to add the objects Series to the collection, and is using the value of that array to name the object Series being created. Most likely you have the value zero (0) more than once in the array, so it creates the first object Series with name "0", but the second time he finds a zero, he says that the name already exists.

You can use the array index, for example, to name the object Series, this way I believe you will not have problems:

for (int ib = 0; ib < lista.Count; ib++)
{
   seri = Grafico.Series.Add(Convert.ToString(ib)); 
   seri.Points.Add((pointsArray[ib]));
}
  • Unfortunately I can not use this way because each number of 0255 represents a color on the grayscale and when I order the list, the number of the color has to follow how many times it appears. Wouldn’t there be some way to wipe the data off the chart? I took a look at the documentation but couldn’t find a way, and apparently Grafico.Series.Clear();

  • You have to find a way to give unique names to these objects, because repeated names will not be accepted. You use this name later to access the objects of the collection (SeriesCollection)?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.