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

查看:23
本文介绍了从 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:

我只使用了一个 Chart,并且我为您的每个 Columns 添加了一个 Series.

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);
   }
}

请注意,我已选择将 DataPointsAddXY 重载一起添加,并且还决定通过添加 X 使我的生活更轻松-Values 作为 strings.在内部,它们仍然被转换为 doubles,并且都将具有 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天全站免登陆