从DataGridView到图表获取数据 [英] Get data from DataGridView to Chart

查看:178
本文介绍了从DataGridView到图表获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据从单个DataGridView获取到3个不同的图表中,每个列都指向一个图表。
这是我第一次使用图表,因此我对其进行了一些研究,但找不到任何可以帮助我解决此特定问题的东西。

I need to get data from a single DataGridView into 3 distinct Charts, each column to a chart. This is the first time I'm working with charts, so I've researched a bit about them but couldn't find anything that could help me with this specific problem.

以下是我的DataGridView和图表的截图,并带有各自的图例:

Here follows a screenshot of what my DataGridView and my Charts are like, with their respective legends:

我需要的是(至少在理论上)非常简单的事情。在 SANITY图表中,我需要将 Table [0,0] 中的值添加到 sanityChart.Series [0] 作为其 Y值,依此类推。 UNIT和 ISSUES DB也是如此。
任何帮助将不胜感激。

What I need is (at least in theory) a very simple thing. In the "SANITY" chart I need to get the value located at Table[0, 0] into something like sanityChart.Series[0] as its 'Y' value, and so on. Same thing for "UNIT" and "ISSUES DB". Any help will be much appreciated.

推荐答案

从您提出问题的方式和显示的图像中我相信您想要这样的东西:

From the way you asked the question and the image you have shown I believe that you want something like this:

我只使用一个图表,并且添加了一个系列用于每个

I use only one Chart and I have added one Series for each of your Columns.

设置图表后,我添加了一个数据点对于表中每个系列/列的每一行..:

After setting up the chart I have added one data point for each row in the table to each of the series/columns..:

// setting up the datagridview
Table.Rows.Clear();
Table.Columns.Clear();
Table.Columns.Add("state", "");
Table.Columns.Add("sanity", "SANITY");
Table.Columns.Add("unit", "UNIT");
Table.Columns.Add("issuesDb", "ISSUES DB");
// filling in some data
Table.Rows.Add("ALL PASSED", 86, 21, 2);
Table.Rows.Add("FAILED", 7, 0, 1);
Table.Rows.Add("Cancelled", 0, 0, 0);

// Now we can set up the Chart:
List<Color> colors = new List<Color>{Color.Green, Color.Red, Color.Black};

chart1.Series.Clear();

for (int i = 0 ; i < Table.Rows.Count; i++)
{
    Series S = chart1.Series.Add(Table[0, i].Value.ToString());
    S.ChartType = SeriesChartType.Column;
    S.Color = colors[i];
}

// and fill in all the values from the dgv to the chart:
for (int i = 0 ; i < Table.Rows.Count; i++)
{
   for (int j = 1 ; j < Table.Columns.Count; j++)
   {
      int p = chart1.Series[i].Points.AddXY(Table.Columns[j].HeaderText, Table[j, i].Value);
   }
}

请注意,我选择添加 DataPoints AddXY 重载,并且还决定通过添加使我的生活更轻松X值作为字符串。在内部,它们仍然转换为 doubles ,并且所有值都将<< c $ c> 0 !在这里对我们来说不是问题,但重要的是要了解我们没有使用有效数字,因此不能将它们视为数字!优点是无需任何进一步的操作即可将轴标签设置为列标题。

Note that I have chosen to add the DataPoints with the AddXY overload and also decided that I want to make my life a little easier by adding the X-Values as strings. Internally they are still transformed to doubles and will all have a value of 0! Not a problem for us here, but it is important to understand that we didn't use valid numbers and therefore can't treat them as numbers! The advantage is that the axis labels are being set to the column headers without any further work..

这篇关于从DataGridView到图表获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆