Pasar datos de un Datagridview a Lightningchart

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

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

Pasar datos de un Datagridview a Lightningchart

Post by CireXD » Mon Aug 26, 2019 4:51 pm

Como puedo pasar los datos de un DataGridView a un gráfico de LightningChart? no se como pasar los datos de la tabla al gráfico, en vb.net, agradecería si me dan un ejemplo.

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

Re: Pasar datos de un Datagridview a Lightningchart

Post by Arction_LasseP » Tue Aug 27, 2019 8:32 am

Hello,

Apologies for not being able to speak/write Spanish.

This should be possible by just reading the values of DataGridView cells in a for-loop for example, and assigning them as SeriesPoint X and Y values. Currently we do not have an example about this in VisualBasic .Net. However, I can attach a small example here, done in WinForms, which should give you the idea how this can be done.

Code: Select all

_chart = new LightningChartUltimate();

_chart.BeginUpdate();

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

FreeformPointLineSeries pls = new FreeformPointLineSeries(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
pls.LineStyle.Color = Color.LimeGreen;
pls.PointsVisible = true;
_chart.ViewXY.FreeformPointLineSeries.Add(pls);

_chart.EndUpdate();


private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_chart != null)
            {
                List<SeriesPoint> points = new List<SeriesPoint>();
                double d1, d2;

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells[0].Value != null && dataGridView1.Rows[i].Cells[1].Value != null)
                    {
                        if (double.TryParse(dataGridView1.Rows[i].Cells[0].Value.ToString(), out d1) && double.TryParse(dataGridView1.Rows[i].Cells[1].Value.ToString(), out d2))
                        {
                            SeriesPoint pnt = new SeriesPoint();
                            pnt.X = d1;
                            pnt.Y = d2;
                            points.Add(pnt);
                        }
                    }
                }
                _chart.ViewXY.FreeformPointLineSeries[0].Points = points.ToArray();
                _chart.ViewXY.FreeformPointLineSeries[0].InvalidateData();
            }
        }
In this example, we create a FreeformPointLineSeries, which gets its data values from a DataGridView (each row has two columns for X- and Y-values). Every time user changes/adds a value in the grid, values are updated inside CellValueChanged-event. Basically all this requires is some null checks and value parsing. Note that creating a new data point list every time is not the most efficient way to do this, could be better to just change the X-/Y-values of the data point corresponding to the cell whose values were changed.

Hope this is helpful.
Best regards,
Lasse

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

Re: Pasar datos de un Datagridview a Lightningchart

Post by CireXD » Wed Aug 28, 2019 7:21 pm

Y en el caso del Gráfico 3D ¿Cómo seria ?

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

Re: Pasar datos de un Datagridview a Lightningchart

Post by Arction_LasseP » Thu Aug 29, 2019 6:47 am

Hello,

The exact same logic works with 3D graphs as well. The only differences are that you are using PointLineSeries3D instead of FreeformPointLineseries, and SeriesPoint3D instead of SeriesPoint when assigning new data points. This means you have to assign three values to a data point (X, Y, Z). Therefore there most likely should be a third column in the DataGridView.

Best regards,
Lasse

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

Re: Pasar datos de un Datagridview a Lightningchart

Post by CireXD » Wed Sep 04, 2019 7:06 pm

¿Existe algún requisito previo de parte del control datagridview para que le gráfico funcione? no he podido crear el gráfico 3D :'(, quizás sea la forma de llenar el datagrid, o tal vez el origen de los datos, como se ingresan los datos, etc

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

Re: Pasar datos de un Datagridview a Lightningchart

Post by Arction_LasseP » Thu Sep 05, 2019 7:27 am

Hello,

It is difficult to say what exactly is the reason why you couldn't create a 3D chart. It is possible that you are lacking some property setting such as setting ActiveView to View3D. Here is the same WinForms test project, now converted to use a 3D-chart.

Code: Select all

_chart = new LightningChartUltimate();

_chart.BeginUpdate();

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

PointLineSeries3D pls = new PointLineSeries3D(_chart.View3D, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary);
pls.LineStyle.Color = Color.Yellow;
pls.PointsVisible = true;
pls.PointStyle.Shape3D = PointShape3D.Sphere;
pls.Material.DiffuseColor = Color.Red;
_chart.View3D.PointLineSeries3D.Add(pls);

_chart.EndUpdate();


private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_chart != null)
            {
                List<SeriesPoint3D> points = new List<SeriesPoint3D>();
                double d1, d2, d3;

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells[0].Value != null && dataGridView1.Rows[i].Cells[1].Value != null && dataGridView1.Rows[i].Cells[2].Value != null)
                    {
                        if (double.TryParse(dataGridView1.Rows[i].Cells[0].Value.ToString(), out d1) && double.TryParse(dataGridView1.Rows[i].Cells[1].Value.ToString(), out d2) 
                            && double.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out d3))
                        {
                            SeriesPoint3D pnt = new SeriesPoint3D();
                            pnt.X = d1;
                            pnt.Y = d2;
                            pnt.Z = d3;
                            points.Add(pnt);
                        }
                    }
                }
                _chart.View3D.PointLineSeries3D[0].Points = points.ToArray();
                _chart.View3D.PointLineSeries3D[0].InvalidateData();
            }
        }
About reading/filling data. LightningChart is primarily a visualization component and does not itself have an interface to connect to a database such as MySQL or Oracle. Technically, LightningChart does not care how data is supplied to chart, but in general it has limited functionality to read from some specific sources. Our Demo Application has “Large data open speed test” -example, which shows how save and read our file format. However, there are unlimited possibilities for ‘database’ and we cannot cover them all in our library. Therefore, user should develop own routines to read data from whatever source they have in order to add data to a chart. The data source or the method to read data from it should not affect the chart itself at all.

Hope this helps.
Best regards,
Lasse

Post Reply