0

I have trouble understanding how to bind my data to a Livecharts Pie Chart.

I would like to display my datagridview in a pie chart and update it everytime I change the values of my cells in my datagridview.

I save my datagridviewdata in an ObservableCollection and want my pie chart to adjust. Right now, all my chart does is drawing a new one with different colors each time I change values.

The closest problem to mine I could find was this one, How do I correctly update my chart values? (In real time), which unfortunately does not answer my question.

I expand my ObservableCollection everytime I expand my List like this:

private void btnAdd_Click(object sender, EventArgs e)   // Zeile Hinzufügen Button
        {
            dataGridViewAuswahl.Rows.Add();
            
            myData neueZeile = new myData();
            Global.ObservableData.Add(myData);

            UpdateElementsList();
        }

My ObservableCollection gets updated by the CellValueChanged event:

private void DataGridViewAuswahl_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                var currentRow = Global.ObservableData[e.RowIndex];

                switch (e.ColumnIndex)
                {
                    case 0:
                        currentRow.Element = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        break;

                    case 1:
                        currentRow.Value = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                        break;

                    default:
                        break;
                }
             UpdateElementsList();
            }
        }

Now I think my problem lies in my updating Method, which is probably not binding my data to the pie chart correctly. This is where I have trouble understanding how to bind it properly.

private void UpdateElementsList()
        {
            Global.seriesList.Clear();
            foreach (var data in Global.ObservableData)
            {
                var series = new PieSeries<int>
                {
                    Values = new ChartValues<int> { data.Value }, 
                    Name = daata.Element,
                    InnerRadius = 25,
                    Stroke = null
                };
                Global.seriesList.Add(series);
            }
            pieChart1.Series = Global.seriesList;
        }

For context, mydata is this:

public class mydata
    {
        public string Element { get; set; }
        public int Value { get; set; }
    }

0 Answers0