3d graph from datagridview data

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

Post Reply
CireXD
Posts: 5
Joined: Mon Aug 26, 2019 4:41 pm

3d graph from datagridview data

Post by CireXD » Sun Oct 27, 2019 4:15 am

good morning, along with greeting you, I would like to ask you for help to make a 3D graphic from a cloud of points of 72 columns and 11 rows (must have this format), forming 24 curves, that is, the first 3 columns form a curve, The next 3 columns form another curve and so on. Unfortunately I have only been able to graph a single curve and it does not show me the other 23, could you help me with this problem? What could I do to show all the curves? (the data is entered in a datagridview)

Arction_LasseP
Posts: 71
Joined: Wed Mar 27, 2019 1:05 pm

Re: 3d graph from datagridview data

Post by Arction_LasseP » Mon Oct 28, 2019 10:24 am

Hello,

If you want to have 24 different curves, you need to create 24 separate PointLineSeries3D objects in code behind and add each one to the chart's PointLineSeries3D collection. Here is a small example, in which these curves as well as data points for them are generated in code behind. The CellValueChanged -event shows how you can determine which cell value belongs to which PointLineSeries3D, when you have DatagridView like the one you described (72 columns, 11 rows).

Code: Select all

            _chart = new LightningChartUltimate();

            _chart.BeginUpdate();

            _chart.Parent = splitContainer1.Panel2;
            _chart.Dock = DockStyle.Fill;
            _chart.ActiveView = ActiveView.View3D;

            _chart.View3D.LegendBox.Visible = false;

            dataGridView1.Rows.Add(11);
            Random rnd = new Random();

            for (int i = 0; i < 24; i++)
            {
                PointLineSeries3D pls = new PointLineSeries3D(_chart.View3D, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary);
                pls.LineStyle.Color = Color.FromArgb(255, 255 - i * 10, i * 10, 10);
                pls.LineStyle.Width = 0.6f;
                pls.PointsVisible = false;

                SeriesPoint3D[] pnt = new SeriesPoint3D[11];
                for (int j = 0; j < 11; j++)
                {
                    pnt[j].X = 5 + j * 9;
                    pnt[j].Y = rnd.Next(5, 96);
                    pnt[j].Z = 1 + i * 4;

                    dataGridView1.Rows[j].Cells[i * 3].Value = pnt[j].X;
                    dataGridView1.Rows[j].Cells[i * 3 + 1].Value = pnt[j].Y;
                    dataGridView1.Rows[j].Cells[i * 3 + 2].Value = pnt[j].Z;
                }
                pls.Points = pnt;
                _chart.View3D.PointLineSeries3D.Add(pls);
            }
            _chart.EndUpdate();
            
            
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_chart != null)
            {
                double value;
                if (double.TryParse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out value) && _chart.View3D.PointLineSeries3D.Count == 24)
                {
                    if (e.ColumnIndex % 3 == 0)
                        _chart.View3D.PointLineSeries3D[(int)Math.Floor(e.ColumnIndex / 3.0)].Points[e.RowIndex].X = value;
                    else if (e.ColumnIndex % 3 == 1)
                        _chart.View3D.PointLineSeries3D[(int)Math.Floor(e.ColumnIndex / 3.0)].Points[e.RowIndex].Y = value;
                    else if (e.ColumnIndex % 3 == 2)
                        _chart.View3D.PointLineSeries3D[(int)Math.Floor(e.ColumnIndex / 3.0)].Points[e.RowIndex].Z = value;

                    _chart.View3D.PointLineSeries3D[(int)Math.Floor(e.ColumnIndex / 3.0)].InvalidateData();
                }
            }
        }
If the data values are entered in the DataGridView instead of code behind, and you haven't generated any data points, the points have to be created inside the event. You can use logic similar to the one mentioned in this earlier thread.

viewtopic.php?f=15&t=2291

Alternatively you can create the data points in code behind and set each data point value to zero in which case the event shown above could be enough.

Hope this helps.
Best regards,
Lasse

CireXD
Posts: 5
Joined: Mon Aug 26, 2019 4:41 pm

Re: 3d graph from datagridview data

Post by CireXD » Wed Oct 30, 2019 8:37 pm

Good afternoon, the state using a code that allows me to paste data to the datagridview with the same format mentioned above (72 columns and 11 rows) but gives me an error on the lines where the points are stored, System.IndexOutOfRangeException: 'Index out of matrix boundaries'.
I use the following code to paste:

Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

If e.Control AndAlso e.KeyCode = Keys.V Then
Try
For Each line As String In Clipboard.GetText.Split(vbNewLine)
Dim item() As String = line.Trim.Split(vbTab)
If item.Length = Me.DataGridView1.ColumnCount Then
Me.DataGridView1.Rows.Add(item)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub

at the moment that I paste the data that error is generated, could you help me solve it please :)

Arction_LasseP
Posts: 71
Joined: Wed Mar 27, 2019 1:05 pm

Re: 3d graph from datagridview data

Post by Arction_LasseP » Thu Oct 31, 2019 10:17 am

Hello,

IndexOutOfRangeException indicates the array/matrix sizes do not match. In other words, data or object is being inserted to array index that does not exist. Where exactly this happens is not clear. Therefore we suggest you check the sizes of the arrays you use during pasting the data values, for example if there are enough columns/rows in your DataGridView before adding data to it, though your code seems to handle the column count correctly.

Best regards,
Lasse

Post Reply