简体   繁体   English

WinForm图表问题

[英]WinForm chart issue

I'm trying to create a chart based on a dataTable . 我正在尝试基于dataTable创建一个chart The table contains only two columns, names and count: 该表仅包含两列,名称和计数:

+-------+-------+
| names | count |
+-------+-------+
| name1 | 650   |
+-------+-------+
| name2 | 0     |
+-------+-------+
| name3 | 211   |
+-------+-------+
| name4 | 50    |
+-------+-------+

I want the Y axis to be the count . 我希望Y axis成为count I want a column to be generated for each name and labelled with the actual names. 我希望为每个name生成一列,并用实际名称标记。

This is what I tried: 这是我尝试的:

int max = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
    if (Convert.ToInt32(table.Rows[i][1]) > max) max = Convert.ToInt32(max.Rows[i][1]);
}
chart1.ChartAreas.Add("area");
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = max;
chart1.ChartAreas[0].AxisY.Interval = max/ 10;

for (int i = 0; i < table.Rows.Count; i++)
{
    chart1.Series.Add(table.Rows[i][0].ToString());
}
for (int i = 0; i < table.Rows.Count; i++)
{
    chart1.Series[i].Points.AddXY(table.Rows[i][0].ToString(), Convert.ToInt32(table.Rows[i][1]));
}

And this is what I get, which is not quite what I expected: 这就是我得到的,与我期望的不完全相同: 在此处输入图片说明

Change this: 更改此:

for (int i = 0; i < table.Rows.Count; i++)
{
    chart1.Series.Add(table.Rows[i][0].ToString());
}
for (int i = 0; i < table.Rows.Count; i++)
{
    chart1.Series[i].Points.AddXY(table.Rows[i][0].ToString(),  
                                  Convert.ToInt32(table.Rows[i][1]));
}

to this: 对此:

Series S = chart1.Series.Add("names");

for (int i = 0; i < table.Rows.Count; i++)
{
    S.Points.AddXY(table.Rows[i][0].ToString(), Convert.ToInt32(table.Rows[i][1]));
}

Your original code added one series for each row and one datapoint to each series. 您的原始代码为每一行添加了一个系列,并为每个系列添加了一个数据点。 This made the chart place them all beside each other and display their sole point, each with the unique series color.. 这使得图表将它们全部并排放置并显示其唯一点,每个点都具有唯一的系列颜色。

The new code creates only one series and adds all datapoints to its Points collection. 新代码仅创建一个系列,并将所有数据点添加到其Points集合中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM