簡體   English   中英

C#使用DataTable的行在圖表中進行序列化

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

我覺得自己是個白痴,但是經過3個多小時的搜索和試驗,我還是無法弄清這一點。 我有一個數據表,其中包含地址列,選擇列,然后有30列標記為1到30的列。我想使用30列制作一個圖表( System.Windows.Forms.DataVisualization.Charting.Chart ),該圖表每行具有一個系列。對於每個系列的值。 “地址”列將充當系列標簽。 之所以選擇列,是因為在我弄清楚如何使數據完全顯示之后,最終進行檢查將使該行顯示在圖表中。

在設計器中,我能夠使用COLUMN來獲取一系列數據,但無法弄清楚如何對ROW進行同樣的處理。 那么我該怎么做呢? 我認為我可能會缺少一些基本知識,因此使用深度解釋圖表的指針也將是一個好方法(我所能找到的都是非常小的示例,其中大多數對x和y使用兩列)。

編輯:關於它的價值(我認為這里真的沒有任何幫助),這是設計師創建的相關代碼:

        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";

我無法弄清楚如何使用DataTable進行此操作,因為我發現的所有內容似乎都假設您要對系列使用列,這對我完全沒有意義。 所以我最終做了困難的事情。 每次我向DataTable添加一行時,我也會創建一個值數組。 然后添加如下系列:

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

我不得不使用循環來添加點,這讓我非常惱火,但是我找不到一個允許我添加項目數組的方法-似乎應該在那里。 AddY方法指示它采用數組,但在運行時崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM