2016-01-25 50 views
0

所以基本上我想有一堆圖表的循環創建圖表的多個實例(會有相當多的人)通過的迴路形成相同的結構。是否有一種方法可以基於它們的名稱(A1,A2,A3,A4,B1,B2,B3 ...)的數組來生成它們的全部(也包括圖表區域和系列名稱),還可以更改每個區域的位置新的一個?通過基於一個名字陣列

我真的很新的C#和將不勝感激任何幫助。希望有一個解決方案。謝謝!

單個圖表的代碼看起來像這樣(有一些行,但我將只顯示結構):

public Main() 
{ 
    System.Windows.Forms.DataVisualization.Charting.ChartArea A1_area = 
    new System.Windows.Forms.DataVisualization.Charting.ChartArea(); 

    this.A1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); 

    System.Windows.Forms.DataVisualization.Charting.Series series1 = 
    new System.Windows.Forms.DataVisualization.Charting.Series(); 
    System.Windows.Forms.DataVisualization.Charting.Series series2 = 
    new System.Windows.Forms.DataVisualization.Charting.Series(); 

    A1_area.Position.Auto = false; 
    this.A1.ChartAreas.Add(A1_area); 
    this.A1.Location = new System.Drawing.Point(216, 43); 

    series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar100; 
    series1.Name = "Series1"; 
    series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar100; 
    series2.Name = "Series2"; 

    this.A1.Series.Add(series1); 
    this.A1.Series.Add(series2); 

    this.A1.Size = new System.Drawing.Size(100, 34); 
    this.A1.TabStop = false; 
    this.A1.Click += new System.EventHandler(this.A1_Click); 
} 

private void A1_Click(object sender, EventArgs e) 
{ 
} 

private System.Windows.Forms.DataVisualization.Charting.Chart A1; 
+0

你有沒有考慮過使用數組/圖表列表? – Eser

+0

感謝您的回答。是的,我嘗試使用列表,但我無法使用它。我搜索了任何類似的例子,但沒有找到任何。也許你可以給我一個提示或任何類似的鏈接? – lumens

+0

'我搜索了任何類似的例子,但沒有找到任何'成爲第一個創建這樣一個令人難以置信的代碼.... – Eser

回答

0

首先,你應該把這個線與其他包含語句以便更容易語法:

using System.Windows.Forms.DataVisualization.Charting; 

我也不清楚自己真正想要實現的目標,但是這是我想你問(未經測試):

public void Main() 
    { 
     string[] chartNames = { "A1", "A2", "A3", "A4", "B1", "B2", "B3" }; 
     List<Chart> charts = new List<Chart>; 
     int i = 0; 
     foreach(string cname in chartNames) 
     { 
      i++; 
      ChartArea chartArea = new ChartArea(); 
      Chart thisChart = new Chart(); 
      Series series1 = new Series(); 
      Series series2 = new Series(); 
      chartArea.Position.Auto = false; 
      thisChart.ChartAreas.Add(chartArea); 
      //each chart will be 43 units higher than the last 
      thisChart.Location = new System.Drawing.Point(216, (43*i)); 
      series1.ChartType = SeriesChartType.StackedBar100; 
      series2.ChartType = SeriesChartType.StackedBar100; 
      series1.Name = "Series1"; 
      series2.Name = "Series2"; 
      thisChart.Series.Add(series1); 
      thisChart.Series.Add(series2); 
      thisChart.Size = new System.Drawing.Size(100, 34); 
      thisChart.TabStop = false; 
      //sets the name of this chart to the corresponding string from the array of names 
      thisChart.Name = cname;   
      thisChart.Click += new System.EventHandler(this.chartClick); 
      //add this chart to the list of charts 
      charts.Add(thisChart); 
     }  
    } 

    private void chartClick(Object sender, EventArgs e) 
    { 
     //you can differentiate between charts using the sender.name property 
     Chart senderChart = (Chart)sender; 
     if(senderChart.Name == "A1") 
     { 
      //if the chart with name A1 is clicked... 
     } 
    } 

此外,您還可以使用類似的代碼foreach循環內以下,設置區域爲特定的名字:

  if(cname == "A1") 
      { 
       //set properties for chart with name A1 
       series1.Name = "A1 series 1"; 
       series2.Name = "A2 series 2"; 
       thisChart.Size = new System.Drawing.Size(200, 50); 
      } 
+0

哇,非常感謝您的時間!這個解決方案完全按照我想要的方式工作。用foreach循環設置名字也應該派上用場。再次感謝你。 – lumens

0

我不能發表評論,但還可以讓你知道從哪裏開始。我沒有使用圖表類,但類是類。不知道具體細節我正在暗中刺探。您需要一個數組中的名稱列表來創建一個包含1個chartarea,2個具有設定值的系列的圖表列表。以陣列中名稱命名的圖表和圖表區域的名稱。開始了。你需要一種方法。該方法將返回對象Chart對象的列表。圖表屬性和圖表屬性內的ChartArea中的名稱屬性將用於通過名稱引用特定的圖表,但整個列表將是您放入的任何內容。在這裏,我們繼續。首先,我不喜歡長名稱空間,所以我會添加下面的行來縮短打字時間。

using System.Windows.Forms.DataVisualization.Charting; 

private List<Chart> MakeCharts(string[] YourArray) 
{ 
    var myChart = new List<Chart>(); 
    Foreach(string chName in YourArray) 
    { 
     ChartArea area = new ChartArea(); 
     area.Name = chName; 
     Chart chart = new Chart(); 
     chart.Name = chName; 
     Series series1 = new Series(); 
     Series series2 = new Series(); 

     area.Position.Auto = false; 
     chart.ChartAreas.Add(area); 
     chart.Location = new System.Drawing.Point(216, 43); 
     series1.ChartType = SeriesChartType.StackedBar100; 
     series1.Name = "Series1"; 
     series2.ChartType = SeriesChartType.StackedBar100; 
     series2.Name = "Series2"; 

     chart.Series.Add(series1); 
     chart.Series.Add(series2); 

     chart.Size = new System.Drawing.Size(100, 34); 
     chart.TabStop = false; 

     //Here is the tricky part. See explanation after code. 
     chart.Click += new System.EventHandler(charts_Click); 
     myChart.Add(chart); 
    } 

    return myChart; 
} 

用它在你的主要發言,如:

var MyListofCharts = MakeCharts(myArrayofNames); 

你可以通過使用一個foreach每個排行榜中的週期或使用LINQ其控制的名字引用特定的。在控制上沒有任何東西可以玩,但是對於linq遊戲應該是這樣的。

var mySelectChart = [Chart]MyListofCharts.First(c => c.Name == "Chart Name"); 

因爲我認爲這可能會將它作爲IEnumerable或其他奇怪的東西返回,所以我將它放回原樣。

最後是您的活動。我不知道Chart控件,但是你的事件必須是動態的,因爲你無法編程爲每個Chart編寫一個新的事件處理程序。 click事件中的sender對象應該包含它來自的控件。如果您在點擊處於活動狀態的內部添加了一箇中斷並檢查發件人對象,則應該能夠找到引用其來自的控件的屬性。一旦你知道它的類型,你可以將它作爲該類型轉換出來,並從對象中操作控制權,並且可以爲所有圖表點擊事件設置1個事件處理程序。

+0

謝謝你的時間!我已經嘗試過提前一點提出解決方案,但我也會嘗試將其中一個用於教育目的,因爲我在使用方法方面非常糟糕。謝謝。 – lumens