Question

I feel like a complete idiot, but after 3 or so hours of searching and experimenting, I cannot figure this out. 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. 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. 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).

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";
Was it helpful?

Solution

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. 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top