简体   繁体   English

C#使用DataTable的行在图表中进行序列化

[英]C# Using rows of DataTable for series in Chart

I feel like a complete idiot, but after 3 or so hours of searching and experimenting, I cannot figure this out. 我觉得自己是个白痴,但是经过3个多小时的搜索和试验,我还是无法弄清这一点。 I have a DataTable with with columns Address, Select, and then 30 columns labeled 1 through 30. I want to make a chart ( System.Windows.Forms.DataVisualization.Charting.Chart ) that has one series per row, using the 30 columns for the values of each series. 我有一个数据表,其中包含地址列,选择列,然后有30列标记为1到30的列。我想使用30列制作一个图表( System.Windows.Forms.DataVisualization.Charting.Chart ),该图表每行具有一个系列。对于每个系列的值。 The Address column will act as the series label. “地址”列将充当系列标签。 The Select column is there because eventually checking this will make that row show up in the chart after I figure out how to even get the data to show up at all. 之所以选择列,是因为在我弄清楚如何使数据完全显示之后,最终进行检查将使该行显示在图表中。

In the designer I am able to use a COLUMN to get a series of data, but cannot figure out how to do the same with a ROW. 在设计器中,我能够使用COLUMN来获取一系列数据,但无法弄清楚如何对ROW进行同样的处理。 So how do I do this? 那么我该怎么做呢? I think I might be missing something basic, so a pointer to an in depth explanation of using the chart would be good, too (All I can find are very small examples, most of which use two columns for x and y). 我认为我可能会缺少一些基本知识,因此使用深度解释图表的指针也将是一个好方法(我所能找到的都是非常小的示例,其中大多数对x和y使用两列)。

Edit: For what it's worth (and I don't think there is really anything helpful here), here is the relevant code the designer created: 编辑:关于它的价值(我认为这里真的没有任何帮助),这是设计师创建的相关代码:

        chartArea1.Name = "ChartArea1";
        this.oneSecondChart.ChartAreas.Add(chartArea1);
        this.oneSecondChart.DataSource = this.oneSecondDataTableBindingSource;
        this.oneSecondChart.Dock = System.Windows.Forms.DockStyle.Fill;
        legend1.Name = "Legend1";
        this.oneSecondChart.Legends.Add(legend1);
        this.oneSecondChart.Location = new System.Drawing.Point(0, 0);
        this.oneSecondChart.Name = "oneSecondChart";
        this.oneSecondChart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.EarthTones;
        series1.ChartArea = "ChartArea1";
        series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        series1.IsXValueIndexed = true;
        series1.Legend = "Legend1";
        series1.Name = "Series1";
        series1.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
        series1.YValueMembers = "1";
        this.oneSecondChart.Series.Add(series1);
        this.oneSecondChart.Size = new System.Drawing.Size(585, 509);
        this.oneSecondChart.TabIndex = 0;
        this.oneSecondChart.Text = "chart1";

I could not figure out how to do this with a DataTable since everything I found seems to be assuming you want to use columns for series, which makes no sense to me at all. 我无法弄清楚如何使用DataTable进行此操作,因为我发现的所有内容似乎都假设您要对系列使用列,这对我完全没有意义。 So I ended up doing it the hard way. 所以我最终做了困难的事情。 Every time I add a row to my DataTable I also created an array of values. 每次我向DataTable添加一行时,我也会创建一个值数组。 I then add a series as follows: 然后添加如下系列:

        Series s = oneSecondChart.Series.Add(name); 
        s.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        for (int i = 0; i < data.Length; i++) {
            oneSecondChart.Series[name].Points.AddY(data[i]);
        }

I am quite irritated by having to use a loop to add the points, but I could not find a method that allows me to add an array of items--something that seems like it SHOULD be there. 我不得不使用循环来添加点,这让我非常恼火,但是我找不到一个允许我添加项目数组的方法-似乎应该在那里。 The AddY method indicates that it takes an array, but it crashes at runtime. AddY方法指示它采用数组,但在运行时崩溃。

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

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